diff --git a/CHANGELOG/CHANGELOG-1.12.md b/CHANGELOG/CHANGELOG-1.12.md index 4028bc645..598182305 100644 --- a/CHANGELOG/CHANGELOG-1.12.md +++ b/CHANGELOG/CHANGELOG-1.12.md @@ -15,3 +15,4 @@ When cutting a new release, update the `unreleased` heading to the tag being gen ## unreleased +- [ENHANCEMENT] [#1115](https://github.com/k8ssandra/k8ssandra-operator/issues/1115) Add a validation check for the projected pod names length \ No newline at end of file diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go index 408561af9..08850eb3e 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go @@ -88,6 +88,27 @@ func (r *K8ssandraCluster) validateK8ssandraCluster() error { } } + if err := r.validateStatefulsetNameSize(); err != nil { + return err + } + + return nil +} + +func (r *K8ssandraCluster) validateStatefulsetNameSize() error { + for _, dc := range r.Spec.Cassandra.Datacenters { + if len(dc.Racks) > 0 { + for _, rack := range dc.Racks { + if len(r.SanitizedName()+"-"+dc.CassDcName()+"-"+rack.Name+"-sts-") > 60 { + return fmt.Errorf("the name of the statefulset for rack %s in DC %s is too long", rack.Name, dc.CassDcName()) + } + } + } else { + if len(r.SanitizedName()+"-"+dc.CassDcName()+"-default-sts-") > 60 { + return fmt.Errorf("the name of the statefulset for DC %s is too long", dc.CassDcName()) + } + } + } return nil } @@ -148,6 +169,10 @@ func (r *K8ssandraCluster) ValidateUpdate(old runtime.Object) error { // TODO StorageConfig can not be modified (not Cluster or DC level) in existing datacenters // TODO Racks can only be added and only at the end of the list - no other operation is allowed to racks + if err := r.validateStatefulsetNameSize(); err != nil { + return err + } + return nil } diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go index cedcffeb4..82071f8f5 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go @@ -144,6 +144,7 @@ func TestWebhook(t *testing.T) { t.Run("StorageConfigValidation", testStorageConfigValidation) t.Run("NumTokensValidation", testNumTokens) t.Run("NumTokensValidationInUpdate", testNumTokensInUpdate) + t.Run("StsNameTooLong", testStsNameTooLong) } func testContextValidation(t *testing.T) { @@ -337,6 +338,15 @@ func testNumTokens(t *testing.T) { required.Error(errorOnValidate, "expected error when changing the value of num tokens while also changing other field values") } +func testStsNameTooLong(t *testing.T) { + required := require.New(t) + createNamespace(required, "too-long-namespace") + cluster := createMinimalClusterObj("create-very-long-cluster-name-which-will-overflow-our-limit", "too-long-namespace") + + err := k8sClient.Create(ctx, cluster) + required.Error(err) +} + func createNamespace(require *require.Assertions, namespace string) { ns := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -386,6 +396,9 @@ func createMinimalClusterObj(name, namespace string) *K8ssandraCluster { }, Datacenters: []CassandraDatacenterTemplate{ { + Meta: EmbeddedObjectMeta{ + Name: "dc1", + }, K8sContext: "envtest", Size: 1, },