func()

in controllers/cloudstackmachine_controller.go [142:202]


func (r *CloudStackMachineReconciler) reconcile(
	log logr.Logger,
	ctx context.Context,
	csCluster *infrav1.CloudStackCluster,
	csMachine *infrav1.CloudStackMachine,
	machine *capiv1.Machine) (ctrl.Result, error) {

	log.V(1).Info("reconcile CloudStackMachine")

	// Make sure bootstrap data is available in CAPI machine.
	if machine.Spec.Bootstrap.DataSecretName == nil {
		log.Info("Bootstrap DataSecretName not yet available.")
		return ctrl.Result{}, nil
	}
	log.Info("Got Bootstrap DataSecretName: " + *machine.Spec.Bootstrap.DataSecretName)

	secret := &corev1.Secret{}
	key := types.NamespacedName{Namespace: machine.Namespace, Name: *machine.Spec.Bootstrap.DataSecretName}
	if err := r.Client.Get(context.TODO(), key, secret); err != nil {
		return ctrl.Result{}, err
	}

	value, ok := secret.Data["value"]
	if !ok {
		return ctrl.Result{}, errors.New("Bootstrap secret data not ok.")
	}

	// Create VM (or Fetch if present). Will set ready to true.
	if err := r.CS.GetOrCreateVMInstance(csMachine, machine, csCluster, string(value)); err == nil {
		if !controllerutil.ContainsFinalizer(csMachine, infrav1.MachineFinalizer) { // Fetched or Created?
			log.Info("CloudStack instance Created", "instanceStatus", csMachine.Status, "instanceSpec", csMachine.Spec)
			controllerutil.AddFinalizer(csMachine, infrav1.MachineFinalizer)
		}
	} else if err != nil {
		return ctrl.Result{}, err
	}

	if csMachine.Status.InstanceState == "Running" {
		log.Info("Machine instance is Running...")
		csMachine.Status.Ready = true
	} else if csMachine.Status.InstanceState == "Error" {
		log.Info("CloudStackMachine VM in error state.  Deleting associated Machine.", "csMachine", csMachine)
		if err := r.Client.Delete(ctx, machine); err != nil {
			return ctrl.Result{}, err
		}
		return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
	} else {
		log.Info(fmt.Sprintf("Instance not ready, is %s.", csMachine.Status.InstanceState))
		return ctrl.Result{RequeueAfter: RequeueTimeout}, nil
	}

	if util.IsControlPlaneMachine(machine) && csCluster.Status.NetworkType != cloud.NetworkTypeShared {
		log.Info("Assigning VM to load balancer rule.")
		err := r.CS.AssignVMToLoadBalancerRule(csCluster, *csMachine.Spec.InstanceID)
		if err != nil {
			return ctrl.Result{}, err
		}
	}

	return ctrl.Result{}, nil
}