diff --git a/CHANGELOG/CHANGELOG-1.11.md b/CHANGELOG/CHANGELOG-1.11.md index 58ab84827..f249b1088 100644 --- a/CHANGELOG/CHANGELOG-1.11.md +++ b/CHANGELOG/CHANGELOG-1.11.md @@ -14,3 +14,5 @@ Changelog for the K8ssandra Operator, new PRs should update the `unreleased` sec When cutting a new release, update the `unreleased` heading to the tag being generated and date, like `## vX.Y.Z - YYYY-MM-DD` and create a new placeholder section for `unreleased` entries. ## unreleased + +- [ENHANCEMENT] [#1115](https://github.com/k8ssandra/k8ssandra-operator/issues/1115) Add a validation check for the projected pod names length diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go index 9999c4108..9ea794c35 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go @@ -87,6 +87,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 } @@ -141,6 +162,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 d0c72cb82..4f732ecc1 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go @@ -143,6 +143,7 @@ func TestWebhook(t *testing.T) { t.Run("ReaperKeyspaceValidation", testReaperKeyspaceValidation) t.Run("StorageConfigValidation", testStorageConfigValidation) t.Run("NumTokensValidation", testNumTokens) + t.Run("StsNameTooLong", testStsNameTooLong) } func testContextValidation(t *testing.T) { @@ -319,6 +320,15 @@ func testNumTokens(t *testing.T) { } +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{ @@ -368,6 +378,9 @@ func createMinimalClusterObj(name, namespace string) *K8ssandraCluster { }, Datacenters: []CassandraDatacenterTemplate{ { + Meta: EmbeddedObjectMeta{ + Name: "dc1", + }, K8sContext: "envtest", Size: 1, },