From 205d8ce9aff0a59ac3f8ebdf53485b958fd25f53 Mon Sep 17 00:00:00 2001 From: Zach <45979377+zawachte-msft@users.noreply.github.com> Date: Wed, 8 Sep 2021 10:53:15 -0400 Subject: [PATCH] Delete Orphaned Machines (#149) * delete orphaned machines * fiix pipeline * up the tag --- Makefile | 2 +- azure-pipelines-release.yml | 8 ++--- config/manager/manager_image_patch.yaml | 2 +- .../azurestackhcicluster_controller.go | 29 +++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 283d961f..c17f944a 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ ETCD=$(TOOLS_BIN_DIR)/etcd # Version MAJOR_VER ?= 0 MINOR_VER ?= 3 -PATCH_VER ?= 9-alpha +PATCH_VER ?= 10-alpha # Define Docker related variables. Releases should modify and double check these vars. REGISTRY ?= mocimages.azurecr.io diff --git a/azure-pipelines-release.yml b/azure-pipelines-release.yml index 2858741a..0bc8e265 100644 --- a/azure-pipelines-release.yml +++ b/azure-pipelines-release.yml @@ -38,10 +38,10 @@ steps: exit 1 fi - make REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller RELEASE_TAG=$(RELEASE_TAG) docker-build docker-push - make REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller RELEASE_TAG=$(RELEASE_TAG) release - make REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller RELEASE_TAG=$(RELEASE_TAG) generate-flavors - make REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller RELEASE_TAG=$(RELEASE_TAG) release-pipelines + make PROD_REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller TAG=$(RELEASE_TAG) docker-build docker-push + make PROD_REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller TAG=$(RELEASE_TAG) release + make PROD_REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller TAG=$(RELEASE_TAG) generate-flavors + make PROD_REGISTRY=ecpacr.azurecr.io IMAGE_NAME=caphcontroller TAG=$(RELEASE_TAG) release-pipelines workingDirectory: '$(System.DefaultWorkingDirectory)' displayName: 'Build CAPH' diff --git a/config/manager/manager_image_patch.yaml b/config/manager/manager_image_patch.yaml index 502af43b..b603d5aa 100644 --- a/config/manager/manager_image_patch.yaml +++ b/config/manager/manager_image_patch.yaml @@ -7,5 +7,5 @@ spec: template: spec: containers: - - image: mocimages.azurecr.io/caphcontroller:0.3.9-alpha + - image: mocimages.azurecr.io/caphcontroller:0.3.10-alpha name: manager diff --git a/controllers/azurestackhcicluster_controller.go b/controllers/azurestackhcicluster_controller.go index 59707a6a..83dfd331 100644 --- a/controllers/azurestackhcicluster_controller.go +++ b/controllers/azurestackhcicluster_controller.go @@ -192,6 +192,15 @@ func (r *AzureStackHCIClusterReconciler) reconcileDelete(clusterScope *scope.Clu } if len(azhciMachines) > 0 { + + err := r.deleteOrphanedMachines(clusterScope, azhciMachines) + if err != nil { + wrappedErr := errors.Wrapf(err, "failed to delete orphaned AzureStackHCIMachines part of AzureStackHCIClusters %s/%s", clusterScope.AzureStackHCICluster.Namespace, clusterScope.AzureStackHCICluster.Name) + r.Recorder.Eventf(azureStackHCICluster, corev1.EventTypeWarning, "FailureListMachinesInCluster", wrappedErr.Error()) + conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, clusterv1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + return reconcile.Result{}, wrappedErr + } + clusterScope.Info("Waiting for AzureStackHCIMachines to be deleted", "count", len(azhciMachines)) conditions.MarkFalse(azureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.AzureStackHCIMachinesDeletingReason, clusterv1.ConditionSeverityWarning, "") return reconcile.Result{RequeueAfter: 20 * time.Second}, nil @@ -230,6 +239,26 @@ func (r *AzureStackHCIClusterReconciler) reconcileDelete(clusterScope *scope.Clu return reconcile.Result{}, nil } +func (r *AzureStackHCIClusterReconciler) deleteOrphanedMachines(clusterScope *scope.ClusterScope, azhciMachines []*infrav1.AzureStackHCIMachine) error { + + for _, azhciMachine := range azhciMachines { + machine, err := util.GetOwnerMachine(clusterScope.Context, clusterScope.Client, azhciMachine.ObjectMeta) + if err != nil { + return err + } + if machine == nil { + clusterScope.Info("Deleting Orphaned Machine", "Name", azhciMachine.Name, "AzureStackHCICluster", clusterScope.AzureStackHCICluster.Name) + if err := r.Client.Delete(clusterScope.Context, azhciMachine); err != nil { + if !apierrors.IsNotFound(err) { + return errors.Wrapf(err, "Failed to delete AzureStackHCIMachine %s", azhciMachine) + } + } + } + } + + return nil +} + func (r *AzureStackHCIClusterReconciler) reconcileAzureStackHCILoadBalancer(clusterScope *scope.ClusterScope) (bool, error) { if clusterScope.AzureStackHCILoadBalancer() == nil { clusterScope.Info("Skipping load balancer reconciliation since AzureStackHCICluster.Spec.AzureStackHCILoadBalancer is nil")