Skip to content

Commit

Permalink
Fix configmap namespacing issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed Feb 1, 2023
1 parent 6a48cb2 commit 4445087
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion controllers/k8ssandra/cassandra_metrics_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions controllers/k8ssandra/datacenters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 13 additions & 6 deletions pkg/telemetry/cassandra_agent/cassandra_agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)},
}
Expand All @@ -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)
Expand Down Expand Up @@ -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",
},
},
},
Expand Down
19 changes: 13 additions & 6 deletions pkg/telemetry/cassandra_agent/cassandra_agent_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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},
}
Expand Down Expand Up @@ -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",
},
},
},
Expand All @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions pkg/test/test_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func NewK8ssandraCluster(name string, namespace string) k8ssandraapi.K8ssandraCl
},
},
},
Datacenters: []k8ssandraapi.CassandraDatacenterTemplate{
{
Meta: k8ssandraapi.EmbeddedObjectMeta{
Name: "dc1",
Namespace: "dc-namespace",
},
},
},
},
},
}
Expand Down

0 comments on commit 4445087

Please sign in to comment.