Skip to content

feat: use patch for finalizer updates #252

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions controller/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func (l *LifecycleManager) addFinalizersIfNeeded(ctx context.Context, instance R
}

update := false
original := instance.DeepCopyObject().(client.Object)
for _, subroutine := range l.subroutines {
if len(subroutine.Finalizers()) > 0 {
needsUpdate := l.addFinalizerIfNeeded(instance, subroutine)
Expand All @@ -375,7 +376,8 @@ func (l *LifecycleManager) addFinalizersIfNeeded(ctx context.Context, instance R
}
}
if update {
err := l.client.Update(ctx, instance)
patch := client.MergeFrom(original)
err := l.client.Patch(ctx, instance, patch)
if err != nil {
return err
}
Expand All @@ -401,14 +403,16 @@ func (l *LifecycleManager) removeFinalizerIfNeeded(ctx context.Context, instance

if !result.Requeue && result.RequeueAfter == 0 {
update := false
original := instance.DeepCopyObject().(client.Object)
for _, f := range subroutine.Finalizers() {
needsUpdate := controllerutil.RemoveFinalizer(instance, f)
if needsUpdate {
update = true
}
}
if update {
err := l.client.Update(ctx, instance)
patch := client.MergeFrom(original)
err := l.client.Patch(ctx, instance, patch)
if err != nil {
return errors.NewOperatorError(errors.Wrap(err, "failed to update instance"), true, false)
}
Expand Down
1 change: 1 addition & 0 deletions controller/testSupport/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func CreateFakeClient(t *testing.T, objects ...client.Object) client.WithWatch {
builder := fake.NewClientBuilder()
s := runtime.NewScheme()
sBuilder := scheme.Builder{GroupVersion: schema.GroupVersion{Group: "test.openmfp.io", Version: "v1alpha1"}}
sBuilder.Register(&TestApiObject{})
for _, obj := range objects {
sBuilder.Register(obj)
builder.WithStatusSubresource(obj)
Expand Down
Loading