Skip to content

Commit 9f7e16d

Browse files
[v1.3.x] Fixed invalid object names generated for long package names (#4476)
* Fixed invalid object names generated for long package names * TrimDNS1123Label would trim strings but end up creating invalid DNS1123 strings. * FormatOperatorNameDNS1123 would return strings that were invalid if they begin or end with non-alphanumeric or hyphens. Fixes #4470 Signed-off-by: jesus m. rodriguez <[email protected]> * Handle capitals and use more efficient trim mechanism Signed-off-by: jesus m. rodriguez <[email protected]> * React to new expectation. Signed-off-by: jesus m. rodriguez <[email protected]> Co-authored-by: jesus m. rodriguez <[email protected]>
1 parent 16b8dae commit 9f7e16d

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
entries:
2+
- description: >
3+
Fixed invalid object names generated for long package names passed
4+
to `run packagemanifests` & `run bundle`.
5+
6+
kind: "bugfix"
7+
8+
breaking: false

internal/olm/operator/registry/configmap/deployment_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ var _ = Describe("Deployment", func() {
2828

2929
Describe("getRegistryServerName", func() {
3030
It("should return the formatted servername", func() {
31-
Expect(getRegistryServerName("pkgName")).Should(Equal("pkgName-registry-server"))
31+
Expect(getRegistryServerName("pkgName")).Should(Equal("pkgname-registry-server"))
3232
// This checks if all the special characters are handled correctly
33-
Expect(getRegistryServerName("$abc.foo$@(&#(&!*)@&#")).Should(Equal("-abc-foo--registry-server"))
33+
Expect(getRegistryServerName("$abc.foo$@(&#(&!*)@&#")).Should(Equal("abc-foo-registry-server"))
3434
})
3535
})
3636

@@ -39,7 +39,7 @@ var _ = Describe("Deployment", func() {
3939
labels := map[string]string{
4040
"owner": "operator-sdk",
4141
"package-name": "$abc.foo$@(&#(&!*)@&#",
42-
"server-name": "-abc-foo--registry-server",
42+
"server-name": "abc-foo-registry-server",
4343
}
4444

4545
Expect(getRegistryDeploymentLabels("$abc.foo$@(&#(&!*)@&#")).Should(Equal(labels))

internal/util/k8sutil/k8sutil.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ var dns1123LabelRegexp = regexp.MustCompile("[^a-zA-Z0-9]+")
9393
// replacing all non-compliant UTF-8 characters with "-".
9494
func FormatOperatorNameDNS1123(name string) string {
9595
if len(validation.IsDNS1123Label(name)) != 0 {
96-
return dns1123LabelRegexp.ReplaceAllString(name, "-")
96+
// Use - for any of the non-matching characters
97+
n := dns1123LabelRegexp.ReplaceAllString(name, "-")
98+
99+
// Now let's remove any leading or trailing -
100+
return strings.ToLower(strings.Trim(n, "-"))
97101
}
98102
return name
99103
}
@@ -102,7 +106,7 @@ func FormatOperatorNameDNS1123(name string) string {
102106
// by removing characters from the beginning of label such that len(label) <= 63.
103107
func TrimDNS1123Label(label string) string {
104108
if len(label) > validation.DNS1123LabelMaxLength {
105-
return label[len(label)-validation.DNS1123LabelMaxLength:]
109+
return strings.Trim(label[len(label)-validation.DNS1123LabelMaxLength:], "-")
106110
}
107111
return label
108112
}

internal/util/k8sutil/k8sutil_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,95 @@ func TestSupportsOwnerReference(t *testing.T) {
240240
})
241241
}
242242
}
243+
244+
func TestTrimDNS1123Label(t *testing.T) {
245+
type testcase struct {
246+
name string
247+
label string
248+
expected string
249+
}
250+
testcases := []testcase{
251+
{
252+
name: "return valid truncated values",
253+
label: "quay-io-raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
254+
expected: "raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
255+
},
256+
{
257+
name: "valid labels with proper length are noops",
258+
label: "raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
259+
expected: "raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
260+
},
261+
{
262+
name: "short invalid labels are left alone",
263+
label: "-$*@*#fixed-invalid(__$)@+==-name-#$($",
264+
expected: "-$*@*#fixed-invalid(__$)@+==-name-#$($",
265+
},
266+
}
267+
for _, tc := range testcases {
268+
t.Run(tc.name, func(t *testing.T) {
269+
result := TrimDNS1123Label(tc.label)
270+
assert.Equal(t, tc.expected, result)
271+
})
272+
}
273+
}
274+
275+
func TestFormatOperatorNameDNS1123(t *testing.T) {
276+
type testcase struct {
277+
name string
278+
label string
279+
expected string
280+
}
281+
testcases := []testcase{
282+
{
283+
name: "should not start with -",
284+
label: "-doesnot-start-with-hyphen",
285+
expected: "doesnot-start-with-hyphen",
286+
},
287+
{
288+
name: "should not start with non-alphanumeric",
289+
label: "$@*#(@does-notstart-garbage",
290+
expected: "does-notstart-garbage",
291+
},
292+
{
293+
name: "should not have non-alphanumeric",
294+
label: "sample-1234$@*#(@does-notstart-garbage",
295+
expected: "sample-1234-does-notstart-garbage",
296+
},
297+
{
298+
name: "should not end with non-alphanumeric",
299+
label: "sample-1234-does-notstart-garbage#$*@#*($_!-_@(",
300+
expected: "sample-1234-does-notstart-garbage",
301+
},
302+
{
303+
name: "should not start or end with hyphen",
304+
label: "-does-not-start-or-end-with-hyphen---",
305+
expected: "does-not-start-or-end-with-hyphen",
306+
},
307+
{
308+
name: "empty string is a noop",
309+
label: "",
310+
expected: "",
311+
},
312+
{
313+
name: "string of invalid characters results in empty string",
314+
label: "@#@#)$*!!_$#*$*!@",
315+
expected: "",
316+
},
317+
{
318+
name: "valid long names are not trimmed",
319+
label: "quay-io-raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
320+
expected: "quay-io-raffaelespazzoli-proactive-node-scaling-operator-bundle-latest",
321+
},
322+
{
323+
name: "should not contain capital letters",
324+
label: "QUAY-IO-gobble-gobBLE",
325+
expected: "quay-io-gobble-gobble",
326+
},
327+
}
328+
for _, tc := range testcases {
329+
t.Run(tc.name, func(t *testing.T) {
330+
result := FormatOperatorNameDNS1123(tc.label)
331+
assert.Equal(t, tc.expected, result)
332+
})
333+
}
334+
}

0 commit comments

Comments
 (0)