Skip to content

Commit

Permalink
Fix normalized resource names with leading numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
wille committed Sep 4, 2024
1 parent 02f1880 commit 5d20aae
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
16 changes: 2 additions & 14 deletions internal/utils/resourcenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions internal/utils/resourcenames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})

Expand Down

0 comments on commit 5d20aae

Please sign in to comment.