Skip to content

Commit

Permalink
Add validation check for projected pod names length (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
adejanovski authored Jan 5, 2024
1 parent b39cc4d commit a86edfe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG/CHANGELOG-1.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -386,6 +396,9 @@ func createMinimalClusterObj(name, namespace string) *K8ssandraCluster {
},
Datacenters: []CassandraDatacenterTemplate{
{
Meta: EmbeddedObjectMeta{
Name: "dc1",
},
K8sContext: "envtest",
Size: 1,
},
Expand Down

0 comments on commit a86edfe

Please sign in to comment.