Skip to content

Commit

Permalink
Use controller reference for Reaper deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Jul 14, 2023
1 parent 4d37445 commit fbd57a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 108 deletions.
78 changes: 4 additions & 74 deletions controllers/k8ssandra/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
43 changes: 9 additions & 34 deletions controllers/k8ssandra/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fbd57a4

Please sign in to comment.