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

uniform finalizer #153

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
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
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