Skip to content

Commit

Permalink
Reject cluster creation if a DC has an invalid name (fixes #1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Mar 15, 2024
1 parent bd7ca25 commit 811b78c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha1

import (
"fmt"
"k8s.io/apimachinery/pkg/util/validation"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/k8ssandra/k8ssandra-operator/pkg/clientcache"
Expand Down Expand Up @@ -69,8 +71,15 @@ func (r *K8ssandraCluster) ValidateCreate() error {

func (r *K8ssandraCluster) validateK8ssandraCluster() error {
hasClusterStorageConfig := r.Spec.Cassandra.DatacenterOptions.StorageConfig != nil
// Verify given k8s-contexts are correct
for _, dc := range r.Spec.Cassandra.Datacenters {
dns1035Errs := validation.IsDNS1035Label(dc.Meta.Name)
if len(dns1035Errs) > 0 {
return fmt.Errorf(
"invalid DC name (you might want to use datacenterName to override the name used in Cassandra): %s",
strings.Join(dns1035Errs, ", "))
}

// Verify given k8s-context is correct
_, err := clientCache.GetRemoteClient(dc.K8sContext)
if err != nil {
// No client found for this context name, reject
Expand Down
14 changes: 14 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestWebhook(t *testing.T) {
t.Run("NumTokensValidationInUpdate", testNumTokensInUpdate)
t.Run("StsNameTooLong", testStsNameTooLong)
t.Run("MedusaPrefixMissing", testMedusaPrefixMissing)
t.Run("InvalidDcName", testInvalidDcName)
}

func testContextValidation(t *testing.T) {
Expand Down Expand Up @@ -219,6 +220,7 @@ func testStorageConfigValidation(t *testing.T) {
required.NoError(err)

cluster.Spec.Cassandra.Datacenters = append(cluster.Spec.Cassandra.Datacenters, CassandraDatacenterTemplate{
Meta: EmbeddedObjectMeta{Name: "dc2"},
K8sContext: "envtest",
Size: 1,
})
Expand Down Expand Up @@ -376,6 +378,7 @@ func createClusterObjWithCassandraConfig(name, namespace string) *K8ssandraClust

Datacenters: []CassandraDatacenterTemplate{
{
Meta: EmbeddedObjectMeta{Name: "dc1"},
K8sContext: "envtest",
Size: 1,
},
Expand Down Expand Up @@ -451,3 +454,14 @@ func testMedusaPrefixMissing(t *testing.T) {
err = k8sClient.Create(ctx, clusterWithPrefix)
required.NoError(err)
}

func testInvalidDcName(t *testing.T) {
required := require.New(t)
createNamespace(required, "ns")

clusterWithBadDcName := createMinimalClusterObj("bad-dc-name", "ns")
clusterWithBadDcName.Spec.Cassandra.Datacenters[0].Meta.Name = "DC1"
err := k8sClient.Create(ctx, clusterWithBadDcName)
required.Error(err)
required.Contains(err.Error(), "invalid DC name")
}

0 comments on commit 811b78c

Please sign in to comment.