Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

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

## [1.2.14](https://github.com/arangodb/kube-arangodb/tree/1.2.14) (2022-07-14)
- (Feature) Add ArangoSync TLS based rotation
Expand Down
24 changes: 16 additions & 8 deletions pkg/apis/shared/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 will 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)
Expand All @@ -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
}
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/shared/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}