diff --git a/controllers/backupconfiguration_controller.go b/controllers/backupconfiguration_controller.go index 37c88d3..0db5a17 100644 --- a/controllers/backupconfiguration_controller.go +++ b/controllers/backupconfiguration_controller.go @@ -24,10 +24,11 @@ import ( "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1" - formolutils "github.com/desmo999r/formol/pkg/utils" + //formolutils "github.com/desmo999r/formol/pkg/utils" ) // BackupConfigurationReconciler reconciles a BackupConfiguration object @@ -70,10 +71,10 @@ func (r *BackupConfigurationReconciler) Reconcile(ctx context.Context, req ctrl. if !backupConf.ObjectMeta.DeletionTimestamp.IsZero() { r.Log.V(0).Info("backupconf being deleted", "backupconf", backupConf.ObjectMeta.Finalizers) - if formolutils.ContainsString(backupConf.ObjectMeta.Finalizers, finalizerName) { + if controllerutil.ContainsFinalizer(&backupConf, finalizerName) { _ = r.DeleteSidecar(backupConf) _ = r.DeleteCronJob(backupConf) - backupConf.ObjectMeta.Finalizers = formolutils.RemoveString(backupConf.ObjectMeta.Finalizers, finalizerName) + controllerutil.RemoveFinalizer(&backupConf, finalizerName) if err := r.Update(ctx, &backupConf); err != nil { r.Log.Error(err, "unable to remove finalizer") return ctrl.Result{}, err @@ -85,9 +86,9 @@ func (r *BackupConfigurationReconciler) Reconcile(ctx context.Context, req ctrl. } // Add finalizer - if !formolutils.ContainsString(backupConf.ObjectMeta.Finalizers, finalizerName) { + if !controllerutil.ContainsFinalizer(&backupConf, finalizerName) { r.Log.V(0).Info("adding finalizer", "backupconf", backupConf) - backupConf.ObjectMeta.Finalizers = append(backupConf.ObjectMeta.Finalizers, finalizerName) + controllerutil.AddFinalizer(&backupConf, finalizerName) if err := r.Update(ctx, &backupConf); err != nil { r.Log.Error(err, "unable to append finalizer") return ctrl.Result{}, err diff --git a/controllers/backupsession_controller.go b/controllers/backupsession_controller.go index 79ba0c3..3f744d7 100644 --- a/controllers/backupsession_controller.go +++ b/controllers/backupsession_controller.go @@ -20,14 +20,20 @@ import ( "context" "github.com/go-logr/logr" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1" ) +const ( + finalizerName string = "finalizer.backupsession.formol.desmojim.fr" +) + // BackupSessionReconciler reconciles a BackupSession object type BackupSessionReconciler struct { client.Client @@ -54,6 +60,42 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques r.Context = ctx r.Log.V(1).Info("Enter Reconcile with req", "req", req, "reconciler", r) + backupSession := formolv1alpha1.BackupSession{} + err := r.Get(ctx, req.NamespacedName, &backupSession) + if err != nil { + if errors.IsNotFound(err) { + return ctrl.Result{}, nil + } + return ctrl.Result{}, err + } + backupConf := formolv1alpha1.BackupConfiguration{} + if err := r.Get(ctx, client.ObjectKey{ + Namespace: backupSession.Spec.Ref.Namespace, + Name: backupSession.Spec.Ref.Name, + }, &backupConf); err != nil { + r.Log.Error(err, "unable to get BackupConfiguration") + return ctrl.Result{}, err + } + + if !backupSession.ObjectMeta.DeletionTimestamp.IsZero() { + r.Log.V(0).Info("BackupSession is being deleted") + if controllerutil.ContainsFinalizer(&backupSession, finalizerName) { + controllerutil.RemoveFinalizer(&backupSession, finalizerName) + err := r.Update(ctx, &backupSession) + if err != nil { + r.Log.Error(err, "unable to remove finalizer") + } + return ctrl.Result{}, err + } + } + if !controllerutil.ContainsFinalizer(&backupSession, finalizerName) { + controllerutil.AddFinalizer(&backupSession, finalizerName) + err := r.Update(ctx, &backupSession) + if err != nil { + r.Log.Error(err, "unable to add finalizer") + } + return ctrl.Result{}, err + } return ctrl.Result{}, nil }