From 5d20aae20ff19215a457a98f25b44c832c3e9179 Mon Sep 17 00:00:00 2001 From: wille Date: Wed, 4 Sep 2024 18:54:31 +0200 Subject: [PATCH] Fix normalized resource names with leading numbers --- internal/utils/resourcenames.go | 16 ++-------------- internal/utils/resourcenames_test.go | 1 + 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/internal/utils/resourcenames.go b/internal/utils/resourcenames.go index a9c2257..aa7a8fd 100644 --- a/internal/utils/resourcenames.go +++ b/internal/utils/resourcenames.go @@ -32,22 +32,10 @@ func normalize(s string) string { s = regexp.MustCompile("[-/_.]+").ReplaceAllString(s, "-") // Only allow leading a-z to comply with DNS1035 for Service names and label values - for i, c := range s { - if isAlpha(c) { - break - } - - s = s[i+1:] - } + s = regexp.MustCompile("^[^a-z]+").ReplaceAllString(s, "") // Only allow trailing alphanumeric characters to comply with DNS1123 and DNS1035 - for i := len(s) - 1; i >= 0; i-- { - if isAlphanumeric(rune(s[i])) { - break - } - - s = s[:i] - } + s = regexp.MustCompile("[^a-z0-9]+$").ReplaceAllString(s, "") if len(s) > validation.DNS1123LabelMaxLength { s = normalize(s[:validation.DNS1123LabelMaxLength]) diff --git a/internal/utils/resourcenames_test.go b/internal/utils/resourcenames_test.go index d0c02b5..356698c 100644 --- a/internal/utils/resourcenames_test.go +++ b/internal/utils/resourcenames_test.go @@ -35,6 +35,7 @@ var _ = Describe("Resource names", func() { validate("test-test", "test-test") validate(strings.Repeat("a", 100), strings.Repeat("a", validation.DNS1035LabelMaxLength)) validate("feature/v1å[]", "feature-v1") + validate("86954rgpd_tillo-api-upgrade", "rgpd-tillo-api-upgrade") }) })