-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1248 from spidernet-io/refactor-code
Fix RemoveFinalizers to improve code reusability
- Loading branch information
Showing
5 changed files
with
85 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
func RemoveFinalizers(objMeta *metav1.ObjectMeta, finalizers ...string) { | ||
for _, f := range finalizers { | ||
for i := len(objMeta.Finalizers) - 1; i >= 0; i-- { | ||
if objMeta.Finalizers[i] == f { | ||
objMeta.Finalizers = append(objMeta.Finalizers[:i], objMeta.Finalizers[i+1:]...) | ||
break | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// TestRemoveFinalizers tests the RemoveFinalizers function. | ||
func TestRemoveFinalizers(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
objMeta metav1.ObjectMeta | ||
finalizers []string | ||
want []string | ||
}{ | ||
{ | ||
name: "remove single finalizer", | ||
objMeta: metav1.ObjectMeta{ | ||
Finalizers: []string{"finalize1", "finalize2", "finalize3"}, | ||
}, | ||
finalizers: []string{"finalize2"}, | ||
want: []string{"finalize1", "finalize3"}, | ||
}, | ||
{ | ||
name: "remove multiple finalizers", | ||
objMeta: metav1.ObjectMeta{ | ||
Finalizers: []string{"finalize1", "finalize2", "finalize3", "finalize4"}, | ||
}, | ||
finalizers: []string{"finalize2", "finalize4"}, | ||
want: []string{"finalize1", "finalize3"}, | ||
}, | ||
{ | ||
name: "remove non-existent finalizer", | ||
objMeta: metav1.ObjectMeta{ | ||
Finalizers: []string{"finalize1", "finalize2"}, | ||
}, | ||
finalizers: []string{"finalize3"}, | ||
want: []string{"finalize1", "finalize2"}, | ||
}, | ||
{ | ||
name: "remove all finalizers", | ||
objMeta: metav1.ObjectMeta{ | ||
Finalizers: []string{"finalize1", "finalize2"}, | ||
}, | ||
finalizers: []string{"finalize1", "finalize2"}, | ||
want: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
RemoveFinalizers(&tt.objMeta, tt.finalizers...) | ||
if !reflect.DeepEqual(tt.objMeta.Finalizers, tt.want) { | ||
t.Errorf("RemoveFinalizers() got = %v, want %v", tt.objMeta.Finalizers, tt.want) | ||
} | ||
}) | ||
} | ||
} |