Skip to content

Commit

Permalink
Move VMState Status update to VM reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecile Robert-Michon committed Mar 16, 2021
1 parent 6f43c41 commit e700487
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
25 changes: 25 additions & 0 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,31 @@ func (m *MachineScope) SetCondition(condition clusterv1.ConditionType, reason st
}
}

func (m *MachineScope) UpdateStatus() {
switch m.VMState() {
case infrav1.VMStateSucceeded:
m.V(2).Info("VM is running", "id", m.GetVMID())
conditions.MarkTrue(m.AzureMachine, infrav1.VMRunningCondition)
case infrav1.VMStateCreating:
m.V(2).Info("VM is creating", "id", m.GetVMID())
conditions.MarkFalse(m.AzureMachine, infrav1.VMRunningCondition, infrav1.VMCreatingReason, clusterv1.ConditionSeverityInfo, "")
case infrav1.VMStateUpdating:
m.V(2).Info("VM is updating", "id", m.GetVMID())
conditions.MarkFalse(m.AzureMachine, infrav1.VMRunningCondition, infrav1.VMUpdatingReason, clusterv1.ConditionSeverityInfo, "")
case infrav1.VMStateDeleting:
m.Info("Unexpected VM deletion", "id", m.GetVMID())
conditions.MarkFalse(m.AzureMachine, infrav1.VMRunningCondition, infrav1.VMDeletingReason, clusterv1.ConditionSeverityWarning, "")
case infrav1.VMStateFailed:
m.Error(errors.New("Failed to create or update VM"), "VM is in failed state", "id", m.GetVMID())
m.SetFailureReason(capierrors.UpdateMachineError)
m.SetFailureMessage(errors.Errorf("Azure VM state is %s", m.VMState()))
conditions.MarkFalse(m.AzureMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, "")
default:
m.V(2).Info("VM state is undefined", "id", m.GetVMID())
conditions.MarkUnknown(m.AzureMachine, infrav1.VMRunningCondition, "", "")
}
}

// SetAnnotation sets a key value annotation on the AzureMachine.
func (m *MachineScope) SetAnnotation(key, value string) {
if m.AzureMachine.Annotations == nil {
Expand Down
2 changes: 2 additions & 0 deletions azure/services/virtualmachines/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type VMScope interface {
SetProviderID(string)
SetAddresses([]corev1.NodeAddress)
SetVMState(infrav1.VMState)
UpdateStatus()
}

// Service provides operations on azure resources
Expand Down Expand Up @@ -97,6 +98,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
s.Scope.SetAnnotation("cluster-api-provider-azure", "true")
s.Scope.SetAddresses(existingVM.Addresses)
s.Scope.SetVMState(existingVM.State)
s.Scope.UpdateStatus()
default:
s.Scope.V(2).Info("creating VM", "vm", vmSpec.Name)
sku, err := s.resourceSKUCache.Get(ctx, vmSpec.Size, resourceskus.VirtualMachines)
Expand Down
43 changes: 5 additions & 38 deletions controllers/azuremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,8 @@ func (r *AzureMachineReconciler) reconcileNormal(ctx context.Context, machineSco
// Handle transient and terminal errors
var reconcileError azure.ReconcileError
if errors.As(err, &reconcileError) {
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "ReconcileError", errors.Wrap(err, "failed to reconcile AzureMachine").Error())
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, err.Error())

if reconcileError.IsTerminal() {
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "ReconcileError", errors.Wrapf(err, "failed to reconcile AzureMachine").Error())
machineScope.Error(err, "failed to reconcile AzureMachine", "name", machineScope.Name())
machineScope.SetFailureReason(capierrors.CreateMachineError)
machineScope.SetFailureMessage(err)
Expand All @@ -309,48 +307,17 @@ func (r *AzureMachineReconciler) reconcileNormal(ctx context.Context, machineSco
}

if reconcileError.IsTransient() {
machineScope.Error(err, "failed to reconcile AzureMachine", "name", machineScope.Name())
machineScope.Error(err, "transient failure to reconcile AzureMachine, retrying", "name", machineScope.Name())
machineScope.SetNotReady()
return reconcile.Result{RequeueAfter: reconcileError.RequeueAfter()}, nil
}

return reconcile.Result{}, errors.Wrap(err, "failed to reconcile AzureMachine")
}

r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "Error creating new AzureMachine", errors.Wrap(err, "failed to reconcile AzureMachine").Error())
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "ReconcileError", errors.Wrapf(err, "failed to reconcile AzureMachine").Error())
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, err.Error())
return reconcile.Result{}, errors.Wrap(err, "failed to reconcile AzureMachine")
}

switch machineScope.VMState() {
case infrav1.VMStateSucceeded:
machineScope.V(2).Info("VM is running", "id", machineScope.GetVMID())
conditions.MarkTrue(machineScope.AzureMachine, infrav1.VMRunningCondition)
machineScope.SetReady()
case infrav1.VMStateCreating:
machineScope.V(2).Info("VM is creating", "id", machineScope.GetVMID())
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMCreatingReason, clusterv1.ConditionSeverityInfo, "")
machineScope.SetNotReady()
case infrav1.VMStateUpdating:
machineScope.V(2).Info("VM is updating", "id", machineScope.GetVMID())
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMUpdatingReason, clusterv1.ConditionSeverityInfo, "")
machineScope.SetNotReady()
case infrav1.VMStateDeleting:
machineScope.Info("Unexpected VM deletion", "id", machineScope.GetVMID())
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "UnexpectedVMDeletion", "Unexpected Azure VM deletion")
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMDeletingReason, clusterv1.ConditionSeverityWarning, "")
machineScope.SetNotReady()
case infrav1.VMStateFailed:
machineScope.Error(errors.New("Failed to create or update VM"), "VM is in failed state", "id", machineScope.GetVMID())
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "FailedVMState", "Azure VM is in failed state")
machineScope.SetFailureReason(capierrors.UpdateMachineError)
machineScope.SetFailureMessage(errors.Errorf("Azure VM state is %s", machineScope.VMState()))
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, "")
machineScope.SetNotReady()
default:
machineScope.V(2).Info("VM state is undefined", "id", machineScope.GetVMID())
conditions.MarkUnknown(machineScope.AzureMachine, infrav1.VMRunningCondition, "", "")
machineScope.SetNotReady()
}
machineScope.SetReady()

return reconcile.Result{}, nil
}
Expand Down

0 comments on commit e700487

Please sign in to comment.