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

controllers: use controllerutils finalizer functions #16

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions controllers/lvmcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -99,14 +100,14 @@ func (r *LVMClusterReconciler) reconcile(ctx context.Context, instance *lvmv1alp

//The resource was deleted
if !instance.DeletionTimestamp.IsZero() {
if contains(instance.GetFinalizers(), lvmClusterFinalizer) {
if controllerutil.ContainsFinalizer(instance, lvmClusterFinalizer) {
for _, unit := range resourceList {
err := unit.ensureDeleted(r, ctx, instance)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed cleaning up: %s %w", unit.getName(), err)
}
}
instance.ObjectMeta.Finalizers = remove(instance.ObjectMeta.Finalizers, lvmClusterFinalizer)
controllerutil.RemoveFinalizer(instance, lvmClusterFinalizer)
if err := r.Client.Update(context.TODO(), instance); err != nil {
r.Log.Info("failed to remove finalizer from LvmCluster", "LvmCluster", instance.Name)
return reconcile.Result{}, err
Expand All @@ -115,9 +116,9 @@ func (r *LVMClusterReconciler) reconcile(ctx context.Context, instance *lvmv1alp
return ctrl.Result{}, nil
}

if !contains(instance.GetFinalizers(), lvmClusterFinalizer) {
if !controllerutil.ContainsFinalizer(instance, lvmClusterFinalizer) {
r.Log.Info("Finalizer not found for LvmCluster. Adding finalizer.", "LvmCluster", instance.Name)
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, lvmClusterFinalizer)
controllerutil.AddFinalizer(instance, lvmClusterFinalizer)
if err := r.Client.Update(context.TODO(), instance); err != nil {
r.Log.Info("failed to update LvmCluster with finalizer.", "LvmCluster", instance.Name)
return reconcile.Result{}, err
Expand Down Expand Up @@ -171,24 +172,3 @@ type resourceManager interface {
// status that changes only when the operands change.
updateStatus(*LVMClusterReconciler, context.Context, *lvmv1alpha1.LVMCluster) error
}

// Checks whether a string is contained within a slice
func contains(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}

// Removes a given string from a slice and returns the new slice
func remove(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}