From ec03b5ab470427f53e4d6a6b50cc194f855e367b Mon Sep 17 00:00:00 2001 From: Sergio Franco Date: Mon, 9 Mar 2020 11:05:32 +0000 Subject: [PATCH] fix: Sanitize uppercase letters from resource name Modify SanitizeResourceName function to transform uppercase characters into lowercase, as kubernetes doesn't allow uppercase characters as part of the resource name: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/ Create unit test to cover that function. --- pkg/model/util.go | 5 +++-- pkg/model/util_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/model/util.go b/pkg/model/util.go index f405f8b3d..45d3949fb 100644 --- a/pkg/model/util.go +++ b/pkg/model/util.go @@ -7,6 +7,7 @@ import ( "net" "strconv" "strings" + "unicode" v1 "k8s.io/api/core/v1" @@ -63,9 +64,9 @@ func SanitizeResourceName(name string) string { continue } - // Uppercase letters + // Uppercase letters are transformed to lowercase if ascii >= 65 && ascii <= 90 { - sb.WriteRune(char) + sb.WriteRune(unicode.ToLower(char)) continue } diff --git a/pkg/model/util_test.go b/pkg/model/util_test.go index bb3b9ae3c..42b5d6f0d 100644 --- a/pkg/model/util_test.go +++ b/pkg/model/util_test.go @@ -66,3 +66,17 @@ func TestUtil_Test_GetServiceEnvVar(t *testing.T) { assert.Equal(t, GetServiceEnvVar("SERVICE_HOST"), "KEYCLOAK_POSTGRESQL_SERVICE_HOST") assert.Equal(t, GetServiceEnvVar("SERVICE_PORT"), "KEYCLOAK_POSTGRESQL_SERVICE_PORT") } + +func TestUtil_SanitizeResourceName(t *testing.T) { + expected := map[string]string{ + // Allowed characters + "test123-_.": "test123--.", + // Mixed of allowed characters and disallowed characters + "testTEST[(/%^&*,)]123-_.": "testtest123--.", + } + + for input, output := range expected { + actual := SanitizeResourceName(input) + assert.Equal(t, output, actual) + } +}