Skip to content

✨ implement helpers for add/remove finalizer #545

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

Merged
merged 1 commit into from
Oct 18, 2019
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
45 changes: 45 additions & 0 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -168,3 +169,47 @@ func mutate(f MutateFn, key client.ObjectKey, obj runtime.Object) error {

// MutateFn is a function which mutates the existing object into it's desired state.
type MutateFn func() error

// AddFinalizer accepts a metav1 object and adds the provided finalizer if not present.
func AddFinalizer(o metav1.Object, finalizer string) {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
return
}
}
o.SetFinalizers(append(f, finalizer))
}

// AddFinalizerWithError tries to convert a runtime object to a metav1 object and add the provided finalizer.
// It returns an error if the provided object cannot provide an accessor.
func AddFinalizerWithError(o runtime.Object, finalizer string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this name is not great. Perhaps AddFinalizerIfPossible?

m, err := meta.Accessor(o)
if err != nil {
return err
}
AddFinalizer(m, finalizer)
return nil
}

// RemoveFinalizer accepts a metav1 object and removes the provided finalizer if present.
func RemoveFinalizer(o metav1.Object, finalizer string) {
f := o.GetFinalizers()
for i, e := range f {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This is nicer with k8s.io/apimachinery/pkg/util/sets.String

Copy link
Contributor Author

@alexeldeib alexeldeib Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found this today coincidentally. can try swapping it out 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better?

if e == finalizer {
f = append(f[:i], f[i+1:]...)
}
}
o.SetFinalizers(f)
}

// RemoveFinalizerWithError tries to convert a runtime object to a metav1 object and remove the provided finalizer.
// It returns an error if the provided object cannot provide an accessor.
func RemoveFinalizerWithError(o runtime.Object, finalizer string) error {
m, err := meta.Accessor(o)
if err != nil {
return err
}
RemoveFinalizer(m, finalizer)
return nil
}
53 changes: 53 additions & 0 deletions pkg/controller/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,63 @@ var _ = Describe("Controllerutil", func() {
Expect(err).To(HaveOccurred())
})
})

Describe("Finalizers", func() {
var obj runtime.Object = &errRuntimeObj{}
var deploy *appsv1.Deployment

Describe("AddFinalizerWithError", func() {
It("should return an error if object can't provide accessor", func() {
Expect(controllerutil.AddFinalizerWithError(obj, testFinalizer)).To(HaveOccurred())
})
})

Describe("RemoveFinalizerWithError", func() {
It("should return an error if object can't provide accessor", func() {
Expect(controllerutil.RemoveFinalizerWithError(obj, testFinalizer)).To(HaveOccurred())
})
})

Describe("AddFinalizer", func() {
deploy = &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{},
},
}

It("should add the finalizer when not present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})

It("should not add the finalizer when already present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})
})

Describe("RemoveFinalizer", func() {
It("should remove finalizer if present", func() {
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})
})
})

const testFinalizer = "foo.bar.baz"

var _ runtime.Object = &errRuntimeObj{}
var _ metav1.Object = &errMetaObj{}

type errRuntimeObj struct {
runtime.TypeMeta
}

func (o *errRuntimeObj) DeepCopyObject() runtime.Object {
return &errRuntimeObj{}
}

type errMetaObj struct {
metav1.ObjectMeta
}
Expand Down