Skip to content

Commit 796fe98

Browse files
Fix the managed-by-label getting populated with invalid value.
Signed-off-by: Vamsi Krishna Siddu <[email protected]>
1 parent 496c5fc commit 796fe98

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

cmd/csi-provisioner/util.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ import (
2828
// it will truncate the base, add the hash of the base and return [base]-[hash]-[suffix]
2929
// copied from https://github.com/openshift/library-go/blob/master/pkg/build/naming/namer.go
3030
func getNameWithMaxLength(base, suffix string, maxLength int) string {
31-
name := fmt.Sprintf("%s-%s", base, suffix)
31+
name := suffix
32+
if base != "" {
33+
name = fmt.Sprintf("%s-%s", base, suffix)
34+
}
35+
3236
if len(name) <= maxLength {
3337
return name
3438
}
3539

3640
baseLength := maxLength - 10 /*length of -hash-*/ - len(suffix)
3741

3842
// if the suffix is too long, ignore it
39-
if baseLength < 0 {
43+
if baseLength <= 0 {
44+
shortName := hash(name)
4045
prefix := base[0:min(len(base), max(0, maxLength-9))]
41-
// Calculate hash on initial base-suffix string
42-
shortName := fmt.Sprintf("%s-%s", prefix, hash(name))
46+
if prefix != "" {
47+
// Calculate hash on initial base-suffix string
48+
shortName = fmt.Sprintf("%s-%s", prefix, hash(name))
49+
}
4350
return shortName[:min(maxLength, len(shortName))]
4451
}
4552

cmd/csi-provisioner/util_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,56 @@ func TestGetNameWithMaxLength(t *testing.T) {
3030
testcases := map[string]struct {
3131
expected string
3232
nodeName string
33+
base string
3334
}{
3435
"unchanged": {
3536
expected: fmt.Sprintf("%s-%s", externalProvisioner, "node01"),
3637
nodeName: "node01",
38+
base: externalProvisioner,
39+
},
40+
"with empty base": {
41+
expected: fmt.Sprintf("%s", "node01"),
42+
nodeName: "node01",
43+
base: "",
3744
},
3845
"exactly63": {
3946
// 20 (external-provisioner) + 1 (-) + 4 (node) + 38 = 63
4047
expected: fmt.Sprintf("%s-%s", externalProvisioner, fmt.Sprintf("node%s", strings.Repeat("a", 38))),
4148
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 38)),
49+
base: externalProvisioner,
4250
},
4351
"one over": {
4452
expected: fmt.Sprintf("%s-%s-%s", "external-p", "53f40b57", fmt.Sprintf("node%s", strings.Repeat("a", 39))),
4553
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 39)),
54+
base: externalProvisioner,
55+
},
56+
"long node name with 52": {
57+
expected: fmt.Sprintf("%s-%s-%s", "e", "53f40b57", fmt.Sprintf("node%s", strings.Repeat("a", 48))),
58+
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 48)),
59+
base: externalProvisioner,
60+
},
61+
62+
"long node name with 53, ignore suffix ": {
63+
expected: fmt.Sprintf("%s-%s", externalProvisioner, "a3607ff1"),
64+
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 49)),
65+
base: externalProvisioner,
4666
},
4767
"very long, ignore suffix": {
4868
expected: fmt.Sprintf("%s-%s", externalProvisioner, "df38e37f"),
4969
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 63)),
70+
base: externalProvisioner,
71+
},
72+
"long node name ,with empty base": {
73+
expected: fmt.Sprintf("%s", "fa65587e"),
74+
nodeName: fmt.Sprintf("node%s", strings.Repeat("a", 63)),
75+
base: "",
5076
},
5177
}
5278

5379
for name, c := range testcases {
5480
t.Run(name, func(t *testing.T) {
5581
expected := c.expected
56-
res := getNameWithMaxLength(externalProvisioner, c.nodeName, 63)
82+
res := getNameWithMaxLength(c.base, c.nodeName, 63)
5783
if expected != res {
5884
t.Errorf("Expected: %s, does not match result: %s", expected, res)
5985
}

0 commit comments

Comments
 (0)