Skip to content

Commit

Permalink
Add validation check for projected pod names length
Browse files Browse the repository at this point in the history
  • Loading branch information
adejanovski committed Nov 24, 2023
1 parent be45e84 commit bc7629d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG/CHANGELOG-1.11.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

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

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go#L100-L103

Added lines #L100 - L103 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 @@ -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
}

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

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go#L166-L167

Added lines #L166 - L167 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 @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -368,6 +378,9 @@ func createMinimalClusterObj(name, namespace string) *K8ssandraCluster {
},
Datacenters: []CassandraDatacenterTemplate{
{
Meta: EmbeddedObjectMeta{
Name: "dc1",
},
K8sContext: "envtest",
Size: 1,
},
Expand Down

0 comments on commit bc7629d

Please sign in to comment.