Skip to content

Commit

Permalink
uniform finalizer (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
eytan-avisror authored Jul 30, 2020
1 parent ff2721a commit bf02c19
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
11 changes: 0 additions & 11 deletions controllers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "[]")
Expand Down
28 changes: 14 additions & 14 deletions controllers/instancegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package controllers

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bf02c19

Please sign in to comment.