Skip to content

Commit

Permalink
Account for existing finalizers when adding the prune object finalizer
Browse files Browse the repository at this point in the history
A merge patch will overwrite the existing value.

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
(cherry picked from commit 7248a00)
  • Loading branch information
mprahl authored and magic-mirror-bot[bot] committed Apr 7, 2023
1 parent 9b996d5 commit a728253
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,18 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi

// set finalizer if it hasn't been set
if !objHasFinalizer(&plc, pruneObjectFinalizer) {
mergePatch := []byte(`{"metadata": {"finalizers": ["` + pruneObjectFinalizer + `"]}}`)
var patch []byte
if plc.Finalizers == nil {
patch = []byte(
`[{"op":"add","path":"/metadata/finalizers","value":["` + pruneObjectFinalizer + `"]}]`,
)
} else {
patch = []byte(
`[{"op":"add","path":"/metadata/finalizers/-","value":"` + pruneObjectFinalizer + `"}]`,
)
}

err := r.Patch(context.TODO(), &plc, client.RawPatch(types.MergePatchType, mergePatch))
err := r.Patch(context.TODO(), &plc, client.RawPatch(types.JSONPatchType, patch))
if err != nil {
log.V(1).Error(err, "Error setting finalizer for configuration policy")

Expand Down

0 comments on commit a728253

Please sign in to comment.