Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use controller references to clean up DC components #1022

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions CHANGELOG/CHANGELOG-1.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ 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

* [ENHANCEMENT] [#992](https://github.com/k8ssandra/k8ssandra-operator/issues/992) Use controller references to clean up DC components
6 changes: 0 additions & 6 deletions controllers/k8ssandra/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
olim7t marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
69 changes: 58 additions & 11 deletions controllers/k8ssandra/cassandra_metrics_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -78,18 +76,63 @@ func createSingleDcClusterWithMetricsAgent(t *testing.T, ctx context.Context, f
t.Log("check that the datacenter was created")
dcKey := framework.ClusterKey{NamespacedName: types.NamespacedName{Namespace: namespace, Name: "dc1"}, K8sContext: f.DataPlaneContexts[0]}
require.Eventually(f.DatacenterExists(ctx, dcKey), timeout, interval)

t.Log("update datacenter status to ready")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I don't love having so much indirection and would almost rather keep this logic inline. But there is a function that does this already that might be useful to you here.

kcKey := framework.NewClusterKey(f.ControlPlaneContext, namespace, kc.Name)
err = f.PatchDatacenterStatus(ctx, dcKey, func(dc *cassdcapi.CassandraDatacenter) {
dc.Status.CassandraOperatorProgress = cassdcapi.ProgressReady
dc.SetCondition(cassdcapi.DatacenterCondition{
Type: cassdcapi.DatacenterReady,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
})
})
require.NoError(err, "failed to update datacenter status to ready")

t.Log("check that the K8ssandraCluster status is updated")
require.Eventually(func() bool {
kc := &api.K8ssandraCluster{}
err = f.Get(ctx, kcKey, kc)

if err != nil {
t.Logf("failed to get K8ssandraCluster: %v", err)
return false
}

if len(kc.Status.Datacenters) == 0 {
return false
}

k8ssandraStatus, found := kc.Status.Datacenters[dcKey.Name]
if !found {
t.Logf("status for datacenter %s not found", dcKey)
return false
}

condition := findDatacenterCondition(k8ssandraStatus.Cassandra, cassdcapi.DatacenterReady)
return condition != nil && condition.Status == corev1.ConditionTrue
}, timeout, interval, "timed out waiting for K8ssandraCluster status update")

require.Eventually(func() bool {
return f.UpdateDatacenterGeneration(ctx, t, dcKey)
}, timeout, interval, "failed to update dc1 generation")

// Check that we have the right volumes and volume mounts.
dc := &cassdcapi.CassandraDatacenter{}
if err := f.Get(ctx, dcKey, dc); err != nil {
require.Fail("could not find dc")
}

// 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]}
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)
Expand All @@ -109,9 +152,13 @@ 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)
}

func findDatacenterCondition(status *cassdcapi.CassandraDatacenterStatus, condType cassdcapi.DatacenterConditionType) *cassdcapi.DatacenterCondition {
for _, condition := range status.Conditions {
if condition.Type == condType {
return &condition
}
}
return nil
}
Loading
Loading