Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
fix: Sanitize uppercase letters from resource name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sergioifg94 authored and stianst committed Mar 10, 2020
1 parent 1761511 commit ec03b5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/model/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"strconv"
"strings"
"unicode"

v1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/model/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ec03b5a

Please sign in to comment.