From 4b116809d7b307cc0daa75d43c57f37ca5e3abf2 Mon Sep 17 00:00:00 2001 From: apluchik <68574278+apluchik@users.noreply.github.com> Date: Tue, 6 Jul 2021 19:28:13 -0700 Subject: [PATCH] Bubble up conditions to capi objects (#141) --- cloud/scope/machine.go | 2 +- controllers/azurestackhcicluster_controller.go | 12 +++++++++++- controllers/azurestackhcimachine_controller.go | 4 ++-- .../azurestackhcivirtualmachine_controller.go | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index a7e2960e..04338b79 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -209,7 +209,7 @@ func (m *MachineScope) PatchObject() error { // Close the MachineScope by updating the machine spec, machine status. func (m *MachineScope) Close() error { - return m.patchHelper.Patch(context.TODO(), m.AzureStackHCIMachine) + return m.PatchObject() } // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName. diff --git a/controllers/azurestackhcicluster_controller.go b/controllers/azurestackhcicluster_controller.go index 100b9603..a52a507b 100644 --- a/controllers/azurestackhcicluster_controller.go +++ b/controllers/azurestackhcicluster_controller.go @@ -26,6 +26,7 @@ import ( azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud" "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope" infrav1util "github.com/microsoft/cluster-api-provider-azurestackhci/pkg/util" + mocerrors "github.com/microsoft/moc/pkg/errors" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -132,9 +133,18 @@ func (r *AzureStackHCIClusterReconciler) reconcileNormal(clusterScope *scope.Clu err := newAzureStackHCIClusterReconciler(clusterScope).Reconcile() if err != nil { + switch mocerrors.GetErrorCode(err) { + case mocerrors.OutOfMemory.Error(): + conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.OutOfMemoryReason, clusterv1.ConditionSeverityError, err.Error()) + case mocerrors.OutOfCapacity.Error(): + conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.OutOfCapacityReason, clusterv1.ConditionSeverityError, err.Error()) + default: + conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.ClusterReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error()) + } + wrappedErr := errors.Wrap(err, "failed to reconcile cluster services") r.Recorder.Eventf(azureStackHCICluster, corev1.EventTypeWarning, "ClusterReconcileFailed", wrappedErr.Error()) - conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.ClusterReconciliationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + return reconcile.Result{}, wrappedErr } diff --git a/controllers/azurestackhcimachine_controller.go b/controllers/azurestackhcimachine_controller.go index 3f4aa3c1..2d6f6003 100644 --- a/controllers/azurestackhcimachine_controller.go +++ b/controllers/azurestackhcimachine_controller.go @@ -222,6 +222,8 @@ func (r *AzureStackHCIMachineReconciler) reconcileNormal(machineScope *scope.Mac // TODO(vincepri): Remove this annotation when clusterctl is no longer relevant. machineScope.SetAnnotation("cluster-api-provider-azurestackhci", "true") + machineScope.AzureStackHCIMachine.Status.Conditions = append(machineScope.AzureStackHCIMachine.Status.Conditions, vm.Status.Conditions...) + if vm.Status.VMState == nil { machineScope.Info("Waiting for VM controller to set vm state") return reconcile.Result{Requeue: true, RequeueAfter: time.Minute}, nil @@ -241,8 +243,6 @@ func (r *AzureStackHCIMachineReconciler) reconcileNormal(machineScope *scope.Mac machineScope.SetFailureMessage(errors.Errorf("AzureStackHCI VM state %q is unexpected", *machineScope.GetVMState())) } - machineScope.AzureStackHCIMachine.Status.Conditions = append(machineScope.AzureStackHCIMachine.Status.Conditions, vm.Status.Conditions...) - return reconcile.Result{}, nil } diff --git a/controllers/azurestackhcivirtualmachine_controller.go b/controllers/azurestackhcivirtualmachine_controller.go index 39e32ca6..054f6ea5 100644 --- a/controllers/azurestackhcivirtualmachine_controller.go +++ b/controllers/azurestackhcivirtualmachine_controller.go @@ -188,7 +188,7 @@ func (r *AzureStackHCIVirtualMachineReconciler) getOrCreate(virtualMachineScope case mocerrors.OutOfMemory.Error(): conditions.MarkFalse(virtualMachineScope.AzureStackHCIVirtualMachine, infrav1.VMRunningCondition, infrav1.OutOfMemoryReason, clusterv1.ConditionSeverityError, err.Error()) case mocerrors.OutOfCapacity.Error(): - conditions.MarkFalse(virtualMachineScope.AzureStackHCIVirtualMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, err.Error()) + conditions.MarkFalse(virtualMachineScope.AzureStackHCIVirtualMachine, infrav1.VMRunningCondition, infrav1.OutOfCapacityReason, clusterv1.ConditionSeverityError, err.Error()) default: conditions.MarkFalse(virtualMachineScope.AzureStackHCIVirtualMachine, infrav1.VMRunningCondition, infrav1.VMProvisionFailedReason, clusterv1.ConditionSeverityError, err.Error()) }