Skip to content

Commit 3319768

Browse files
Merge pull request #1179 from openshift-cherrypick-robot/cherry-pick-1177-to-release-4.18
[release-4.18] OCPBUGS-51018: ensure that storage names don't end in dashes
2 parents 00882ba + b3536e6 commit 3319768

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
@@ -119,17 +119,20 @@ func GenerateStorageName(listers *regopclient.StorageListers, additionalInfo ...
119119

120120
// Check the length and pad or truncate as needed
121121
switch {
122+
case len(name) == 61:
123+
// if the name is 61 chars, we don't have room for a dash before the padding
124+
name = name + string(byte(97+rand.Intn(25)))
122125
case len(name) < 62:
123126
padding := 62 - len(name) - 1
124127
bytes := make([]byte, padding)
125128
for i := 0; i < padding; i++ {
126129
bytes[i] = byte(97 + rand.Intn(25)) // a=97 and z=97+25
127130
}
128131
name = fmt.Sprintf("%s-%s", name, string(bytes))
129-
case len(name) > 62:
132+
case len(name) >= 62:
130133
name = name[0:62]
131-
if strings.HasSuffix(name, "-") {
132-
name = name[0:61] + string(byte(97+rand.Intn(25)))
134+
if before, hasDash := strings.CutSuffix(name, "-"); hasDash {
135+
name = before + string(byte(97+rand.Intn(25)))
133136
}
134137
}
135138

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)