Skip to content

Commit 6162869

Browse files
Merge pull request #2752 from Nordix/delete-child-res-first-release-1-10/adil
🐛 Delete child resources before machine
2 parents d879d4f + 204eaa6 commit 6162869

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

controllers/metal3machine_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,20 @@ func (r *Metal3MachineReconciler) reconcileDelete(ctx context.Context,
292292

293293
errType := capierrors.DeleteMachineError
294294

295+
// dissociate metadata if any
296+
if err := machineMgr.DissociateM3Metadata(ctx); err != nil {
297+
machineMgr.SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DisassociateM3MetaDataFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
298+
return checkMachineError(machineMgr, err,
299+
"failed to dissociate Metadata", errType)
300+
}
301+
295302
// delete the machine
296303
if err := machineMgr.Delete(ctx); err != nil {
297304
machineMgr.SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
298305
return checkMachineError(machineMgr, err,
299306
"failed to delete Metal3Machine", errType)
300307
}
301308

302-
if err := machineMgr.DissociateM3Metadata(ctx); err != nil {
303-
machineMgr.SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DisassociateM3MetaDataFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
304-
return checkMachineError(machineMgr, err,
305-
"failed to dissociate Metadata", errType)
306-
}
307-
308309
// metal3machine is marked for deletion and ready to be deleted,
309310
// so remove the finalizer.
310311
machineMgr.UnsetFinalizer()

controllers/metal3machine_controller_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ func setReconcileNormalExpectations(ctrl *gomock.Controller,
161161
}
162162

163163
type reconcileDeleteTestCase struct {
164-
ExpectError bool
165-
ExpectRequeue bool
166-
DeleteFails bool
167-
DeleteRequeue bool
164+
ExpectError bool
165+
ExpectRequeue bool
166+
DeleteFails bool
167+
DissociateM3MetadataFails bool
168+
DeleteRequeue bool
168169
}
169170

170171
func setReconcileDeleteExpectations(ctrl *gomock.Controller,
@@ -173,19 +174,28 @@ func setReconcileDeleteExpectations(ctrl *gomock.Controller,
173174
m := baremetal_mocks.NewMockMachineManagerInterface(ctrl)
174175
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "")
175176

177+
if tc.DissociateM3MetadataFails {
178+
m.EXPECT().DissociateM3Metadata(context.TODO()).Return(errors.New("failed"))
179+
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DisassociateM3MetaDataFailedReason, clusterv1.ConditionSeverityWarning, gomock.Any())
180+
m.EXPECT().Delete(context.TODO()).MaxTimes(0)
181+
m.EXPECT().UnsetFinalizer().MaxTimes(0)
182+
return m
183+
}
176184
if tc.DeleteFails {
177-
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, gomock.Any())
185+
m.EXPECT().DissociateM3Metadata(context.TODO())
178186
m.EXPECT().Delete(context.TODO()).Return(errors.New("failed"))
187+
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, gomock.Any())
179188
m.EXPECT().UnsetFinalizer().MaxTimes(0)
180-
m.EXPECT().DissociateM3Metadata(context.TODO()).MaxTimes(0)
181189
return m
182-
} else if tc.DeleteRequeue {
183-
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, gomock.Any())
190+
}
191+
if tc.DeleteRequeue {
192+
m.EXPECT().DissociateM3Metadata(context.TODO())
184193
m.EXPECT().Delete(context.TODO()).Return(baremetal.WithTransientError(errors.New("failed"), requeueAfter))
194+
m.EXPECT().SetConditionMetal3MachineToFalse(infrav1.KubernetesNodeReadyCondition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, gomock.Any())
185195
m.EXPECT().UnsetFinalizer().MaxTimes(0)
186-
m.EXPECT().DissociateM3Metadata(context.TODO()).MaxTimes(0)
187196
return m
188197
}
198+
189199
m.EXPECT().DissociateM3Metadata(context.TODO())
190200
m.EXPECT().Delete(context.TODO()).Return(nil)
191201
m.EXPECT().UnsetFinalizer()
@@ -336,6 +346,12 @@ var _ = Describe("Metal3Machine manager", func() {
336346
ExpectRequeue: true,
337347
DeleteRequeue: true,
338348
}),
349+
Entry("DissociateM3Metadata failure", reconcileDeleteTestCase{
350+
ExpectError: true,
351+
ExpectRequeue: false,
352+
DeleteRequeue: false,
353+
DissociateM3MetadataFails: true,
354+
}),
339355
)
340356
})
341357

0 commit comments

Comments
 (0)