From ce6f2877cef94d5c91bafe2ce4acba52ee22c1fe Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Thu, 10 Mar 2022 19:24:48 +0100 Subject: [PATCH] Initialize prefix remapping map to avoid panic (#2453) A `nil` `target` passed to `AsOptionalMap` causes a panic. Signed-off-by: Pierangelo Di Pilato --- leaderelection/config.go | 9 ++-- leaderelection/config_test.go | 78 +++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/leaderelection/config.go b/leaderelection/config.go index 3ef47483df..13e0832e5e 100644 --- a/leaderelection/config.go +++ b/leaderelection/config.go @@ -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), } } diff --git a/leaderelection/config_test.go b/leaderelection/config_test.go index 3b95b448c0..6e821831ef 100644 --- a/leaderelection/config_test.go +++ b/leaderelection/config_test.go @@ -38,10 +38,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{}, } } @@ -122,10 +123,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", @@ -139,10 +141,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{}, }, }} @@ -168,6 +171,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 {