diff --git a/controllers/k8ssandra/auth_test.go b/controllers/k8ssandra/auth_test.go index 3d33ce79d..e6e08ded3 100644 --- a/controllers/k8ssandra/auth_test.go +++ b/controllers/k8ssandra/auth_test.go @@ -127,8 +127,6 @@ func createSingleDcClusterNoAuth(t *testing.T, ctx context.Context, f *framework err = f.DeleteK8ssandraCluster(ctx, client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}, timeout, interval) require.NoError(t, err, "failed to delete K8ssandraCluster") f.AssertObjectDoesNotExist(ctx, t, dcKey, &cassdcapi.CassandraDatacenter{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, stargateKey, &stargateapi.Stargate{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, reaperKey, &reaperapi.Reaper{}, timeout, interval) } // createSingleDcClusterAuth verifies that it is possible to create an authenticated cluster with one DC and with @@ -240,8 +238,6 @@ func createSingleDcClusterAuth(t *testing.T, ctx context.Context, f *framework.F err = f.DeleteK8ssandraCluster(ctx, client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}, timeout, interval) require.NoError(t, err, "failed to delete K8ssandraCluster") f.AssertObjectDoesNotExist(ctx, t, dcKey, &cassdcapi.CassandraDatacenter{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, stargateKey, &stargateapi.Stargate{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, reaperKey, &reaperapi.Reaper{}, timeout, interval) } // createSingleDcClusterAuthExternalSecrets verifies that kubernetes secrets for credentials are not created when @@ -367,8 +363,6 @@ func createSingleDcClusterAuthExternalSecrets(t *testing.T, ctx context.Context, err = f.DeleteK8ssandraCluster(ctx, client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}, timeout, interval) require.NoError(t, err, "failed to delete K8ssandraCluster") f.AssertObjectDoesNotExist(ctx, t, dcKey, &cassdcapi.CassandraDatacenter{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, stargateKey, &stargateapi.Stargate{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, reaperKey, &reaperapi.Reaper{}, timeout, interval) } func createSingleDcClusterExternalInternode(t *testing.T, ctx context.Context, f *framework.Framework, namespace string) { diff --git a/controllers/k8ssandra/cassandra_metrics_agent_test.go b/controllers/k8ssandra/cassandra_metrics_agent_test.go index e5defbfdc..531b516b2 100644 --- a/controllers/k8ssandra/cassandra_metrics_agent_test.go +++ b/controllers/k8ssandra/cassandra_metrics_agent_test.go @@ -4,8 +4,6 @@ import ( "context" "testing" - "github.com/stretchr/testify/assert" - cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" api "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" telemetryapi "github.com/k8ssandra/k8ssandra-operator/apis/telemetry/v1alpha1" @@ -87,9 +85,13 @@ func createSingleDcClusterWithMetricsAgent(t *testing.T, ctx context.Context, f // check that we have the right ConfigMap 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") - } + require.Eventually(func() bool { + if err := f.Get(ctx, agentCmKey, &agentCm); err != nil { + t.Log("could not find expected metrics-agent-config configmap") + return false + } + return f.IsOwnedByCassandraDatacenter(&agentCm) + }, timeout, interval) // Verify the ConfigMap is set to be mounted require.True(len(dc.Spec.StorageConfig.AdditionalVolumes) > 0) @@ -109,9 +111,4 @@ func createSingleDcClusterWithMetricsAgent(t *testing.T, ctx context.Context, f err = f.DeleteK8ssandraCluster(ctx, client.ObjectKey{Namespace: namespace, Name: kc.Name}, timeout, interval) require.NoError(err, "failed to delete K8ssandraCluster") f.AssertObjectDoesNotExist(ctx, t, dcKey, &cassdcapi.CassandraDatacenter{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, - agentCmKey, - &corev1.ConfigMap{}, - timeout, - interval) } diff --git a/controllers/k8ssandra/reaper_test.go b/controllers/k8ssandra/reaper_test.go index 3ed5c7945..3c8fd6b1e 100644 --- a/controllers/k8ssandra/reaper_test.go +++ b/controllers/k8ssandra/reaper_test.go @@ -254,7 +254,10 @@ func createMultiDcClusterWithControlPlaneReaper(t *testing.T, ctx context.Contex Name: "reaper"}, } t.Log("check that control plane reaper is created") - require.Eventually(f.ReaperExists(ctx, cpReaperKey), timeout, interval) + withReaper := f.NewWithReaper(ctx, cpReaperKey) + require.Eventually(withReaper(func(r *reaperapi.Reaper) bool { + return true + }), timeout, interval) kc := &api.K8ssandraCluster{ ObjectMeta: metav1.ObjectMeta{ diff --git a/controllers/k8ssandra/remove_dc_test.go b/controllers/k8ssandra/remove_dc_test.go index e9184139a..da73f36d5 100644 --- a/controllers/k8ssandra/remove_dc_test.go +++ b/controllers/k8ssandra/remove_dc_test.go @@ -9,7 +9,6 @@ import ( cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" api "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" reaperapi "github.com/k8ssandra/k8ssandra-operator/apis/reaper/v1alpha1" - stargateapi "github.com/k8ssandra/k8ssandra-operator/apis/stargate/v1alpha1" "github.com/k8ssandra/k8ssandra-operator/pkg/annotations" "github.com/k8ssandra/k8ssandra-operator/pkg/cassandra" "github.com/k8ssandra/k8ssandra-operator/pkg/stargate" @@ -291,8 +290,6 @@ func deleteDcWithStargateAndReaper(ctx context.Context, t *testing.T, f *framewo assertDatacenterRemovedFromClusterStatus(ctx, t, f, kcKey, dc2Key) f.AssertObjectDoesNotExist(ctx, t, dc2Key, &cassdcapi.CassandraDatacenter{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, sg2Key, &stargateapi.Stargate{}, timeout, interval) - f.AssertObjectDoesNotExist(ctx, t, reaper2Key, &reaperapi.Reaper{}, timeout, interval) verifyReplicationOfInternalKeyspacesUpdated(t, mockMgmtApi, replication, updatedReplication) } diff --git a/controllers/k8ssandra/vector.go b/controllers/k8ssandra/vector.go index f694b8a99..3aebdad05 100644 --- a/controllers/k8ssandra/vector.go +++ b/controllers/k8ssandra/vector.go @@ -69,6 +69,7 @@ func (r *K8ssandraClusterReconciler) setupVectorCleanup( remoteClient client.Client, logger logr.Logger, ) result.ReconcileResult { + logger.Info("Setting up Vector Agent ConfigMap cleanup") configMapKey := client.ObjectKey{ Namespace: dc.Namespace, Name: telemetry.VectorAgentConfigMapName(kc.SanitizedName(), dc.SanitizedName()), diff --git a/controllers/k8ssandra/vector_test.go b/controllers/k8ssandra/vector_test.go index 343c68d1e..37949a2b9 100644 --- a/controllers/k8ssandra/vector_test.go +++ b/controllers/k8ssandra/vector_test.go @@ -152,7 +152,7 @@ func createSingleDcClusterWithVector(t *testing.T, ctx context.Context, f *frame assert.Fail(t, "error setting status ready", err) } - // Check that the Vector config map was created + // Check that the Vector config map was created and is owned by the DC vectorConfigMapKey := types.NamespacedName{Namespace: namespace, Name: telemetry.VectorAgentConfigMapName(kc.Name, dc1Key.Name)} vectorConfigMap := &corev1.ConfigMap{} require.Eventually(func() bool { @@ -161,7 +161,7 @@ func createSingleDcClusterWithVector(t *testing.T, ctx context.Context, f *frame t.Logf("failed to get Vector config map: %v", err) return false } - return true + return f.IsOwnedByCassandraDatacenter(vectorConfigMap) }, timeout, interval, "timed out waiting for Vector config map") // Check that Vector configuration was set to the SystemLoggerResources @@ -184,6 +184,5 @@ func createSingleDcClusterWithVector(t *testing.T, ctx context.Context, f *frame t.Log("deleting K8ssandraCluster") err = f.DeleteK8ssandraCluster(ctx, client.ObjectKey{Namespace: namespace, Name: kc.Name}, timeout, interval) require.NoError(err, "failed to delete K8ssandraCluster") - f.AssertObjectDoesNotExist(ctx, t, framework.ClusterKey{K8sContext: f.DataPlaneContexts[1], NamespacedName: vectorConfigMapKey}, &corev1.ConfigMap{}, timeout, interval) f.AssertObjectDoesNotExist(ctx, t, dcKey, &cassdcapi.CassandraDatacenter{}, timeout, interval) } diff --git a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go index c69062d80..1d163a134 100644 --- a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go +++ b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go @@ -3,6 +3,7 @@ package cassandra_agent import ( "context" "path/filepath" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "time" "github.com/adutra/goalesce" @@ -157,6 +158,9 @@ func (c Configurator) ReconcileTelemetryAgentConfig(dc *cassdcapi.CassandraDatac Namespace: c.Kluster.Namespace, } desiredCm.SetLabels(labels.CleanedUpByLabels(KlKey)) + if err := controllerutil.SetControllerReference(dc, desiredCm, c.RemoteClient.Scheme()); err != nil { + return result.Error(err) + } recRes := reconciliation.ReconcileObject(c.Ctx, c.RemoteClient, c.RequeueDelay, *desiredCm) switch { diff --git a/test/framework/framework.go b/test/framework/framework.go index e2d96b485..f411d9e48 100644 --- a/test/framework/framework.go +++ b/test/framework/framework.go @@ -704,7 +704,7 @@ func (f *Framework) withStargate(ctx context.Context, key ClusterKey, condition func (f *Framework) StargateExists(ctx context.Context, key ClusterKey) func() bool { withStargate := f.NewWithStargate(ctx, key) return withStargate(func(s *stargateapi.Stargate) bool { - return true + return f.IsOwnedByCassandraDatacenter(s) }) } @@ -735,7 +735,7 @@ func (f *Framework) withReaper(ctx context.Context, key ClusterKey, condition fu func (f *Framework) ReaperExists(ctx context.Context, key ClusterKey) func() bool { withReaper := f.NewWithReaper(ctx, key) return withReaper(func(r *reaperapi.Reaper) bool { - return true + return f.IsOwnedByCassandraDatacenter(r) }) } @@ -772,3 +772,17 @@ func (f *Framework) AssertObjectDoesNotExist(ctx context.Context, t *testing.T, return err != nil && errors.IsNotFound(err) }, timeout, interval, fmt.Sprintf("failed to verify object (%+v) does not exist", key)) } + +// IsOwnedByCassandraDatacenter checks that the given resource has an owner reference to a CassandraDatacenter. +// We can't directly verify the deletion itself because controller-manager isn't actually running in EnvTest. +// See also: https://github.com/kubernetes-sigs/controller-runtime/issues/626 +func (f *Framework) IsOwnedByCassandraDatacenter(resource metav1.Object) bool { + for _, ref := range resource.GetOwnerReferences() { + // Ideally we'd want to verify that ref.UID matches the CassandraDatacenter UID, but it's always readily + // available in all env tests. This should be good enough. + if ref.Kind == "CassandraDatacenter" && *ref.Controller { + return true + } + } + return false +}