Update ConvertToDNS1123() to validate and fix other special characters, max len and starts-with rules (#298)

* Update ConvertToDNS1123 helper function

* Update ConvertDNS1123() to handle corner cases

* Move consts to consts.go file

Co-authored-by: Nitish Malhotra <nitish.malhotra@gmail.com>
This commit is contained in:
Rajwinder Mahal 2021-06-11 10:14:51 -07:00 коммит произвёл GitHub
Родитель dbef8a74ba
Коммит e30ea47b26
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 80 добавлений и 11 удалений

Просмотреть файл

@ -1,15 +1,5 @@
package utils 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 { func GetSubchartName(appName, scName string) string {
scName = TruncateString(scName, subchartNameMaxLen) scName = TruncateString(scName, subchartNameMaxLen)
appName = TruncateString(GetHash(appName), DNS1123NameMaximumLength-len(scName)-1) appName = TruncateString(GetHash(appName), DNS1123NameMaximumLength-len(scName)-1)

13
pkg/utils/consts.go Normal file
Просмотреть файл

@ -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
)

Просмотреть файл

@ -4,6 +4,7 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"regexp"
"strings" "strings"
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
@ -12,7 +13,18 @@ import (
) )
func ConvertToDNS1123(in string) string { 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 { func ConvertSliceToDNS1123(in []string) []string {

Просмотреть файл

@ -4,6 +4,60 @@ import (
"testing" "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) { func TestTruncateString(t *testing.T) {
type args struct { type args struct {
in string in string