From 555fb5670edd0cc6bf6afc4b4a2956d5af731532 Mon Sep 17 00:00:00 2001 From: Thomas Morin Date: Mon, 23 Sep 2024 11:34:52 +0200 Subject: [PATCH] M3DataTemplate: requeue if reconcileDelete did not clear finalizer Signed-off-by: Thomas Morin --- controllers/metal3datatemplate_controller.go | 16 ++++++++++------ .../metal3datatemplate_controller_test.go | 9 +++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/controllers/metal3datatemplate_controller.go b/controllers/metal3datatemplate_controller.go index d5d136e43f..e343565a45 100644 --- a/controllers/metal3datatemplate_controller.go +++ b/controllers/metal3datatemplate_controller.go @@ -129,7 +129,7 @@ func (r *Metal3DataTemplateReconciler) Reconcile(ctx context.Context, req ctrl.R // Handle deletion of Metal3DataTemplate if !metal3DataTemplate.ObjectMeta.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, dataTemplateMgr) + return r.reconcileDelete(ctx, dataTemplateMgr, log) } // Handle non-deleted Metal3DataTemplate @@ -151,18 +151,22 @@ func (r *Metal3DataTemplateReconciler) reconcileNormal(ctx context.Context, func (r *Metal3DataTemplateReconciler) reconcileDelete(ctx context.Context, dataTemplateMgr baremetal.DataTemplateManagerInterface, + log logr.Logger, ) (ctrl.Result, error) { allocationsCount, err := dataTemplateMgr.UpdateDatas(ctx) if err != nil { - return checkReconcileError(err, "Failed to recreate the status") + _, err := checkReconcileError(err, "Failed to recreate the status, requeuing") + return ctrl.Result{Requeue: true, RequeueAfter: requeueAfter}, err } - if allocationsCount == 0 { - // metal3datatemplate is marked for deletion and ready to be deleted, - // so remove the finalizer. - dataTemplateMgr.UnsetFinalizer() + if allocationsCount != 0 { + log.Info("some Metal3DataClaim remain, not unsetting finalizer yet, requeuing") + return ctrl.Result{Requeue: true, RequeueAfter: requeueAfter}, nil } + // metal3datatemplate is marked for deletion and ready to be deleted, + // so remove the finalizer. + dataTemplateMgr.UnsetFinalizer() return ctrl.Result{}, nil } diff --git a/controllers/metal3datatemplate_controller_test.go b/controllers/metal3datatemplate_controller_test.go index b89d3258ec..21e8b82077 100644 --- a/controllers/metal3datatemplate_controller_test.go +++ b/controllers/metal3datatemplate_controller_test.go @@ -159,6 +159,7 @@ var _ = Describe("Metal3DataTemplate manager", func() { expectManager: true, reconcileDeleteError: true, expectError: true, + expectRequeue: true, }), Entry("Paused cluster", testCaseReconcile{ m3dt: &infrav1.Metal3DataTemplate{ @@ -292,7 +293,7 @@ var _ = Describe("Metal3DataTemplate manager", func() { m.EXPECT().UpdateDatas(context.TODO()).Return(0, errors.New("")) } - res, err := r.reconcileDelete(context.TODO(), m) + res, err := r.reconcileDelete(context.TODO(), m, logr.Discard()) gomockCtrl.Finish() if tc.ExpectError { @@ -307,14 +308,14 @@ var _ = Describe("Metal3DataTemplate manager", func() { } }, - Entry("No error", reconcileDeleteTestCase{ + Entry("No error, not ready", reconcileDeleteTestCase{ ExpectError: false, - ExpectRequeue: false, + ExpectRequeue: true, }), Entry("Delete error", reconcileDeleteTestCase{ DeleteError: true, ExpectError: true, - ExpectRequeue: false, + ExpectRequeue: true, }), Entry("Delete ready", reconcileDeleteTestCase{ ExpectError: false,