Skip to content

Commit 385295c

Browse files
committed
fixup! Use Patch instead of Update for finalizer operations
Signed-off-by: Todd Short <[email protected]>
1 parent 68b3412 commit 385295c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

internal/shared/util/finalizer/finalizer.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// If no finalizers are supplied, all FinalizerPrefix finalizers will be removed from the object.
3737
// If any finalizers are supplied, all other FinalizerPrefix finalizers will be removed and only the supplied ones will remain.
3838
// Returns (true, nil) if the finalizers were changed, (false, nil) if they were already set to the desired value.
39-
// Note: This function will update the passed object with the server response.
39+
// Note: This function will update ONLY the finalizers field of the passed object, not other metadata fields.
4040
func EnsureFinalizers(ctx context.Context, owner string, c client.Client, obj client.Object, finalizers ...string) (bool, error) {
4141
newFinalizers := slices.Clone(finalizers)
4242
if newFinalizers == nil {
@@ -83,10 +83,19 @@ func EnsureFinalizers(ctx context.Context, owner string, c client.Client, obj cl
8383
return false, fmt.Errorf("marshalling patch to ensure finalizers: %w", err)
8484
}
8585

86-
// Use patch to update finalizers
87-
if err := c.Patch(ctx, obj, client.RawPatch(types.MergePatchType, patchJSON)); err != nil {
86+
// Create a copy to use for patching. We patch the copy to avoid having the server's
87+
// patch response overwrite the original object with potentially changed metadata fields
88+
// (like annotations) that the caller didn't intend to modify.
89+
objCopy := obj.DeepCopyObject().(client.Object)
90+
91+
// Use patch to update finalizers on the server
92+
if err := c.Patch(ctx, objCopy, client.RawPatch(types.MergePatchType, patchJSON)); err != nil {
8893
return false, fmt.Errorf("updating finalizers: %w", err)
8994
}
9095

96+
// Update only the finalizers in the original object to reflect the change,
97+
// without copying other metadata that may have changed on the server.
98+
obj.SetFinalizers(objCopy.GetFinalizers())
99+
91100
return true, nil
92101
}

0 commit comments

Comments
 (0)