Repository: knative/pkg
Pull request #2459 (2022-03-10 18:51:06)

[release-1.3] Initialize prefix remapping map to avoid panic
This is an automated cherry-pick of #2453

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2459#" title="Author self-approved">knative-prow-robot</a>*, *<a href="https://github.com/knative/pkg/pull/2459#pullrequestreview-906307122" title="LGTM">pierDipi</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **carlisia** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @carlisia` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/knative/pkg/blob/release-1.3/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["carlisia"]} -->
dprotaso: note: downstream tests failures are expected if the branch doesn't exist
codecov[bot]: # [Codecov](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) Report
> Merging [#2459](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (0bf7625) into [release-1.3](https://codecov.io/gh/knative/pkg/commit/2fdd5f232e77efb2afd214ddc89e0b341508a700?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (2fdd5f2) will **increase** coverage by `0.11%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/knative/pkg/pull/2459/graphs/tree.svg?width=650&height=150&src=pr&token=rlqpDnfxbO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)

```diff
@@               Coverage Diff               @@
##           release-1.3    #2459      +/-   ##
===============================================
+ Coverage        63.53%   63.64%   +0.11%     
===============================================
  Files              228      228              
  Lines             9999    10016      +17     
===============================================
+ Hits              6353     6375      +22     
+ Misses            3343     3340       -3     
+ Partials           303      301       -2     
```


| [Impacted Files](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) | Coverage Δ | |
|---|---|---|
| [leaderelection/config.go](https://codecov.io/gh/knative/pkg/pull/2459/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-bGVhZGVyZWxlY3Rpb24vY29uZmlnLmdv) | `80.00% <100.00%> (+0.40%)` | :arrow_up: |
| [hash/bucketer.go](https://codecov.io/gh/knative/pkg/pull/2459/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-aGFzaC9idWNrZXRlci5nbw==) | `100.00% <0.00%> (ø)` | |
| [reconciler/retry.go](https://codecov.io/gh/knative/pkg/pull/2459/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-cmVjb25jaWxlci9yZXRyeS5nbw==) | `100.00% <0.00%> (ø)` | |
| [controller/controller.go](https://codecov.io/gh/knative/pkg/pull/2459/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-Y29udHJvbGxlci9jb250cm9sbGVyLmdv) | `88.38% <0.00%> (+0.37%)` | :arrow_up: |
| [test/gcs/mock/mock.go](https://codecov.io/gh/knative/pkg/pull/2459/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-dGVzdC9nY3MvbW9jay9tb2NrLmdv) | `92.47% <0.00%> (+2.15%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Last update [2fdd5f2...0bf7625](https://codecov.io/gh/knative/pkg/pull/2459?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).

dprotaso: /lgtm
/approve
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2459#issuecomment-1064445637" title="Approved">dprotaso</a>*, *<a href="https://github.com/knative/pkg/pull/2459#" title="Author self-approved">knative-prow-robot</a>*, *<a href="https://github.com/knative/pkg/pull/2459#pullrequestreview-906307122" title="LGTM">pierDipi</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative/pkg/blob/release-1.3/OWNERS)~~ [dprotaso]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/leaderelection/config.go b/leaderelection/config.go
index 3ef47483d..13e0832e5 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),
 	}
 }
 


Test patch:
diff --git a/leaderelection/config_test.go b/leaderelection/config_test.go
index 2f2ca83a0..52b96e4c5 100644
--- a/leaderelection/config_test.go
+++ b/leaderelection/config_test.go
@@ -26,6 +26,7 @@ import (
 
 	"github.com/google/go-cmp/cmp"
 	corev1 "k8s.io/api/core/v1"
+
 	"knative.dev/pkg/kmap"
 )
 
@@ -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{},
 	}
 }
 
@@ -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",
@@ -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{},
 		},
 	}}
 
@@ -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 {