diff --git a/controllers/k8ssandra/cleanup.go b/controllers/k8ssandra/cleanup.go index 96663eafd..b96ecf094 100644 --- a/controllers/k8ssandra/cleanup.go +++ b/controllers/k8ssandra/cleanup.go @@ -8,7 +8,6 @@ import ( cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" api "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" - reaperapi "github.com/k8ssandra/k8ssandra-operator/apis/reaper/v1alpha1" "github.com/k8ssandra/k8ssandra-operator/pkg/annotations" "github.com/k8ssandra/k8ssandra-operator/pkg/k8ssandra" k8ssandralabels "github.com/k8ssandra/k8ssandra-operator/pkg/labels" @@ -64,10 +63,6 @@ func (r *K8ssandraClusterReconciler) checkDeletion(ctx context.Context, kc *api. hasErrors = true } - if r.deleteReapers(ctx, kc, dcTemplate, namespace, remoteClient, logger) { - hasErrors = true - } - if r.deleteDeployments(ctx, kc, dcTemplate, namespace, remoteClient, logger) { hasErrors = true } @@ -137,19 +132,7 @@ func (r *K8ssandraClusterReconciler) checkDcDeletion(ctx context.Context, kc *ap func (r *K8ssandraClusterReconciler) deleteDc(ctx context.Context, kc *api.K8ssandraCluster, dcName string, logger logr.Logger) result.ReconcileResult { kcKey := utils.GetKey(kc) - reaper, remoteClient, err := r.findReaperForDeletion(ctx, kcKey, dcName, nil) - if err != nil { - return result.Error(err) - } - - if reaper != nil { - if err = remoteClient.Delete(ctx, reaper); err != nil && !errors.IsNotFound(err) { - return result.Error(fmt.Errorf("failed to delete Reaper for dc (%s): %v", dcName, err)) - } - logger.Info("Deleted Reaper", "Reaper", utils.GetKey(reaper)) - } - - dc, remoteClient, err := r.findDcForDeletion(ctx, kcKey, dcName, remoteClient) + dc, remoteClient, err := r.findDcForDeletion(ctx, kcKey, dcName) if err != nil { return result.Error(err) } @@ -182,72 +165,19 @@ func (r *K8ssandraClusterReconciler) deleteDc(ctx context.Context, kc *api.K8ssa return result.Continue() } -func (r *K8ssandraClusterReconciler) findReaperForDeletion( - ctx context.Context, - kcKey client.ObjectKey, - dcName string, - remoteClient client.Client) (*reaperapi.Reaper, client.Client, error) { - - selector := k8ssandralabels.CleanedUpByLabels(kcKey) - options := &client.ListOptions{LabelSelector: labels.SelectorFromSet(selector)} - reaperList := &reaperapi.ReaperList{} - reaperName := kcKey.Name + "-" + dcName + "-reaper" - - if remoteClient == nil { - for _, remoteClient := range r.ClientCache.GetAllClients() { - err := remoteClient.List(ctx, reaperList, options) - if err != nil { - return nil, nil, fmt.Errorf("failed to find Reaper (%s) for DC (%s) deletion: %v", reaperName, dcName, err) - } - for _, reaper := range reaperList.Items { - if reaper.Name == reaperName { - return &reaper, remoteClient, nil - } - } - } - } else { - err := remoteClient.List(ctx, reaperList, options) - if err != nil { - return nil, nil, fmt.Errorf("failed to find Reaper (%s) for DC (%s) deletion: %v", reaperName, dcName, err) - } - - for _, reaper := range reaperList.Items { - if reaper.Name == reaperName { - return &reaper, remoteClient, nil - } - } - } - - return nil, nil, nil -} - func (r *K8ssandraClusterReconciler) findDcForDeletion( ctx context.Context, kcKey client.ObjectKey, - dcName string, - remoteClient client.Client) (*cassdcapi.CassandraDatacenter, client.Client, error) { + dcName string) (*cassdcapi.CassandraDatacenter, client.Client, error) { selector := k8ssandralabels.CleanedUpByLabels(kcKey) options := &client.ListOptions{LabelSelector: labels.SelectorFromSet(selector)} dcList := &cassdcapi.CassandraDatacenterList{} - if remoteClient == nil { - for _, remoteClient := range r.ClientCache.GetAllClients() { - err := remoteClient.List(ctx, dcList, options) - if err != nil { - return nil, nil, fmt.Errorf("failed to CassandraDatacenter (%s) for DC (%s) deletion: %v", dcName, dcName, err) - } - for _, dc := range dcList.Items { - if dc.Name == dcName { - return &dc, remoteClient, nil - } - } - } - } else { + for _, remoteClient := range r.ClientCache.GetAllClients() { err := remoteClient.List(ctx, dcList, options) if err != nil { - return nil, nil, fmt.Errorf("failed to find CassandraDatacenter (%s) for deletion: %v", dcName, err) + return nil, nil, fmt.Errorf("failed to CassandraDatacenter (%s) for DC (%s) deletion: %v", dcName, dcName, err) } - for _, dc := range dcList.Items { if dc.Name == dcName { return &dc, remoteClient, nil diff --git a/controllers/k8ssandra/reaper.go b/controllers/k8ssandra/reaper.go index 89acfce02..c0e4ff628 100644 --- a/controllers/k8ssandra/reaper.go +++ b/controllers/k8ssandra/reaper.go @@ -18,6 +18,7 @@ package k8ssandra import ( "context" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "github.com/go-logr/logr" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" @@ -30,7 +31,6 @@ import ( "github.com/k8ssandra/k8ssandra-operator/pkg/reaper" "github.com/k8ssandra/k8ssandra-operator/pkg/result" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -113,7 +113,10 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( if err := remoteClient.Get(ctx, reaperKey, actualReaper); err != nil { if errors.IsNotFound(err) { logger.Info("Creating Reaper resource") - if err := remoteClient.Create(ctx, desiredReaper); err != nil { + if err := controllerutil.SetControllerReference(actualDc, desiredReaper, r.Scheme); err != nil { + logger.Error(err, "Failed to set controller reference on Reaper resource") + return result.Error(err) + } else if err := remoteClient.Create(ctx, desiredReaper); err != nil { logger.Error(err, "Failed to create Reaper resource") return result.Error(err) } else { @@ -137,7 +140,10 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( resourceVersion := actualReaper.GetResourceVersion() desiredReaper.DeepCopyInto(actualReaper) actualReaper.SetResourceVersion(resourceVersion) - if err := remoteClient.Update(ctx, actualReaper); err != nil { + if err := controllerutil.SetControllerReference(actualDc, desiredReaper, r.Scheme); err != nil { + logger.Error(err, "Failed to set controller reference on Reaper resource") + return result.Error(err) + } else if err := remoteClient.Update(ctx, actualReaper); err != nil { logger.Error(err, "Failed to update Reaper resource") return result.Error(err) } @@ -179,37 +185,6 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( } } -func (r *K8ssandraClusterReconciler) deleteReapers( - ctx context.Context, - kc *api.K8ssandraCluster, - dcTemplate api.CassandraDatacenterTemplate, - namespace string, - remoteClient client.Client, - kcLogger logr.Logger, -) (hasErrors bool) { - selector := k8ssandralabels.CleanedUpByLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}) - reaperList := &reaperapi.ReaperList{} - options := client.ListOptions{ - Namespace: namespace, - LabelSelector: labels.SelectorFromSet(selector), - } - if err := remoteClient.List(ctx, reaperList, &options); err != nil { - kcLogger.Error(err, "Failed to list Reaper objects", "Context", dcTemplate.K8sContext) - return true - } - for _, rp := range reaperList.Items { - if err := remoteClient.Delete(ctx, &rp); err != nil { - key := client.ObjectKey{Namespace: namespace, Name: rp.Name} - if !errors.IsNotFound(err) { - kcLogger.Error(err, "Failed to delete Reaper", "Reaper", key, - "Context", dcTemplate.K8sContext) - hasErrors = true - } - } - } - return -} - func (r *K8ssandraClusterReconciler) setStatusForReaper(kc *api.K8ssandraCluster, reaper *reaperapi.Reaper, dcName string) error { if len(kc.Status.Datacenters) == 0 { kc.Status.Datacenters = make(map[string]api.K8ssandraStatus)