Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Migrate controllerutil AddFinalizer and RemoveFinalizer to controllerutil.Object and deprecate *WithError functions #962

Merged
merged 1 commit into from
May 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ func mutate(f MutateFn, key client.ObjectKey, obj runtime.Object) error {
// MutateFn is a function which mutates the existing object into it's desired state.
type MutateFn func() error

// AddFinalizer accepts a metav1 object and adds the provided finalizer if not present.
func AddFinalizer(o metav1.Object, finalizer string) {
// AddFinalizer accepts an Object and adds the provided finalizer if not present.
func AddFinalizer(o Object, finalizer string) {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
Expand All @@ -250,17 +250,19 @@ func AddFinalizer(o metav1.Object, finalizer string) {

// AddFinalizerWithError tries to convert a runtime object to a metav1 object and add the provided finalizer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @dmvolod I think it would be better for now to leave the WithError funcs and just add a comment that they are deprecated and shouldn't be used. We can remove them when we also merge other braking changes. AFAIK right now we do not have any breaking changes planned.

/ok-to-test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for feedback, @alvaroaleman
I've changed just common functions and marked *WithError as deprecated

// It returns an error if the provided object cannot provide an accessor.
//
// Deprecated: Use AddFinalizer instead. Check is performing on compile time.
func AddFinalizerWithError(o runtime.Object, finalizer string) error {
m, err := meta.Accessor(o)
if err != nil {
return err
}
AddFinalizer(m, finalizer)
AddFinalizer(m.(Object), finalizer)
return nil
}

// RemoveFinalizer accepts a metav1 object and removes the provided finalizer if present.
func RemoveFinalizer(o metav1.Object, finalizer string) {
// RemoveFinalizer accepts an Object and removes the provided finalizer if present.
func RemoveFinalizer(o Object, finalizer string) {
f := o.GetFinalizers()
for i := 0; i < len(f); i++ {
if f[i] == finalizer {
Expand All @@ -273,16 +275,18 @@ func RemoveFinalizer(o metav1.Object, finalizer string) {

// RemoveFinalizerWithError tries to convert a runtime object to a metav1 object and remove the provided finalizer.
// It returns an error if the provided object cannot provide an accessor.
//
// Deprecated: Use RemoveFinalizer instead. Check is performing on compile time.
func RemoveFinalizerWithError(o runtime.Object, finalizer string) error {
m, err := meta.Accessor(o)
if err != nil {
return err
}
RemoveFinalizer(m, finalizer)
RemoveFinalizer(m.(Object), finalizer)
return nil
}

// ContainsFinalizer checks a metav1 object that the provided finalizer is present.
// ContainsFinalizer checks an Object that the provided finalizer is present.
func ContainsFinalizer(o Object, finalizer string) bool {
f := o.GetFinalizers()
for _, e := range f {
Expand Down