Skip to content

Commit

Permalink
resource: use Patch in APIFinalizer
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Sep 18, 2023
1 parent 2ad600c commit 4af9c39
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions pkg/resource/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,19 @@ func (a *APIFinalizer) AddFinalizer(ctx context.Context, obj Object) error {
if meta.FinalizerExists(obj, a.finalizer) {
return nil
}
orig := obj.DeepCopyObject().(Object)
meta.AddFinalizer(obj, a.finalizer)
return errors.Wrap(a.client.Update(ctx, obj), errUpdateObject)
return errors.Wrap(a.client.Patch(ctx, obj, client.MergeFromWithOptions(orig, client.MergeFromWithOptimisticLock{})), errUpdateObject)
}

// RemoveFinalizer from the supplied Managed resource.
func (a *APIFinalizer) RemoveFinalizer(ctx context.Context, obj Object) error {
if !meta.FinalizerExists(obj, a.finalizer) {
return nil
}
orig := obj.DeepCopyObject().(Object)
meta.RemoveFinalizer(obj, a.finalizer)
return errors.Wrap(IgnoreNotFound(a.client.Update(ctx, obj)), errUpdateObject)
return errors.Wrap(IgnoreNotFound(a.client.Patch(ctx, obj, client.MergeFromWithOptions(orig, client.MergeFromWithOptimisticLock{}))), errUpdateObject)
}

// A FinalizerFns satisfy the Finalizer interface.
Expand Down
8 changes: 4 additions & 4 deletions pkg/resource/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestManagedRemoveFinalizer(t *testing.T) {
want want
}{
"UpdateError": {
client: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)},
client: &test.MockClient{MockPatch: test.NewMockPatchFn(errBoom)},
args: args{
ctx: context.Background(),
obj: &fake.Object{ObjectMeta: metav1.ObjectMeta{Finalizers: []string{finalizer}}},
Expand All @@ -313,7 +313,7 @@ func TestManagedRemoveFinalizer(t *testing.T) {
},
},
"Successful": {
client: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)},
client: &test.MockClient{MockPatch: test.NewMockPatchFn(nil)},
args: args{
ctx: context.Background(),
obj: &fake.Object{ObjectMeta: metav1.ObjectMeta{Finalizers: []string{finalizer}}},
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestAPIFinalizerAdder(t *testing.T) {
want want
}{
"UpdateError": {
client: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)},
client: &test.MockClient{MockPatch: test.NewMockPatchFn(errBoom)},
args: args{
ctx: context.Background(),
obj: &fake.Object{ObjectMeta: metav1.ObjectMeta{Finalizers: []string{}}},
Expand All @@ -371,7 +371,7 @@ func TestAPIFinalizerAdder(t *testing.T) {
},
},
"Successful": {
client: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)},
client: &test.MockClient{MockPatch: test.NewMockPatchFn(nil)},
args: args{
ctx: context.Background(),
obj: &fake.Object{ObjectMeta: metav1.ObjectMeta{Finalizers: []string{}}},
Expand Down

0 comments on commit 4af9c39

Please sign in to comment.