Skip to content

Commit 9c9e91a

Browse files
[Bugfix] GT-103 Ensure pod names not too long (#1053)
1 parent c3003a3 commit 9c9e91a

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
4+
- (Bugfix) Ensure pod names not too long
45

56
## [1.2.14](https://github.com/arangodb/kube-arangodb/tree/1.2.14) (2022-07-14)
67
- (Feature) Add ArangoSync TLS based rotation

pkg/apis/shared/names.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var (
3131
arangodPrefixes = []string{"CRDN-", "PRMR-", "AGNT-", "SNGL-"}
3232
)
3333

34+
const (
35+
qualifiedNameMaxLength int = 63
36+
)
37+
3438
// StripArangodPrefix removes well know arangod ID prefixes from the given id.
3539
func StripArangodPrefix(id string) string {
3640
for _, prefix := range arangodPrefixes {
@@ -43,13 +47,11 @@ func StripArangodPrefix(id string) string {
4347

4448
// FixupResourceName ensures that the given name
4549
// complies with kubernetes name requirements.
46-
// If the name is to long or contains invalid characters,
47-
// if will be adjusted and a hash with be added.
50+
// If the name is too long or contains invalid characters,
51+
// it will be adjusted and a hash will be added.
4852
func FixupResourceName(name string) string {
49-
maxLen := 63
50-
5153
sb := strings.Builder{}
52-
needHash := len(name) > maxLen
54+
needHash := len(name) > qualifiedNameMaxLength
5355
for _, ch := range name {
5456
if unicode.IsDigit(ch) || unicode.IsLower(ch) || ch == '-' {
5557
sb.WriteRune(ch)
@@ -64,8 +66,8 @@ func FixupResourceName(name string) string {
6466
if needHash {
6567
hash := sha1.Sum([]byte(name))
6668
h := fmt.Sprintf("-%0x", hash[:3])
67-
if len(result)+len(h) > maxLen {
68-
result = result[:maxLen-(len(h))]
69+
if len(result)+len(h) > qualifiedNameMaxLength {
70+
result = result[:qualifiedNameMaxLength-(len(h))]
6971
}
7072
result = result + h
7173
}
@@ -75,7 +77,13 @@ func FixupResourceName(name string) string {
7577
// CreatePodHostName returns the hostname of the pod for a member with
7678
// a given id in a deployment with a given name.
7779
func CreatePodHostName(deploymentName, role, id string) string {
78-
return deploymentName + "-" + role + "-" + StripArangodPrefix(id)
80+
suffix := "-" + role + "-" + StripArangodPrefix(id)
81+
maxDeplNameLen := qualifiedNameMaxLength - len(suffix)
82+
// shorten deployment name part if resulting name is too big:
83+
if maxDeplNameLen > 1 && len(deploymentName) > maxDeplNameLen {
84+
deploymentName = deploymentName[:maxDeplNameLen-1]
85+
}
86+
return deploymentName + suffix
7987
}
8088

8189
// CreatePersistentVolumeClaimName returns the name of the persistent volume claim for a member with

pkg/apis/shared/names_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ import (
2424
"testing"
2525

2626
"github.com/stretchr/testify/require"
27+
"k8s.io/apimachinery/pkg/util/validation"
2728
)
2829

2930
func Test_Names(t *testing.T) {
3031
t.Run("Empty", func(t *testing.T) {
3132
require.EqualError(t, ValidateResourceName(""), "Name '' is not a valid resource name")
3233
})
34+
t.Run("Pod name is valid", func(t *testing.T) {
35+
name := CreatePodHostName("the-matrix-db", "arangodb-coordinator", "CRDN-549cznuy")
36+
require.Empty(t, validation.IsQualifiedName(name))
37+
38+
name = CreatePodHostName("the-matrix-application-db-instance", "arangodb-coordinator", "CRDN-549cznuy")
39+
require.Empty(t, validation.IsQualifiedName(name))
40+
})
3341
}

0 commit comments

Comments
 (0)