diff --git a/controllers/k8ssandra/cassandra_metrics_agent_test.go b/controllers/k8ssandra/cassandra_metrics_agent_test.go index aa6dd7e8e..605b4263d 100644 --- a/controllers/k8ssandra/cassandra_metrics_agent_test.go +++ b/controllers/k8ssandra/cassandra_metrics_agent_test.go @@ -96,7 +96,7 @@ func createSingleDcClusterWithMetricsAgent(t *testing.T, ctx context.Context, f } // check that we have the right ConfigMap - agentCmKey := framework.ClusterKey{NamespacedName: types.NamespacedName{Name: "test" + "-metrics-agent-config", Namespace: namespace}, K8sContext: f.DataPlaneContexts[0]} + agentCmKey := framework.ClusterKey{NamespacedName: types.NamespacedName{Name: "test-dc1" + "-metrics-agent-config", Namespace: namespace}, K8sContext: f.DataPlaneContexts[0]} agentCm := corev1.ConfigMap{} if err := f.Get(ctx, agentCmKey, &agentCm); err != nil { assert.Fail(t, "could not find expected metrics-agent-config configmap") diff --git a/controllers/k8ssandra/datacenters.go b/controllers/k8ssandra/datacenters.go index 57d53eb2b..d9acdbe5a 100644 --- a/controllers/k8ssandra/datacenters.go +++ b/controllers/k8ssandra/datacenters.go @@ -109,6 +109,8 @@ func (r *K8ssandraClusterReconciler) reconcileDatacenters(ctx context.Context, k Ctx: ctx, Kluster: kc, RequeueDelay: r.DefaultDelay, + DcNamespace: desiredDc.Namespace, + DcName: desiredDc.Name, } agentRes := agentCfg.ReconcileTelemetryAgentConfig(desiredDc) if agentRes.IsRequeue() { diff --git a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go index 230fafdc9..f2d2f37f8 100644 --- a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go +++ b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go @@ -36,6 +36,8 @@ type Configurator struct { Ctx context.Context RemoteClient client.Client RequeueDelay time.Duration + DcNamespace string + DcName string } func (c Configurator) GetTelemetryAgentConfigMap() (*corev1.ConfigMap, error) { @@ -55,8 +57,8 @@ func (c Configurator) GetTelemetryAgentConfigMap() (*corev1.ConfigMap, error) { cm := corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Namespace: c.Kluster.Namespace, - Name: c.Kluster.Name + "-metrics-agent-config", + Namespace: c.DcNamespace, + Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config", }, Data: map[string]string{filepath.Base(agentConfigLocation): string(yamlData)}, } @@ -69,10 +71,15 @@ func (c Configurator) ReconcileTelemetryAgentConfig(dc *cassdcapi.CassandraDatac if err != nil { return result.Error(err) } - cmObjectKey := types.NamespacedName{Name: c.Kluster.Name + "-metrics-agent-config", - Namespace: c.Kluster.Namespace} + cmObjectKey := types.NamespacedName{ + Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config", + Namespace: c.DcNamespace, + } labels.SetManagedBy(desiredCm, cmObjectKey) - KlKey := types.NamespacedName{Name: c.Kluster.Name, Namespace: c.Kluster.Namespace} + KlKey := types.NamespacedName{ + Name: c.Kluster.Name, + Namespace: c.Kluster.Namespace, + } partOfLabels := labels.PartOfLabels(KlKey) desiredCm.SetLabels(partOfLabels) annotations.AddHashAnnotation(desiredCm) @@ -124,7 +131,7 @@ func (c Configurator) AddStsVolumes(dc *cassdcapi.CassandraDatacenter) error { }, }, LocalObjectReference: corev1.LocalObjectReference{ - Name: c.Kluster.Name + "-metrics-agent-config", + Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config", }, }, }, diff --git a/pkg/telemetry/cassandra_agent/cassandra_agent_config_test.go b/pkg/telemetry/cassandra_agent/cassandra_agent_config_test.go index be861eb63..9ceb33f3a 100644 --- a/pkg/telemetry/cassandra_agent/cassandra_agent_config_test.go +++ b/pkg/telemetry/cassandra_agent/cassandra_agent_config_test.go @@ -26,6 +26,8 @@ var ( Ctx: context.Background(), RemoteClient: testutils.NewFakeClientWRestMapper(), RequeueDelay: time.Second * 1, + DcNamespace: testCluster.Spec.Cassandra.Datacenters[0].Meta.Namespace, + DcName: testCluster.Spec.Cassandra.Datacenters[0].Meta.Name, } expectedYaml string = `endpoint: address: 127.0.0.1 @@ -43,8 +45,8 @@ filters: func getExpectedConfigMap() corev1.ConfigMap { expectedCm := corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Namespace: Cfg.Kluster.Namespace, - Name: Cfg.Kluster.Name + "-metrics-agent-config", + Namespace: Cfg.DcNamespace, + Name: Cfg.Kluster.Name + "-" + Cfg.DcName + "-metrics-agent-config", }, Data: map[string]string{filepath.Base(agentConfigLocation): expectedYaml}, } @@ -92,7 +94,7 @@ func Test_AddStsVolumes(t *testing.T) { }, }, LocalObjectReference: corev1.LocalObjectReference{ - Name: Cfg.Kluster.Name + "-metrics-agent-config", + Name: Cfg.Kluster.Name + "-" + Cfg.DcName + "-metrics-agent-config", }, }, }, @@ -116,7 +118,7 @@ func Test_ReconcileTelemetryAgentConfig_CMCreateSuccess(t *testing.T) { recRes := Cfg.ReconcileTelemetryAgentConfig(&dc) assert.True(t, recRes.IsRequeue()) actualCm := &corev1.ConfigMap{} - err := Cfg.RemoteClient.Get(Cfg.Ctx, types.NamespacedName{Name: Cfg.Kluster.Name + "-metrics-agent-config", Namespace: Cfg.Kluster.Namespace}, actualCm) + err := Cfg.RemoteClient.Get(Cfg.Ctx, types.NamespacedName{Name: Cfg.Kluster.Name + "-" + Cfg.DcName + "-metrics-agent-config", Namespace: Cfg.DcNamespace}, actualCm) assert.NoError(t, err) } func Test_ReconcileTelemetryAgentConfig_CMCreateFailed(t *testing.T) { @@ -147,7 +149,9 @@ func Test_ReconcileTelemetryAgentConfig_CMUpdateSuccess(t *testing.T) { // After the update we should see the expected ConfigMap afterUpdateCM := &corev1.ConfigMap{} err = Cfg.RemoteClient.Get(Cfg.Ctx, - types.NamespacedName{Name: Cfg.Kluster.Name + "-metrics-agent-config", Namespace: Cfg.Kluster.Namespace}, + types.NamespacedName{ + Name: Cfg.Kluster.Name + "-" + Cfg.DcName + "-metrics-agent-config", + Namespace: Cfg.DcNamespace}, afterUpdateCM) assert.NoError(t, err) @@ -167,7 +171,10 @@ func Test_ReconcileTelemetryAgentConfig_CMUpdateDone(t *testing.T) { // After the update we should see the expected ConfigMap afterUpdateCM := &corev1.ConfigMap{} err := Cfg.RemoteClient.Get(Cfg.Ctx, - types.NamespacedName{Name: Cfg.Kluster.Name + "-metrics-agent-config", Namespace: Cfg.Kluster.Namespace}, + types.NamespacedName{ + Name: Cfg.Kluster.Name + "-" + Cfg.DcName + "-metrics-agent-config", + Namespace: Cfg.DcNamespace, + }, afterUpdateCM) assert.NoError(t, err) // If we reconcile again, we should move into the Done state. diff --git a/pkg/test/test_objects.go b/pkg/test/test_objects.go index 93211f1b3..8ddbe5e26 100644 --- a/pkg/test/test_objects.go +++ b/pkg/test/test_objects.go @@ -40,6 +40,14 @@ func NewK8ssandraCluster(name string, namespace string) k8ssandraapi.K8ssandraCl }, }, }, + Datacenters: []k8ssandraapi.CassandraDatacenterTemplate{ + { + Meta: k8ssandraapi.EmbeddedObjectMeta{ + Name: "dc1", + Namespace: "dc-namespace", + }, + }, + }, }, }, }