Skip to content

Commit 5407081

Browse files
Fix vm state bug and deletion bug (#340)
* Do not put machine in failed state if VM is in stopping or stopped state, and use ID from instance during deletion
1 parent c46573b commit 5407081

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

cloud/scope/machine.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ func (m *MachineScope) getFreeFormTags() map[string]string {
301301
}
302302

303303
// DeleteMachine terminates the instance using InstanceId from the OCIMachine spec and deletes the boot volume
304-
func (m *MachineScope) DeleteMachine(ctx context.Context) error {
305-
req := core.TerminateInstanceRequest{InstanceId: m.OCIMachine.Spec.InstanceId,
304+
func (m *MachineScope) DeleteMachine(ctx context.Context, instance *core.Instance) error {
305+
req := core.TerminateInstanceRequest{InstanceId: instance.Id,
306306
PreserveBootVolume: common.Bool(false)}
307307
_, err := m.ComputeClient.TerminateInstance(ctx, req)
308308
return err
@@ -405,6 +405,11 @@ func (m *MachineScope) SetReady() {
405405
m.OCIMachine.Status.Ready = true
406406
}
407407

408+
// SetNotReady sets the OCIMachine Ready Status to false.
409+
func (m *MachineScope) SetNotReady() {
410+
m.OCIMachine.Status.Ready = false
411+
}
412+
408413
// IsReady returns the ready status of the machine.
409414
func (m *MachineScope) IsReady() bool {
410415
return m.OCIMachine.Status.Ready

cloud/scope/machine_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2393,6 +2393,7 @@ func TestInstanceDeletion(t *testing.T) {
23932393
expectedEvent string
23942394
eventNotExpected string
23952395
matchError error
2396+
instance *core.Instance
23962397
errorSubStringMatch bool
23972398
testSpecificSetup func(machineScope *MachineScope, computeClient *mock_compute.MockComputeClient)
23982399
}{
@@ -2406,6 +2407,9 @@ func TestInstanceDeletion(t *testing.T) {
24062407
PreserveBootVolume: common.Bool(false),
24072408
})).Return(core.TerminateInstanceResponse{}, nil)
24082409
},
2410+
instance: &core.Instance{
2411+
Id: common.String("test"),
2412+
},
24092413
},
24102414
{
24112415
name: "delete instance error",
@@ -2418,6 +2422,9 @@ func TestInstanceDeletion(t *testing.T) {
24182422
PreserveBootVolume: common.Bool(false),
24192423
})).Return(core.TerminateInstanceResponse{}, errors.New("could not terminate instance"))
24202424
},
2425+
instance: &core.Instance{
2426+
Id: common.String("test"),
2427+
},
24212428
},
24222429
}
24232430

@@ -2427,7 +2434,7 @@ func TestInstanceDeletion(t *testing.T) {
24272434
defer teardown(t, g)
24282435
setup(t, g)
24292436
tc.testSpecificSetup(ms, computeClient)
2430-
err := ms.DeleteMachine(context.Background())
2437+
err := ms.DeleteMachine(context.Background(), tc.instance)
24312438
if tc.errorExpected {
24322439
g.Expect(err).To(Not(BeNil()))
24332440
if tc.errorSubStringMatch {

controllers/ocimachine_controller.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ func (r *OCIMachineReconciler) reconcileNormal(ctx context.Context, logger logr.
326326
machineScope.Info("Instance is pending")
327327
conditions.MarkFalse(machineScope.OCIMachine, infrastructurev1beta2.InstanceReadyCondition, infrastructurev1beta2.InstanceNotReadyReason, clusterv1.ConditionSeverityInfo, "")
328328
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
329+
case core.InstanceLifecycleStateStopping, core.InstanceLifecycleStateStopped:
330+
machineScope.SetNotReady()
331+
conditions.MarkFalse(machineScope.OCIMachine, infrastructurev1beta2.InstanceReadyCondition, infrastructurev1beta2.InstanceNotReadyReason, clusterv1.ConditionSeverityInfo, "")
332+
return reconcile.Result{}, nil
329333
case core.InstanceLifecycleStateRunning:
330334
machineScope.Info("Instance is active")
331335
if machine.Status.Addresses == nil || len(machine.Status.Addresses) == 0 {
@@ -387,6 +391,7 @@ func (r *OCIMachineReconciler) reconcileNormal(ctx context.Context, logger logr.
387391
}
388392
fallthrough
389393
default:
394+
machineScope.SetNotReady()
390395
conditions.MarkFalse(machineScope.OCIMachine, infrastructurev1beta2.InstanceReadyCondition, infrastructurev1beta2.InstanceProvisionFailedReason, clusterv1.ConditionSeverityError, "")
391396
machineScope.SetFailureReason(capierrors.CreateMachineError)
392397
machineScope.SetFailureMessage(errors.Errorf("Instance status %q is unexpected", instance.LifecycleState))
@@ -446,7 +451,7 @@ func (r *OCIMachineReconciler) reconcileDelete(ctx context.Context, machineScope
446451
if err != nil {
447452
return reconcile.Result{}, err
448453
}
449-
if err := machineScope.DeleteMachine(ctx); err != nil {
454+
if err := machineScope.DeleteMachine(ctx, instance); err != nil {
450455
machineScope.Error(err, "Error deleting Instance")
451456
return ctrl.Result{}, errors.Wrapf(err, "error deleting instance %s", machineScope.Name())
452457
}

controllers/ocimachine_controller_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,38 @@ func TestNormalReconciliationFunction(t *testing.T) {
302302
g.Expect(result.RequeueAfter).To(Equal(300 * time.Second))
303303
},
304304
},
305+
{
306+
name: "instance in stopped state",
307+
errorExpected: false,
308+
conditionAssertion: []conditionAssertion{{infrastructurev1beta2.InstanceReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, infrastructurev1beta2.InstanceNotReadyReason}},
309+
testSpecificSetup: func(t *test, machineScope *scope.MachineScope, computeClient *mock_compute.MockComputeClient, vcnClient *mock_vcn.MockClient, nlbclient *mock_nlb.MockNetworkLoadBalancerClient) {
310+
computeClient.EXPECT().GetInstance(gomock.Any(), gomock.Eq(core.GetInstanceRequest{
311+
InstanceId: common.String("test"),
312+
})).
313+
Return(core.GetInstanceResponse{
314+
Instance: core.Instance{
315+
Id: common.String("test"),
316+
LifecycleState: core.InstanceLifecycleStateStopped,
317+
},
318+
}, nil)
319+
},
320+
},
321+
{
322+
name: "instance in stopping state",
323+
errorExpected: false,
324+
conditionAssertion: []conditionAssertion{{infrastructurev1beta2.InstanceReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, infrastructurev1beta2.InstanceNotReadyReason}},
325+
testSpecificSetup: func(t *test, machineScope *scope.MachineScope, computeClient *mock_compute.MockComputeClient, vcnClient *mock_vcn.MockClient, nlbclient *mock_nlb.MockNetworkLoadBalancerClient) {
326+
computeClient.EXPECT().GetInstance(gomock.Any(), gomock.Eq(core.GetInstanceRequest{
327+
InstanceId: common.String("test"),
328+
})).
329+
Return(core.GetInstanceResponse{
330+
Instance: core.Instance{
331+
Id: common.String("test"),
332+
LifecycleState: core.InstanceLifecycleStateStopping,
333+
},
334+
}, nil)
335+
},
336+
},
305337
{
306338
name: "instance in terminated state",
307339
errorExpected: true,

0 commit comments

Comments
 (0)