From 314bfe70ceb663a6421dd270745b899a972aea93 Mon Sep 17 00:00:00 2001 From: Nikita Vanyasin Date: Tue, 12 Jul 2022 18:57:01 +0400 Subject: [PATCH 1/2] Ensure pod names not too long --- CHANGELOG.md | 1 + pkg/apis/shared/names.go | 24 ++++++++++++++++-------- pkg/apis/shared/names_test.go | 8 ++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0bd8ac7f..70a44a964 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - (Feature) Member restarts metric - (Bugfix) Infinite loop fix in ArangoD AsyncClient - (Bugfix) Add Panic Handler +- (Bugfix) Ensure pod names not too long ## [1.2.13](https://github.com/arangodb/kube-arangodb/tree/1.2.13) (2022-06-07) - (Bugfix) Fix arangosync members state inspection diff --git a/pkg/apis/shared/names.go b/pkg/apis/shared/names.go index 127da2230..3e177148c 100644 --- a/pkg/apis/shared/names.go +++ b/pkg/apis/shared/names.go @@ -31,6 +31,10 @@ var ( arangodPrefixes = []string{"CRDN-", "PRMR-", "AGNT-", "SNGL-"} ) +const ( + qualifiedNameMaxLength int = 63 +) + // StripArangodPrefix removes well know arangod ID prefixes from the given id. func StripArangodPrefix(id string) string { for _, prefix := range arangodPrefixes { @@ -43,13 +47,11 @@ func StripArangodPrefix(id string) string { // FixupResourceName ensures that the given name // complies with kubernetes name requirements. -// If the name is to long or contains invalid characters, -// if will be adjusted and a hash with be added. +// If the name is too long or contains invalid characters, +// it will be adjusted and a hash with be added. func FixupResourceName(name string) string { - maxLen := 63 - sb := strings.Builder{} - needHash := len(name) > maxLen + needHash := len(name) > qualifiedNameMaxLength for _, ch := range name { if unicode.IsDigit(ch) || unicode.IsLower(ch) || ch == '-' { sb.WriteRune(ch) @@ -64,8 +66,8 @@ func FixupResourceName(name string) string { if needHash { hash := sha1.Sum([]byte(name)) h := fmt.Sprintf("-%0x", hash[:3]) - if len(result)+len(h) > maxLen { - result = result[:maxLen-(len(h))] + if len(result)+len(h) > qualifiedNameMaxLength { + result = result[:qualifiedNameMaxLength-(len(h))] } result = result + h } @@ -75,7 +77,13 @@ func FixupResourceName(name string) string { // CreatePodHostName returns the hostname of the pod for a member with // a given id in a deployment with a given name. func CreatePodHostName(deploymentName, role, id string) string { - return deploymentName + "-" + role + "-" + StripArangodPrefix(id) + suffix := "-" + role + "-" + StripArangodPrefix(id) + maxDeplNameLen := qualifiedNameMaxLength - len(suffix) + // shorten deployment name part if resulting name is too big: + if maxDeplNameLen > 1 && len(deploymentName) > maxDeplNameLen { + deploymentName = deploymentName[:maxDeplNameLen-1] + } + return deploymentName + suffix } // CreatePersistentVolumeClaimName returns the name of the persistent volume claim for a member with diff --git a/pkg/apis/shared/names_test.go b/pkg/apis/shared/names_test.go index ce40f6336..41494f3d4 100644 --- a/pkg/apis/shared/names_test.go +++ b/pkg/apis/shared/names_test.go @@ -24,10 +24,18 @@ import ( "testing" "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/util/validation" ) func Test_Names(t *testing.T) { t.Run("Empty", func(t *testing.T) { require.EqualError(t, ValidateResourceName(""), "Name '' is not a valid resource name") }) + t.Run("Pod name is valid", func(t *testing.T) { + name := CreatePodHostName("the-matrix-db", "arangodb-coordinator", "CRDN-549cznuy") + require.Empty(t, validation.IsQualifiedName(name)) + + name = CreatePodHostName("the-matrix-application-db-instance", "arangodb-coordinator", "CRDN-549cznuy") + require.Empty(t, validation.IsQualifiedName(name)) + }) } From b6a64b603a34ce38d4e67224eb2d4178c82dd121 Mon Sep 17 00:00:00 2001 From: Nikita Vanyasin Date: Tue, 12 Jul 2022 21:40:53 +0400 Subject: [PATCH 2/2] Fix comment typo --- pkg/apis/shared/names.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/apis/shared/names.go b/pkg/apis/shared/names.go index 3e177148c..e618bfedf 100644 --- a/pkg/apis/shared/names.go +++ b/pkg/apis/shared/names.go @@ -48,7 +48,7 @@ func StripArangodPrefix(id string) string { // FixupResourceName ensures that the given name // complies with kubernetes name requirements. // If the name is too long or contains invalid characters, -// it will be adjusted and a hash with be added. +// it will be adjusted and a hash will be added. func FixupResourceName(name string) string { sb := strings.Builder{} needHash := len(name) > qualifiedNameMaxLength