Skip to content

Commit

Permalink
update correlation id of virtual machine CRs when create/delete loadb…
Browse files Browse the repository at this point in the history
…alancer (#238)

Co-authored-by: peidlu <[email protected]>
  • Loading branch information
lpdnju and lpdnju authored Oct 27, 2023
1 parent 26d9955 commit cbf8502
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions controllers/azurestackhciloadbalancer_virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud"
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope"
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/telemetry"
infrav1util "github.com/microsoft/cluster-api-provider-azurestackhci/pkg/util"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -130,6 +131,13 @@ func (r *AzureStackHCILoadBalancerReconciler) reconcileDeleteVirtualMachines(loa

for _, vm := range vmList {
if vm.GetDeletionTimestamp().IsZero() {
// update correlationId before deletion
infrav1util.CopyCorrelationId(loadBalancerScope.AzureStackHCILoadBalancer, vm)
if err := r.Client.Update(clusterScope.Context, vm); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to update AzureStackHCIVirtualMachine %s", vm.Name)
}
}
err := r.Client.Delete(clusterScope.Context, vm)
telemetry.RecordHybridAKSCRDChange(
clusterScope.GetLogger(),
Expand Down Expand Up @@ -198,6 +206,7 @@ func (r *AzureStackHCILoadBalancerReconciler) createOrUpdateVirtualMachine(loadB
return errors.Wrap(err, "failed to get AzureStackHCILoadBalancer image")
}
image.DeepCopyInto(&vm.Spec.Image)
infrav1util.CopyCorrelationId(loadBalancerScope.AzureStackHCILoadBalancer, vm)

return nil
}
Expand All @@ -221,8 +230,15 @@ func (r *AzureStackHCILoadBalancerReconciler) createOrUpdateVirtualMachine(loadB
}

// deleteVirtualMachine deletes a virtual machine
func (r *AzureStackHCILoadBalancerReconciler) deleteVirtualMachine(clusterScope *scope.ClusterScope, vm *infrav1.AzureStackHCIVirtualMachine) error {
func (r *AzureStackHCILoadBalancerReconciler) deleteVirtualMachine(lbs *scope.LoadBalancerScope, clusterScope *scope.ClusterScope, vm *infrav1.AzureStackHCIVirtualMachine) error {
if vm.GetDeletionTimestamp().IsZero() {
// update correlationId before deletion
infrav1util.CopyCorrelationId(lbs.AzureStackHCILoadBalancer, vm)
if err := r.Client.Update(clusterScope.Context, vm); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to update AzureStackHCIVirtualMachine %s", vm.Name)
}
}
err := r.Client.Delete(clusterScope.Context, vm)
telemetry.RecordHybridAKSCRDChange(
clusterScope.GetLogger(),
Expand Down Expand Up @@ -263,7 +279,7 @@ func (r *AzureStackHCILoadBalancerReconciler) scaleDownVirtualMachines(lbs *scop
return err
}

err = r.deleteVirtualMachine(clusterScope, vm)
err = r.deleteVirtualMachine(lbs, clusterScope, vm)
if err != nil {
return errors.Wrapf(err, "failed to scale down loadbalancer VM %s", vm.Name)
}
Expand Down

0 comments on commit cbf8502

Please sign in to comment.