From c81f29858be2a52a05da1cab9d02c4615f147e82 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 24 Jun 2022 11:33:17 +0300 Subject: [PATCH] Add finalizer to ImageUpdateAutomation resources Signed-off-by: Stefan Prodan --- api/v1beta1/imageupdateautomation_types.go | 5 +++- .../imageupdateautomation_controller.go | 24 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/api/v1beta1/imageupdateautomation_types.go b/api/v1beta1/imageupdateautomation_types.go index 2e10a974..3c1c67af 100644 --- a/api/v1beta1/imageupdateautomation_types.go +++ b/api/v1beta1/imageupdateautomation_types.go @@ -25,7 +25,10 @@ import ( "github.com/fluxcd/pkg/apis/meta" ) -const ImageUpdateAutomationKind = "ImageUpdateAutomation" +const ( + ImageUpdateAutomationKind = "ImageUpdateAutomation" + ImageUpdateAutomationFinalizer = "finalizers.fluxcd.io" +) // ImageUpdateAutomationSpec defines the desired state of ImageUpdateAutomation type ImageUpdateAutomationSpec struct { diff --git a/controllers/imageupdateautomation_controller.go b/controllers/imageupdateautomation_controller.go index 3399107d..8c7c03b3 100644 --- a/controllers/imageupdateautomation_controller.go +++ b/controllers/imageupdateautomation_controller.go @@ -29,12 +29,11 @@ import ( "time" "github.com/Masterminds/sprig/v3" - libgit2 "github.com/libgit2/git2go/v33" - "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" securejoin "github.com/cyphar/filepath-securejoin" "github.com/go-logr/logr" + libgit2 "github.com/libgit2/git2go/v33" corev1 "k8s.io/api/core/v1" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -46,6 +45,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" @@ -115,6 +115,26 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr return ctrl.Result{}, client.IgnoreNotFound(err) } + // Add our finalizer if it does not exist. + if !controllerutil.ContainsFinalizer(&auto, imagev1.ImageUpdateAutomationFinalizer) { + patch := client.MergeFrom(auto.DeepCopy()) + controllerutil.AddFinalizer(&auto, imagev1.ImageUpdateAutomationFinalizer) + if err := r.Patch(ctx, &auto, patch); err != nil { + log.Error(err, "unable to register finalizer") + return ctrl.Result{}, err + } + } + + // If the object is under deletion, record the readiness, and remove our finalizer. + if !auto.ObjectMeta.DeletionTimestamp.IsZero() { + r.recordReadinessMetric(ctx, &auto) + controllerutil.RemoveFinalizer(&auto, imagev1.ImageUpdateAutomationFinalizer) + if err := r.Update(ctx, &auto); err != nil { + return ctrl.Result{}, err + } + return ctrl.Result{}, nil + } + // record suspension metrics defer r.recordSuspension(ctx, auto)