Skip to content

Commit

Permalink
Delete Orphaned Machines (#149)
Browse files Browse the repository at this point in the history
* delete orphaned machines

* fiix pipeline

* up the tag
  • Loading branch information
zawachte authored Sep 8, 2021
1 parent 3650d51 commit 205d8ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions azure-pipelines-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager_image_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions controllers/azurestackhcicluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 205d8ce

Please sign in to comment.