Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize prefix remapping map to avoid panic #2453

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions leaderelection/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ func (c *Config) GetComponentConfig(name string) ComponentConfig {

func defaultConfig() *Config {
return &Config{
Buckets: 1,
LeaseDuration: 60 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
Buckets: 1,
LeaseDuration: 60 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: make(map[string]string),
}
}

Expand Down
79 changes: 67 additions & 12 deletions leaderelection/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"

"knative.dev/pkg/kmap"
)

Expand All @@ -38,10 +39,11 @@ const (

func okConfig() *Config {
return &Config{
Buckets: 1,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Buckets: 1,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
}
}

Expand Down Expand Up @@ -122,10 +124,11 @@ func TestNewConfigMapFromData(t *testing.T) {
"buckets": "5",
},
expected: &Config{
Buckets: 5,
LeaseDuration: 2 * time.Second,
RenewDeadline: 3 * time.Second,
RetryPeriod: 4 * time.Second,
Buckets: 5,
LeaseDuration: 2 * time.Second,
RenewDeadline: 3 * time.Second,
RetryPeriod: 4 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}, {
name: "prioritize new keys",
Expand All @@ -139,10 +142,11 @@ func TestNewConfigMapFromData(t *testing.T) {
"buckets": "7",
},
expected: &Config{
Buckets: 7,
LeaseDuration: 1 * time.Second,
RenewDeadline: 2 * time.Second,
RetryPeriod: 3 * time.Second,
Buckets: 7,
LeaseDuration: 1 * time.Second,
RenewDeadline: 2 * time.Second,
RetryPeriod: 3 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}}

Expand All @@ -168,6 +172,57 @@ func TestNewConfigMapFromData(t *testing.T) {
}
}

func TestNewConfigFromMap(t *testing.T) {

tt := []struct {
name string
data map[string]string
want Config
wantErr bool
}{{
name: "ok config",
data: map[string]string{
"lease-duration": "15s",
"buckets": "5",
},
want: Config{
Buckets: 5,
LeaseDuration: 15 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}, {
name: "ok config, prefix map",
data: map[string]string{
"lease-duration": "15s",
"buckets": "5",
"map-lease-prefix.reconciler": "reconciler1",
},
want: Config{
Buckets: 5,
LeaseDuration: 15 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: map[string]string{
"reconciler": "reconciler1",
},
},
}}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c, err := NewConfigFromMap(tc.data)
if tc.wantErr != (err != nil) {
t.Fatalf("want err %v got %v", tc.wantErr, err)
}
if diff := cmp.Diff(tc.want, *c); diff != "" {
t.Fatal("(-want, +got)", diff)
}
})
}
}

func TestGetComponentConfig(t *testing.T) {
const expectedName = "the-component"
cases := []struct {
Expand Down