diff --git a/pkg/utils/chart.go b/pkg/utils/chart.go index dbb865c..23d12e4 100644 --- a/pkg/utils/chart.go +++ b/pkg/utils/chart.go @@ -1,15 +1,5 @@ package utils -const ( - DNS1123NameMaximumLength = 63 - - // subchartNameMaxLen is the maximum length of a subchart name. - // - // The max name length limit enforced by DNS1123 is 63 chars. We reserve 10 chars - // for concatenating application name hash. - subchartNameMaxLen = 53 -) - func GetSubchartName(appName, scName string) string { scName = TruncateString(scName, subchartNameMaxLen) appName = TruncateString(GetHash(appName), DNS1123NameMaximumLength-len(scName)-1) diff --git a/pkg/utils/consts.go b/pkg/utils/consts.go new file mode 100644 index 0000000..6f65882 --- /dev/null +++ b/pkg/utils/consts.go @@ -0,0 +1,13 @@ +package utils + +const ( + DNS1123NameMaximumLength = 63 + DNS1123NotAllowedChars = "[^-a-z0-9]" + DNS1123NotAllowedStartChars = "^[^a-z0-9]+" + + // subchartNameMaxLen is the maximum length of a subchart name. + // + // The max name length limit enforced by DNS1123 is 63 chars. We reserve 10 chars + // for concatenating application name hash. + subchartNameMaxLen = 53 +) diff --git a/pkg/utils/helpers.go b/pkg/utils/helpers.go index 1584f24..6168101 100644 --- a/pkg/utils/helpers.go +++ b/pkg/utils/helpers.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "regexp" "strings" fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" @@ -12,7 +13,18 @@ import ( ) func ConvertToDNS1123(in string) string { - return strings.ReplaceAll(in, "_", "-") + name := strings.ToLower(in) + + notAllowedChars := regexp.MustCompile(DNS1123NotAllowedChars) + name = notAllowedChars.ReplaceAllString(name, "-") + + notAllowedStartChars := regexp.MustCompile(DNS1123NotAllowedStartChars) + name = notAllowedStartChars.ReplaceAllString(name, "") + + if name == "" { + name = GetHash(in) + } + return TruncateString(name, DNS1123NameMaximumLength) } func ConvertSliceToDNS1123(in []string) []string { diff --git a/pkg/utils/helpers_test.go b/pkg/utils/helpers_test.go index 936c995..0a7f2f2 100644 --- a/pkg/utils/helpers_test.go +++ b/pkg/utils/helpers_test.go @@ -4,6 +4,60 @@ import ( "testing" ) +func TestConvertToDNS1123(t *testing.T) { + type args struct { + in string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "testing valid name", + args: args{ + in: "good-small-name", + }, + want: "good-small-name", + }, + { + name: "testing invalid name", + args: args{ + in: "tOk3_??ofTHE-Runner", + }, + want: "tok3---ofthe-runner", + }, + { + name: "testing all characters are invalid", + args: args{ + in: "?.?&^%#$@_??", + }, + want: GetHash("?.?&^%#$@_??")[0:63], + }, + { + name: "testing invalid start chars", + args: args{ + in: "----??tOk3_??ofTHE-Runner", + }, + want: "tok3---ofthe-runner", + }, + { + name: "testing very long name", + args: args{ + in: "very-long-name------------------------------------------------end", + }, + want: "very-long-name------------------------------------------------e", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ConvertToDNS1123(tt.args.in); got != tt.want { + t.Errorf("ConvertDNS1123() = %v, want %v", got, tt.want) + } + }) + } +} + func TestTruncateString(t *testing.T) { type args struct { in string