diff --git a/CHANGELOG/CHANGELOG-1.14.md b/CHANGELOG/CHANGELOG-1.14.md index a9aa4e8dc..069036345 100644 --- a/CHANGELOG/CHANGELOG-1.14.md +++ b/CHANGELOG/CHANGELOG-1.14.md @@ -14,4 +14,6 @@ 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 -* [BUGFIX] [#1226](https://github.com/k8ssandra/k8ssandra-operator/issues/1226) Medusa purge cronjob should be created in the operator namespace \ No newline at end of file +* [BUGFIX] [#1226](https://github.com/k8ssandra/k8ssandra-operator/issues/1226) Medusa purge cronjob should be created in the operator namespace +* [BUGFIX] [#1141](https://github.com/k8ssandra/k8ssandra-operator/issues/1141) Use DC name override when naming secondary resources +* [BUGFIX] [#1138](https://github.com/k8ssandra/k8ssandra-operator/issues/1138) Use cluster name override for metrics agent ConfigMap diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go index 97e4b1b30..a0b0c2321 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go @@ -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" @@ -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 diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go index 1f86cce89..aed36c9ed 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go @@ -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) { @@ -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, }) @@ -376,6 +378,7 @@ func createClusterObjWithCassandraConfig(name, namespace string) *K8ssandraClust Datacenters: []CassandraDatacenterTemplate{ { + Meta: EmbeddedObjectMeta{Name: "dc1"}, K8sContext: "envtest", Size: 1, }, @@ -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") +} diff --git a/controllers/k8ssandra/cassandra_telemetry_reconciler.go b/controllers/k8ssandra/cassandra_telemetry_reconciler.go index 7067ac5d6..1de74796f 100644 --- a/controllers/k8ssandra/cassandra_telemetry_reconciler.go +++ b/controllers/k8ssandra/cassandra_telemetry_reconciler.go @@ -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), } diff --git a/controllers/k8ssandra/per_node_config.go b/controllers/k8ssandra/per_node_config.go index 612f5391e..4b0b517c6 100644 --- a/controllers/k8ssandra/per_node_config.go +++ b/controllers/k8ssandra/per_node_config.go @@ -44,10 +44,10 @@ func (r *K8ssandraClusterReconciler) reconcileDefaultPerNodeConfiguration( kcKey := utils.GetKey(kc) - perNodeConfigKey := nodeconfig.NewDefaultPerNodeConfigMapKey(kcKey, dcConfig) + perNodeConfigKey := nodeconfig.NewDefaultPerNodeConfigMapKey(kc, dcConfig) dcLogger = dcLogger.WithValues("PerNodeConfigMap", perNodeConfigKey) - desiredPerNodeConfig := nodeconfig.NewDefaultPerNodeConfigMap(kcKey, dcConfig) + desiredPerNodeConfig := nodeconfig.NewDefaultPerNodeConfigMap(kcKey, kc, dcConfig) if desiredPerNodeConfig != nil { annotations.AddHashAnnotation(desiredPerNodeConfig) } diff --git a/controllers/reaper/vector_test.go b/controllers/reaper/vector_test.go index 440084c07..cb61f7253 100644 --- a/controllers/reaper/vector_test.go +++ b/controllers/reaper/vector_test.go @@ -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) } diff --git a/controllers/stargate/vector_test.go b/controllers/stargate/vector_test.go index e18fbe183..1070b720d 100644 --- a/controllers/stargate/vector_test.go +++ b/controllers/stargate/vector_test.go @@ -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) } diff --git a/pkg/nodeconfig/generate.go b/pkg/nodeconfig/generate.go index 700c3b41d..ad4c3f301 100644 --- a/pkg/nodeconfig/generate.go +++ b/pkg/nodeconfig/generate.go @@ -2,6 +2,7 @@ package nodeconfig import ( "fmt" + cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" "strings" "github.com/k8ssandra/k8ssandra-operator/pkg/labels" @@ -16,9 +17,9 @@ import ( // NewDefaultPerNodeConfigMap generates a ConfigMap that contains default per-node configuration // files for this DC. It returns nil if this DC does not require any per-node configuration. -func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, dcConfig *cassandra.DatacenterConfig) *corev1.ConfigMap { +func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, kc *k8ssandraapi.K8ssandraCluster, dcConfig *cassandra.DatacenterConfig) *corev1.ConfigMap { - configKey := NewDefaultPerNodeConfigMapKey(kcKey, dcConfig) + configKey := NewDefaultPerNodeConfigMapKey(kc, dcConfig) perNodeConfig := newPerNodeConfigMap(kcKey, configKey) // append all the default per-node configuration to the ConfigMap; @@ -36,10 +37,10 @@ func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, dcConfig *cassandra. return nil } -func NewDefaultPerNodeConfigMapKey(kcKey types.NamespacedName, dcConfig *cassandra.DatacenterConfig) types.NamespacedName { +func NewDefaultPerNodeConfigMapKey(kc *k8ssandraapi.K8ssandraCluster, dcConfig *cassandra.DatacenterConfig) types.NamespacedName { return types.NamespacedName{ - Name: fmt.Sprintf("%s-%s-per-node-config", kcKey.Name, dcConfig.CassDcName()), - Namespace: utils.FirstNonEmptyString(dcConfig.Meta.Namespace, kcKey.Namespace), + Name: cassdcapi.CleanupForKubernetes(kc.CassClusterName() + "-" + dcConfig.CassDcName() + "-per-node-config"), + Namespace: utils.FirstNonEmptyString(dcConfig.Meta.Namespace, kc.Namespace), } } diff --git a/pkg/nodeconfig/generate_test.go b/pkg/nodeconfig/generate_test.go index cfff87a48..1037a1785 100644 --- a/pkg/nodeconfig/generate_test.go +++ b/pkg/nodeconfig/generate_test.go @@ -69,7 +69,10 @@ func TestNewDefaultPerNodeConfigMap(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := NewDefaultPerNodeConfigMap(tt.kcKey, tt.dc) + kc := &api.K8ssandraCluster{ + ObjectMeta: metav1.ObjectMeta{Name: tt.kcKey.Name, Namespace: tt.kcKey.Namespace}, + } + got := NewDefaultPerNodeConfigMap(tt.kcKey, kc, tt.dc) assert.Equal(t, tt.want, got) }) } diff --git a/pkg/reaper/vector.go b/pkg/reaper/vector.go index f55d98afa..dd9da7e13 100644 --- a/pkg/reaper/vector.go +++ b/pkg/reaper/vector.go @@ -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 { diff --git a/pkg/stargate/deployments.go b/pkg/stargate/deployments.go index cf7464e40..0587ad2cc 100644 --- a/pkg/stargate/deployments.go +++ b/pkg/stargate/deployments.go @@ -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) } diff --git a/pkg/stargate/vector.go b/pkg/stargate/vector.go index f65e15332..d06a7f7a5 100644 --- a/pkg/stargate/vector.go +++ b/pkg/stargate/vector.go @@ -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 { diff --git a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go index 963b88190..c69062d80 100644 --- a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go +++ b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go @@ -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() @@ -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(), }, }, }, diff --git a/pkg/telemetry/vector.go b/pkg/telemetry/vector.go index 2e5e0acda..820d7c07b 100644 --- a/pkg/telemetry/vector.go +++ b/pkg/telemetry/vector.go @@ -330,5 +330,5 @@ func BuildVectorAgentConfigMap(namespace, k8cName, dcName, k8cNamespace, vectorT } func VectorAgentConfigMapName(k8cName, dcName string) string { - return fmt.Sprintf("%s-%s-cass-vector", k8cName, dcName) + return cassdcapi.CleanupForKubernetes(fmt.Sprintf("%s-%s-cass-vector", k8cName, dcName)) }