From 4761631fbf37c7679cae559896595dd552f87c21 Mon Sep 17 00:00:00 2001 From: hatfieldbrian Date: Thu, 19 Aug 2021 13:50:20 -0700 Subject: [PATCH] return a bool from AddFinalizer and RemoveFinalizer Signed-off-by: hatfieldbrian --- pkg/config/v1alpha1/zz_generated.deepcopy.go | 1 - .../controllerutil/controllerutil.go | 15 ++++- .../controllerutil/controllerutil_test.go | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/pkg/config/v1alpha1/zz_generated.deepcopy.go b/pkg/config/v1alpha1/zz_generated.deepcopy.go index 5329bef667..752fa9754c 100644 --- a/pkg/config/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/config/v1alpha1/zz_generated.deepcopy.go @@ -1,4 +1,3 @@ -//go:build !ignore_autogenerated // +build !ignore_autogenerated // Code generated by controller-gen. DO NOT EDIT. diff --git a/pkg/controller/controllerutil/controllerutil.go b/pkg/controller/controllerutil/controllerutil.go index 13f14a7ed3..8925d62c1f 100644 --- a/pkg/controller/controllerutil/controllerutil.go +++ b/pkg/controller/controllerutil/controllerutil.go @@ -350,25 +350,38 @@ type MutateFn func() error // AddFinalizer accepts an Object and adds the provided finalizer if not present. func AddFinalizer(o client.Object, finalizer string) { + AddFinalizerV2(o, finalizer) +} + +// AddFinalizerV2 accepts an Object and adds the provided finalizer and returns true if not present. +func AddFinalizerV2(o client.Object, finalizer string) (finalizersUpdated bool) { f := o.GetFinalizers() for _, e := range f { if e == finalizer { - return + return false } } o.SetFinalizers(append(f, finalizer)) + return true } // RemoveFinalizer accepts an Object and removes the provided finalizer if present. func RemoveFinalizer(o client.Object, finalizer string) { + RemoveFinalizerV2(o, finalizer) +} + +// RemoveFinalizerV2 accepts an Object and removes the provided finalizer and returns true if present. +func RemoveFinalizerV2(o client.Object, finalizer string) (finalizersUpdated bool) { f := o.GetFinalizers() for i := 0; i < len(f); i++ { if f[i] == finalizer { f = append(f[:i], f[i+1:]...) i-- + finalizersUpdated = true } } o.SetFinalizers(f) + return } // ContainsFinalizer checks an Object that the provided finalizer is present. diff --git a/pkg/controller/controllerutil/controllerutil_test.go b/pkg/controller/controllerutil/controllerutil_test.go index d47c691465..a428244686 100644 --- a/pkg/controller/controllerutil/controllerutil_test.go +++ b/pkg/controller/controllerutil/controllerutil_test.go @@ -700,6 +700,62 @@ var _ = Describe("Controllerutil", func() { }) }) + Describe("AddFinalizerV2, which returns an indication of whether it modified the input object's finalizers list,", func() { + deploy = &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Finalizers: []string{}, + }, + } + + When("the input object's finalizers list has no instances of the input finalizer", func() { + It("should return true", func() { + Expect(controllerutil.AddFinalizerV2(deploy, testFinalizer)).To(BeTrue()) + }) + It("should add the input finalizer to the input object's finalizers list", func() { + Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer})) + }) + }) + + When("the input object's finalizers list has an instance of the input finalizer", func() { + It("should return false", func() { + Expect(controllerutil.AddFinalizerV2(deploy, testFinalizer)).To(BeFalse()) + }) + It("should not modify the input object's finalizers list", func() { + Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer})) + }) + }) + }) + + Describe("RemoveFinalizerV2, which returns an indication of whether it modified the input object's finalizers list,", func() { + When("the input object's finalizers list has no instances of the input finalizer", func() { + It("should return false", func() { + Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer1)).To(BeFalse()) + }) + It("should not modify the input object's finalizers list", func() { + Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer})) + }) + }) + + When("the input object's finalizers list has one instance of the input finalizer", func() { + It("should return true", func() { + Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer)).To(BeTrue()) + }) + It("should remove the instance of the input finalizer from the input object's finalizers list", func() { + Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{})) + }) + }) + + When("the input object's finalizers list has multiple instances of the input finalizer", func() { + It("should return true", func() { + deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer)) + Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer)).To(BeTrue()) + }) + It("should remove each instance of the input finalizer from the input object's finalizers list", func() { + Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{})) + }) + }) + }) + Describe("ContainsFinalizer", func() { It("should check that finalizer is present", func() { controllerutil.AddFinalizer(deploy, testFinalizer) @@ -715,6 +771,7 @@ var _ = Describe("Controllerutil", func() { }) const testFinalizer = "foo.bar.baz" +const testFinalizer1 = testFinalizer + "1" var _ runtime.Object = &errRuntimeObj{} var _ metav1.Object = &errMetaObj{}