Skip to content

Commit

Permalink
Fix name override issues
Browse files Browse the repository at this point in the history
- reject cluster creation if a DC has an invalid name
- always use sanitized DC name override when naming secondary resources
- use cluster name override for metrics agent configMap

Fixes #1138
Fixes #1141
  • Loading branch information
olim7t committed Mar 18, 2024
1 parent 8189711 commit 00d660f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 11 deletions.
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")
}
2 changes: 1 addition & 1 deletion controllers/k8ssandra/cassandra_telemetry_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r *K8ssandraClusterReconciler) reconcileCassandraDCTelemetry(
cfg := telemetry.PrometheusResourcer{
MonitoringTargetNS: actualDc.Namespace,
MonitoringTargetName: actualDc.Name,
ServiceMonitorName: kc.SanitizedName() + "-" + actualDc.DatacenterName() + "-" + "cass-servicemonitor",
ServiceMonitorName: cassdcapi.CleanupForKubernetes(kc.CassClusterName() + "-" + actualDc.DatacenterName() + "-" + "cass-servicemonitor"),
Logger: logger,
CommonLabels: mustLabels(kc.Name, kc.Namespace, actualDc.DatacenterName(), commonLabels),
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/reaper/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func TestBuildVectorAgentConfigMap(t *testing.T) {
vectorToml := "Test"
vectorConfigMap := reaperpkg.CreateVectorConfigMap("k8ssandra-operator", vectorToml, test.NewCassandraDatacenter("testDc", "k8ssandra-operator"))
assert.Equal(t, vectorToml, vectorConfigMap.Data["vector.toml"])
assert.Equal(t, "test-cluster-testDc-reaper-vector", vectorConfigMap.Name)
assert.Equal(t, "test-cluster-testdc-reaper-vector", vectorConfigMap.Name)
assert.Equal(t, "k8ssandra-operator", vectorConfigMap.Namespace)
}
2 changes: 1 addition & 1 deletion controllers/stargate/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func TestBuildVectorAgentConfigMap(t *testing.T) {
vectorToml := "Test"
vectorConfigMap := stargatepkg.CreateVectorConfigMap("k8ssandra-operator", vectorToml, test.NewCassandraDatacenter("testDc", "k8ssandra-operator"))
assert.Equal(t, vectorToml, vectorConfigMap.Data["vector.toml"])
assert.Equal(t, "test-cluster-testDc-stargate-vector", vectorConfigMap.Name)
assert.Equal(t, "test-cluster-testdc-stargate-vector", vectorConfigMap.Name)
assert.Equal(t, "k8ssandra-operator", vectorConfigMap.Namespace)
}
2 changes: 1 addition & 1 deletion pkg/reaper/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// VectorAgentConfigMapName generates a ConfigMap name based on
// the K8s sanitized cluster name and datacenter name.
func VectorAgentConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, vectorConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), vectorConfigMap)
}

func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraDatacenter) *corev1.ConfigMap {
Expand Down
2 changes: 1 addition & 1 deletion pkg/stargate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func FilterConfig(config map[string]interface{}, retainedSettings []string) map[
func CreateStargateConfigMap(namespace, cassandraYaml, stargateCqlYaml string, dc cassdcapi.CassandraDatacenter) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: GeneratedConfigMapName(dc.Spec.ClusterName, dc.DatacenterName()),
Name: GeneratedConfigMapName(dc.Spec.ClusterName, dc.Name),

Check warning on line 45 in pkg/stargate/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/stargate/config.go#L45

Added line #L45 was not covered by tests
Namespace: namespace,
Labels: utils.MergeMap(
map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions pkg/stargate/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func computeVolumes(template *api.StargateTemplate, dc *cassdcapi.CassandraDatac
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: GeneratedConfigMapName(dc.Spec.ClusterName, dc.DatacenterName()),
Name: GeneratedConfigMapName(dc.Spec.ClusterName, dc.Name),
},
},
},
Expand Down Expand Up @@ -513,5 +513,5 @@ func createPodMeta(stargate *api.Stargate, deploymentName string) meta.Tags {
}

func GeneratedConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, cassandraConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), cassandraConfigMap)
}
2 changes: 1 addition & 1 deletion pkg/stargate/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// VectorAgentConfigMapName generates a ConfigMap name based on
// the K8s sanitized cluster name and datacenter name.
func VectorAgentConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, vectorConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), vectorConfigMap)
}

func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraDatacenter) *corev1.ConfigMap {
Expand Down
8 changes: 6 additions & 2 deletions pkg/telemetry/cassandra_agent/cassandra_agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,17 @@ func (c Configurator) GetTelemetryAgentConfigMap() (*corev1.ConfigMap, error) {
cm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.DcNamespace,
Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config",
Name: c.configMapName(),
},
Data: map[string]string{filepath.Base(agentConfigLocation): string(yamlData)},
}
return &cm, nil
}

func (c Configurator) configMapName() string {
return cassdcapi.CleanupForKubernetes(c.Kluster.CassClusterName() + "-" + c.DcName + "-metrics-agent-config")
}

func (c Configurator) ReconcileTelemetryAgentConfig(dc *cassdcapi.CassandraDatacenter) result.ReconcileResult {
//Reconcile the agent's ConfigMap
desiredCm, err := c.GetTelemetryAgentConfigMap()
Expand Down Expand Up @@ -182,7 +186,7 @@ func (c Configurator) AddVolumeSource(dc *cassdcapi.CassandraDatacenter) error {
},
},
LocalObjectReference: corev1.LocalObjectReference{
Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config",
Name: c.configMapName(),

Check warning on line 189 in pkg/telemetry/cassandra_agent/cassandra_agent_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/telemetry/cassandra_agent/cassandra_agent_config.go#L189

Added line #L189 was not covered by tests
},
},
},
Expand Down

0 comments on commit 00d660f

Please sign in to comment.