mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-14 11:57:37 +00:00
[Bugfix] GT-103 Ensure pod names not too long (#1053)
This commit is contained in:
parent
c3003a31eb
commit
9c9e91a1f1
3 changed files with 25 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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 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)
|
||||
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue