diff --git a/api/v1beta2/conditions_consts.go b/api/v1beta2/conditions_consts.go index 716d487a4..1506ee0c2 100644 --- a/api/v1beta2/conditions_consts.go +++ b/api/v1beta2/conditions_consts.go @@ -36,8 +36,11 @@ const ( // InstanceErroredReason instance is in a errored state. InstanceErroredReason = "InstanceErrored" - // InstanceNotReadyReason used when the instance is in a pending state. + // InstanceNotReadyReason used when the instance is in a not ready state. InstanceNotReadyReason = "InstanceNotReady" + + // InstanceStateUnknownReason used when the instance is in a unknown state. + InstanceStateUnknownReason = "InstanceStateUnknown" ) const ( diff --git a/cloud/scope/powervs_machine.go b/cloud/scope/powervs_machine.go index bc07cc8c3..32028599a 100644 --- a/cloud/scope/powervs_machine.go +++ b/cloud/scope/powervs_machine.go @@ -202,6 +202,15 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err // TODO need a reasonable wrapped error. return instanceReply, nil } + + // Check if create request has been already triggered. + // If InstanceReadyCondition is Unknown then return and wait for it to get updated. + for _, con := range m.IBMPowerVSMachine.Status.Conditions { + if con.Type == infrav1beta2.InstanceReadyCondition && con.Status == corev1.ConditionUnknown { + return nil, nil + } + } + cloudInitData, err := m.GetBootstrapData() if err != nil { return nil, err diff --git a/cloud/scope/powervs_machine_test.go b/cloud/scope/powervs_machine_test.go index 45618b99a..67093a652 100644 --- a/cloud/scope/powervs_machine_test.go +++ b/cloud/scope/powervs_machine_test.go @@ -257,6 +257,22 @@ func TestCreateMachinePVS(t *testing.T) { require.Equal(t, expectedOutput.ServerName, out.ServerName) }) + t.Run("Return NIL when Machine is not present in the Instance list and Machine state is unknown", func(t *testing.T) { + g := NewWithT(t) + setup(t) + t.Cleanup(teardown) + expectedOutput := (*models.PVMInstanceReference)(nil) + scope := setupPowerVSMachineScope(clusterName, *core.StringPtr("foo-machine-2"), core.StringPtr(pvsImage), core.StringPtr(pvsNetwork), true, mockpowervs) + scope.IBMPowerVSMachine.Status.Conditions = append(scope.IBMPowerVSMachine.Status.Conditions, capiv1beta1.Condition{ + Type: infrav1beta2.InstanceReadyCondition, + Status: corev1.ConditionUnknown, + }) + mockpowervs.EXPECT().GetAllInstance().Return(pvmInstances, nil) + out, err := scope.CreateMachine() + g.Expect(err).To(BeNil()) + require.Equal(t, expectedOutput, out) + }) + t.Run("Eror while getting instances", func(t *testing.T) { g := NewWithT(t) setup(t) diff --git a/controllers/ibmpowervsmachine_controller.go b/controllers/ibmpowervsmachine_controller.go index e3e228c6f..9177328c5 100644 --- a/controllers/ibmpowervsmachine_controller.go +++ b/controllers/ibmpowervsmachine_controller.go @@ -262,6 +262,9 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(machineScope *scope.PowerV machineScope.Info("PowerVS instance state is undefined", "state", *instance.Status, "instance-id", machineScope.GetInstanceID()) conditions.MarkUnknown(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, "", "") } + } else { + machineScope.SetNotReady() + conditions.MarkUnknown(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceStateUnknownReason, "") } // Requeue after 2 minute if machine is not ready to update status of the machine properly.