Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation check for projected pod names length #1120

Merged
merged 1 commit into from
Jan 5, 2024
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/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 @@
}
}

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())
}

Check warning on line 104 in apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go#L101-L104

Added lines #L101 - L104 were not covered by tests
}
} 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 @@
// 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
}

Check warning on line 174 in apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go#L173-L174

Added lines #L173 - L174 were not covered by tests

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
Loading