Skip to content

Commit

Permalink
chore: optimize function
Browse files Browse the repository at this point in the history
Signed-off-by: 0xff-dev <stevenshuang521@gmail.com>
  • Loading branch information
0xff-dev committed May 25, 2023
1 parent 30eae58 commit f8154d2
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ type MutateFn func() error

// AddFinalizer accepts an Object and adds the provided finalizer if not present.
// It returns an indication of whether it updated the object's list of finalizers.
func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
func AddFinalizer(o client.Object, finalizer string) bool {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
Expand All @@ -363,17 +363,20 @@ func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {

// RemoveFinalizer accepts an Object and removes the provided finalizer if present.
// It returns an indication of whether it updated the object's list of finalizers.
func RemoveFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
func RemoveFinalizer(o client.Object, finalizer string) 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 f8154d2

Please sign in to comment.