Skip to content

Commit

Permalink
refactor: Use Patch instead of Update to set/remove finalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Dec 20, 2024
1 parent fba3d9d commit 1fa382c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
15 changes: 15 additions & 0 deletions controllers/controller_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -366,3 +367,17 @@ func getReferencedValue(ctx context.Context, cl client.Client, cr metav1.ObjectM
}
}
}

// Add finalizer through a MergePatch
// Avoids updating the entire object and only changes the finalizers
func addFinalizer(ctx context.Context, cl client.Client, cr client.Object) error {
patch := []byte(`{"metadata":{"finalizers":["` + grafanaFinalizer + `"]}}`)
return cl.Patch(ctx, cr, client.RawPatch(types.MergePatchType, patch))
}

// Remove finalizer through a MergePatch
// Avoids updating the entire object and only changes the finalizers
func removeFinalizer(ctx context.Context, cl client.Client, cr client.Object) error {
patch := []byte(`{"metadata":{"finalizers":null}}`)
return cl.Patch(ctx, cr, client.RawPatch(types.MergePatchType, patch))
}
14 changes: 7 additions & 7 deletions controllers/grafanaalertrulegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func (r *GrafanaAlertRuleGroupReconciler) Reconcile(ctx context.Context, req ctr
if err != nil {
return ctrl.Result{}, fmt.Errorf("cleaning up alert rule group: %w", err)
}
controllerutil.RemoveFinalizer(group, grafanaFinalizer)
if err := r.Update(ctx, group); err != nil {
if err := removeFinalizer(ctx, r.Client, group); err != nil {
r.Log.Error(err, "failed to remove finalizer")
return ctrl.Result{}, err
}
Expand All @@ -96,12 +95,13 @@ func (r *GrafanaAlertRuleGroupReconciler) Reconcile(ctx context.Context, req ctr
r.Log.Error(err, "updating status")
}
if meta.IsStatusConditionTrue(group.Status.Conditions, conditionNoMatchingInstance) {
controllerutil.RemoveFinalizer(group, grafanaFinalizer)
if err := removeFinalizer(ctx, r.Client, group); err != nil {
r.Log.Error(err, "failed to remove finalizer")
}
} else {
controllerutil.AddFinalizer(group, grafanaFinalizer)
}
if err := r.Update(ctx, group); err != nil {
r.Log.Error(err, "failed to set finalizer")
if err := addFinalizer(ctx, r.Client, group); err != nil {
r.Log.Error(err, "failed to set finalizer")
}
}
}()

Expand Down
11 changes: 6 additions & 5 deletions controllers/grafanacontactpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ func (r *GrafanaContactPointReconciler) Reconcile(ctx context.Context, req ctrl.
r.Log.Error(err, "updating status")
}
if meta.IsStatusConditionTrue(contactPoint.Status.Conditions, conditionNoMatchingInstance) {
controllerutil.RemoveFinalizer(contactPoint, grafanaFinalizer)
if err := removeFinalizer(ctx, r.Client, contactPoint); err != nil {
r.Log.Error(err, "failed to remove finalizer")
}
} else {
controllerutil.AddFinalizer(contactPoint, grafanaFinalizer)
}
if err := r.Update(ctx, contactPoint); err != nil {
r.Log.Error(err, "failed to set finalizer")
if err := addFinalizer(ctx, r.Client, contactPoint); err != nil {
r.Log.Error(err, "failed to set finalizer")
}
}
}()

Expand Down
11 changes: 6 additions & 5 deletions controllers/notificationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ func (r *GrafanaNotificationPolicyReconciler) Reconcile(ctx context.Context, req
r.Log.Error(err, "updating status")
}
if meta.IsStatusConditionTrue(notificationPolicy.Status.Conditions, conditionNoMatchingInstance) {
controllerutil.RemoveFinalizer(notificationPolicy, grafanaFinalizer)
if err := removeFinalizer(ctx, r.Client, notificationPolicy); err != nil {
r.Log.Error(err, "failed to remove finalizer")
}
} else {
controllerutil.AddFinalizer(notificationPolicy, grafanaFinalizer)
}
if err := r.Update(ctx, notificationPolicy); err != nil {
r.Log.Error(err, "failed to set finalizer")
if err := addFinalizer(ctx, r.Client, notificationPolicy); err != nil {
r.Log.Error(err, "failed to set finalizer")
}
}
}()

Expand Down

0 comments on commit 1fa382c

Please sign in to comment.