From f1c20fda5bf8b2b5794e02f58b84cf1ac51d195f Mon Sep 17 00:00:00 2001 From: Eytan Avisror Date: Wed, 29 Jul 2020 13:34:05 -0700 Subject: [PATCH] uniform finalizer --- controllers/common/utils.go | 11 ---------- controllers/instancegroup_controller.go | 28 ++++++++++++------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/controllers/common/utils.go b/controllers/common/utils.go index 91f86da7..9c8330e4 100644 --- a/controllers/common/utils.go +++ b/controllers/common/utils.go @@ -105,17 +105,6 @@ func GetLastElementBy(s, sep string) string { return sp[len(sp)-1] } -// RemoveString removes a string 's' from slice 'slice' -func RemoveString(slice []string, s string) (result []string) { - for _, item := range slice { - if item == s { - continue - } - result = append(result, item) - } - return -} - // ConcatenateList joins lists to strings delimited with `delimiter` func ConcatenateList(list []string, delimiter string) string { return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(list)), delimiter), "[]") diff --git a/controllers/instancegroup_controller.go b/controllers/instancegroup_controller.go index a2f3b925..b12ac256 100644 --- a/controllers/instancegroup_controller.go +++ b/controllers/instancegroup_controller.go @@ -17,7 +17,6 @@ package controllers import ( "context" - "fmt" "strings" "time" @@ -54,31 +53,33 @@ type InstanceGroupAuthenticator struct { Kubernetes kubeprovider.KubernetesClientSet } -func (r *InstanceGroupReconciler) Finalize(instanceGroup *v1alpha1.InstanceGroup, finalizerName string) { +const ( + FinalizerStr = "finalizer.instancegroups.keikoproj.io" +) + +func (r *InstanceGroupReconciler) Finalize(instanceGroup *v1alpha1.InstanceGroup) { // Resource is being deleted meta := &instanceGroup.ObjectMeta deletionTimestamp := meta.GetDeletionTimestamp() if !deletionTimestamp.IsZero() { // And state is "Deleted" if instanceGroup.GetState() == v1alpha1.ReconcileDeleted { - // Unset Finalizer if present - if common.ContainsString(meta.GetFinalizers(), finalizerName) { - meta.SetFinalizers(common.RemoveString(instanceGroup.ObjectMeta.Finalizers, finalizerName)) - if err := r.Update(context.Background(), instanceGroup); err != nil { - r.Log.Error(err, "failed to update custom resource") - } + // remove all finalizers + meta.SetFinalizers([]string{}) + if err := r.Update(context.Background(), instanceGroup); err != nil { + r.Log.Error(err, "failed to update custom resource") } } } } -func (r *InstanceGroupReconciler) SetFinalizer(instanceGroup *v1alpha1.InstanceGroup, finalizerName string) { +func (r *InstanceGroupReconciler) SetFinalizer(instanceGroup *v1alpha1.InstanceGroup) { // Resource is not being deleted if instanceGroup.ObjectMeta.DeletionTimestamp.IsZero() { // And does not contain finalizer - if !common.ContainsString(instanceGroup.ObjectMeta.Finalizers, finalizerName) { + if !common.ContainsString(instanceGroup.ObjectMeta.Finalizers, FinalizerStr) { // Set Finalizer - instanceGroup.ObjectMeta.Finalizers = append(instanceGroup.ObjectMeta.Finalizers, finalizerName) + instanceGroup.ObjectMeta.Finalizers = append(instanceGroup.ObjectMeta.Finalizers, FinalizerStr) if err := r.Update(context.Background(), instanceGroup); err != nil { r.Log.Error(err, "failed to update custom resource") } @@ -109,9 +110,8 @@ func (r *InstanceGroupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro } // set/unset finalizer - finalizerName := fmt.Sprintf("finalizers.%v.instancegroups.keikoproj.io", instanceGroup.Spec.Provisioner) - r.SetFinalizer(instanceGroup, finalizerName) - defer r.Finalize(instanceGroup, finalizerName) + r.SetFinalizer(instanceGroup) + defer r.Finalize(instanceGroup) var defaultConfig *provisioners.DefaultConfiguration if defaultConfig, err = provisioners.UnmarshalConfiguration(r.ConfigMap); err != nil {