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

Use patches for adding and removing finalizers #118

Merged
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
34 changes: 22 additions & 12 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,11 +704,13 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi

if cleanupNow {
if objHasFinalizer(&plc, pruneObjectFinalizer) {
plc.SetFinalizers(removeObjFinalizer(&plc, pruneObjectFinalizer))
patch := removeObjFinalizerPatch(&plc, pruneObjectFinalizer)

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

return
}
}

Expand All @@ -717,11 +719,13 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi

// set finalizer if it hasn't been set
if !objHasFinalizer(&plc, pruneObjectFinalizer) {
plc.SetFinalizers(addObjFinalizer(&plc, pruneObjectFinalizer))
mergePatch := []byte(`{"metadata": {"finalizers": ["` + pruneObjectFinalizer + `"]}}`)

err := r.Update(context.TODO(), &plc)
err := r.Patch(context.TODO(), &plc, client.RawPatch(types.MergePatchType, mergePatch))
Comment on lines +722 to +724
Copy link
Member

Choose a reason for hiding this comment

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

@mprahl could this accidentally remove any other finalizer on the resource? I was expecting a JSON patch like [{"op":"add","path":"/metadata/finalizers/-","value":"..."}]

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I thought the merge patch would act like strategic merge but I got the two confused. Here is a PR with the fix:
#119

if err != nil {
log.V(1).Error(err, "Error setting finalizer for configuration policy", plc)
log.V(1).Error(err, "Error setting finalizer for configuration policy")

return
}
}

Expand All @@ -733,11 +737,14 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi

if len(failures) == 0 {
log.V(1).Info("Objects have been successfully cleaned up, removing finalizer")
plc.SetFinalizers(removeObjFinalizer(&plc, pruneObjectFinalizer))

err := r.Update(context.TODO(), &plc)
patch := removeObjFinalizerPatch(&plc, pruneObjectFinalizer)

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

return
}
} else {
log.V(1).Info("Object cleanup failed, some objects have not been deleted from the cluster")
Expand Down Expand Up @@ -767,10 +774,13 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi
}
} else if objHasFinalizer(&plc, pruneObjectFinalizer) {
// if pruneObjectBehavior is none, no finalizer is needed
plc.SetFinalizers(removeObjFinalizer(&plc, pruneObjectFinalizer))
err := r.Update(context.TODO(), &plc)
patch := removeObjFinalizerPatch(&plc, pruneObjectFinalizer)

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

return
}
}

Expand Down
21 changes: 6 additions & 15 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"

gocmp "github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -562,24 +563,14 @@ func objHasFinalizer(obj metav1.Object, finalizer string) bool {
return false
}

func addObjFinalizer(obj metav1.Object, finalizer string) []string {
if objHasFinalizer(obj, finalizer) {
return obj.GetFinalizers()
}

return append(obj.GetFinalizers(), finalizer)
}

func removeObjFinalizer(obj metav1.Object, finalizer string) []string {
result := []string{}

for _, existingFinalizer := range obj.GetFinalizers() {
if existingFinalizer != finalizer {
result = append(result, existingFinalizer)
func removeObjFinalizerPatch(obj metav1.Object, finalizer string) []byte {
for i, existingFinalizer := range obj.GetFinalizers() {
if existingFinalizer == finalizer {
return []byte(`[{"op":"remove","path":"/metadata/finalizers/` + strconv.FormatInt(int64(i), 10) + `"}]`)
}
}

return result
return nil
}

func containRelated(arr []policyv1.RelatedObject, input policyv1.RelatedObject) bool {
Expand Down