Skip to content

Commit 7eba922

Browse files
Merge pull request #1185 from openshift-cherrypick-robot/cherry-pick-1183-to-release-4.14
[release-4.14] OCPBUGS-51312: ensure that storage names don't end in dashes
2 parents a552128 + 19c52eb commit 7eba922

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pkg/storage/util/util.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,20 @@ func GenerateStorageName(listers *regopclient.StorageListers, additionalInfo ...
116116

117117
// Check the length and pad or truncate as needed
118118
switch {
119+
case len(name) == 61:
120+
// if the name is 61 chars, we don't have room for a dash before the padding
121+
name = name + string(byte(97+rand.Intn(25)))
119122
case len(name) < 62:
120123
padding := 62 - len(name) - 1
121124
bytes := make([]byte, padding)
122125
for i := 0; i < padding; i++ {
123126
bytes[i] = byte(97 + rand.Intn(25)) // a=97 and z=97+25
124127
}
125128
name = fmt.Sprintf("%s-%s", name, string(bytes))
126-
case len(name) > 62:
129+
case len(name) >= 62:
127130
name = name[0:62]
128-
if strings.HasSuffix(name, "-") {
129-
name = name[0:61] + string(byte(97+rand.Intn(25)))
131+
if before, hasDash := strings.CutSuffix(name, "-"); hasDash {
132+
name = before + string(byte(97+rand.Intn(25)))
130133
}
131134
}
132135

pkg/storage/util/util_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func TestGenerateStorageName(t *testing.T) {
8888
infraName: "MY-INFRA",
8989
additionalInfo: []string{"test1", "test2"},
9090
},
91+
{
92+
name: "govcloud region name",
93+
infraName: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
94+
additionalInfo: []string{"us-gov-west-1"},
95+
},
9196
} {
9297
t.Run(tt.name, func(t *testing.T) {
9398
l := regopclient.StorageListers{Infrastructures: MockInfrastructureLister{
@@ -123,6 +128,10 @@ func TestGenerateStorageName(t *testing.T) {
123128
if n != strings.ToLower(n) {
124129
t.Errorf("name should not contain upper case: %s", n)
125130
}
131+
132+
if strings.HasSuffix(n, "-") {
133+
t.Errorf("name should not end in a dash: %s", n)
134+
}
126135
})
127136
}
128137
}

0 commit comments

Comments
 (0)