зеркало из https://github.com/Azure/orkestra.git
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:
Родитель
dbef8a74ba
Коммит
e30ea47b26
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
"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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче