diff --git a/controllers/metal3datatemplate_controller.go b/controllers/metal3datatemplate_controller.go index d5d136e43..e343565a4 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 b89d3258e..21e8b8207 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,