in controllers/cloudstackmachine_controller.go [67:139]
func (r *CloudStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (retRes ctrl.Result, retErr error) {
log := r.Log.WithValues("machine", req.Name, "namespace", req.Namespace)
log.V(1).Info("Reconcile CloudStackMachine")
// Fetch the CloudStackMachine.
csMachine := &infrav1.CloudStackMachine{}
if retErr = r.Client.Get(ctx, req.NamespacedName, csMachine); retErr != nil {
if client.IgnoreNotFound(retErr) == nil {
log.Info("CloudStackMachine not found.")
}
return ctrl.Result{}, client.IgnoreNotFound(retErr)
}
// Setup patcher. This ensures modifications to the csMachine copy fetched above are patched into the origin.
if patchHelper, retErr := patch.NewHelper(csMachine, r.Client); retErr != nil {
return ctrl.Result{}, retErr
} else {
defer func() { // If there was no error on return, but the patch fails, set the error accordingly.
if err := patchHelper.Patch(ctx, csMachine); retErr == nil && err != nil {
retErr = err
}
}()
}
// Fetch the CAPI Machine.
machine, retErr := util.GetOwnerMachine(ctx, r.Client, csMachine.ObjectMeta)
if retErr != nil {
return ctrl.Result{}, retErr
} else if machine == nil {
log.Info("Waiting for CAPI cluster controller to set owner reference on CloudStack machine.")
return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
}
// Fetch the CAPI Cluster.
cluster, retErr := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
if retErr != nil {
log.Info("Machine is missing cluster label or cluster does not exist.")
return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
}
// Check the machine is not paused.
if annotations.IsPaused(cluster, csMachine) {
log.Info("CloudStackMachine or linked Cluster is paused. Requeuing reconcile.")
return reconcile.Result{}, nil
}
// Delete VM instance if deletion timestamp present.
if !csMachine.DeletionTimestamp.IsZero() {
return r.reconcileDelete(log, csMachine)
}
// Fetch the CloudStack cluster associated with this machine.
csCluster := &infrav1.CloudStackCluster{}
if retErr := r.Client.Get(
ctx,
client.ObjectKey{
Namespace: csMachine.Namespace,
Name: cluster.Spec.InfrastructureRef.Name},
csCluster); retErr != nil {
if client.IgnoreNotFound(retErr) == nil {
log.Info("CloudStackCluster not found.")
return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
} else {
return ctrl.Result{}, retErr
}
} else if csCluster.Status.ZoneID == "" {
log.Info("CloudStackCluster ZoneId not initialized. Likely not ready.")
return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
}
// Reconcile a VM instance for creates/updates
return r.reconcile(log, ctx, csCluster, csMachine, machine)
}