From 071dda5e977b85b62990451c389e1645559cc660 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Thu, 8 Sep 2022 21:02:47 +0100 Subject: [PATCH] Add finalizers to all the CRDs Signed-off-by: Somtochi Onyekwere --- api/v1beta1/condition_types.go | 2 ++ controllers/alert_controller.go | 13 +++++++++++++ controllers/provider_controller.go | 13 +++++++++++++ controllers/receiver_controller.go | 13 +++++++++++++ 4 files changed, 41 insertions(+) diff --git a/api/v1beta1/condition_types.go b/api/v1beta1/condition_types.go index 2e7996c2f..0012fef05 100644 --- a/api/v1beta1/condition_types.go +++ b/api/v1beta1/condition_types.go @@ -16,6 +16,8 @@ limitations under the License. package v1beta1 +const NotificationFinalizer = "finalizers.fluxcd.io" + const ( // InitializedReason represents the fact that a given resource has been initialized. InitializedReason string = "Initialized" diff --git a/controllers/alert_controller.go b/controllers/alert_controller.go index 7667b4e40..73be26647 100644 --- a/controllers/alert_controller.go +++ b/controllers/alert_controller.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/ratelimiter" @@ -154,6 +155,18 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu r.Metrics.RecordDuration(ctx, alert, start) }() + if !controllerutil.ContainsFinalizer(alert, v1beta1.NotificationFinalizer) { + controllerutil.AddFinalizer(alert, v1beta1.NotificationFinalizer) + result = ctrl.Result{Requeue: true} + return + } + + if !alert.ObjectMeta.DeletionTimestamp.IsZero() { + controllerutil.RemoveFinalizer(alert, v1beta1.NotificationFinalizer) + result = ctrl.Result{} + return + } + return r.reconcile(ctx, alert) } diff --git a/controllers/provider_controller.go b/controllers/provider_controller.go index c51e74ba0..f6c6c28fa 100644 --- a/controllers/provider_controller.go +++ b/controllers/provider_controller.go @@ -30,6 +30,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/ratelimiter" "sigs.k8s.io/yaml" @@ -133,6 +134,18 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r }() + if !controllerutil.ContainsFinalizer(provider, v1beta1.NotificationFinalizer) { + controllerutil.AddFinalizer(provider, v1beta1.NotificationFinalizer) + result = ctrl.Result{Requeue: true} + return + } + + if !provider.ObjectMeta.DeletionTimestamp.IsZero() { + controllerutil.RemoveFinalizer(provider, v1beta1.NotificationFinalizer) + result = ctrl.Result{} + return + } + return r.reconcile(ctx, provider) } diff --git a/controllers/receiver_controller.go b/controllers/receiver_controller.go index 9e639664a..a8b851915 100644 --- a/controllers/receiver_controller.go +++ b/controllers/receiver_controller.go @@ -30,6 +30,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/ratelimiter" "github.com/fluxcd/pkg/apis/meta" @@ -128,6 +129,18 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r }() + if !controllerutil.ContainsFinalizer(receiver, v1beta1.NotificationFinalizer) { + controllerutil.AddFinalizer(receiver, v1beta1.NotificationFinalizer) + result = ctrl.Result{Requeue: true} + return + } + + if !receiver.ObjectMeta.DeletionTimestamp.IsZero() { + controllerutil.RemoveFinalizer(receiver, v1beta1.NotificationFinalizer) + result = ctrl.Result{} + return + } + return r.reconcile(ctx, receiver) }