Skip to content

Commit

Permalink
Merge pull request #2348 from 0xff-dev/main
Browse files Browse the repository at this point in the history
🌱 chore: optimize Add/RemoveFinalizer
  • Loading branch information
k8s-ci-robot committed Jun 12, 2023
2 parents 7edfc04 + 8e0001c commit 4419a85
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,18 @@ func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
// It returns an indication of whether it updated the object's list of finalizers.
func RemoveFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
f := o.GetFinalizers()
for i := 0; i < len(f); i++ {
length := len(f)

index := 0
for i := 0; i < length; i++ {
if f[i] == finalizer {
f = append(f[:i], f[i+1:]...)
i--
finalizersUpdated = true
continue
}
f[index] = f[i]
index++
}
o.SetFinalizers(f)
return
o.SetFinalizers(f[:index])
return length != index
}

// ContainsFinalizer checks an Object that the provided finalizer is present.
Expand Down

0 comments on commit 4419a85

Please sign in to comment.