Skip to content

Commit

Permalink
Merge pull request #917 from dmvolod/fix-remove-finalizer
Browse files Browse the repository at this point in the history
🐛 controllerutil.RemoveFinalizer can occurs slice bounds out of range exception
  • Loading branch information
k8s-ci-robot committed Apr 24, 2020
2 parents a457e27 + 01dbf9d commit 670b77d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ func AddFinalizerWithError(o runtime.Object, finalizer string) error {
// 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 {
if e == finalizer {
for i := 0; i < len(f); i++ {
if f[i] == finalizer {
f = append(f[:i], f[i+1:]...)
i--
}
}
o.SetFinalizers(f)
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ var _ = Describe("Controllerutil", func() {
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})

It("should remove all equal finalizers if present", func() {
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})
})
})
Expand Down

0 comments on commit 670b77d

Please sign in to comment.