Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize VM Updates, Fix OSDisk name and extraction of VM name from DataDisk #117

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.21
require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.0-beta.2
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0-beta.2
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0-beta.3
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0-beta.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.2
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0-beta.3
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
github.com/gardener/machine-controller-manager v0.50.1
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.4
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.0 h1:xZaKaIde2fNBQ6c
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.0/go.mod h1:veBEHsnzafwWVNkJ71OdC2tqipKKauNabOxdoi05Mhk=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0 h1:d81/ng9rET2YqdVkVwkb6EXeRrLJIwyGnJcAlAWKwhs=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0/go.mod h1:s4kgfzA0covAXNicZHDMN58jExvcng2mC/DepXiF1EI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0-beta.2 h1:RBfZY7RzRdkxUkjjKPT25FmqAp/oSFRb9V/57q9wqNw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0-beta.2/go.mod h1:lnOBBuKriNkBc3Bybhmcnszj/qBliLx+7Oisguurg9I=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2/go.mod h1:FbdwsQ2EzwvXxOPcMFYO8ogEc9uMMIj3YkmCdXdAFmk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0 h1:qgs/VAMSR+9qFhwTw4OwF2NbVuw+2m83pVZJjqkKQMw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.3.0/go.mod h1:uYt4CfhkJA9o0FN7jfE5minm/i4nUE4MjGUJkzB6Zs8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFGRSlMKCQelWwxUyYVEUqseBJVemLyqWJjvMyt0do=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0/go.mod h1:LRr2FzBTQlONPPa5HREE5+RjSCTXl7BwOvYOaWTqCaI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0 h1:pPvTJ1dY0sA35JOeFq6TsY2xj6Z85Yo23Pj4wCCvu4o=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0/go.mod h1:mLfWfj8v3jfWKsL9G4eoBoXVcsqcIUTapmdKy7uGOp0=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0-beta.3 h1:Ck4bBDdwvw09rctYAIFjsNdGPBvH2J65CtCdEKC/AiM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0-beta.3/go.mod h1:C/6PihoAZ9Jihn1VCOJzVnC7sqonTFeOnJsrFsoxRB0=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0-beta.1 h1:z6AuIOPwm8xSIaoJ3nE5ysZ0hfbDP+uHpFvX6VvGv5E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0-beta.1/go.mod h1:6mvQPUvc5ySNuk0etF/MV1f76u2Ja5Eb/Q8KjdxG63U=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0 h1:GdgOqiWT+9hidDxPqrugVKp0D7urLYs3/yvgYPvvSBQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/marketplaceordering/armmarketplaceordering v1.2.0/go.mod h1:HfaiNTisneFxZGjRZOUk18BgHc23ccL4/cV7k+31tVw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0 h1:bXwSugBiSbgtz7rOtbfGf+woewp4f06orW9OP5BjHLA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0/go.mod h1:Y/HgrePTmGy9HjdSGTqZNa+apUpTVIEVKXJyARP2lrk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.2 h1:f9lam+D19V0TDn17+aFhrVhWPpfsF5zaGHeqDGJZAVc=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.2/go.mod h1:29c9+gYpdWhyC4TPANZBPlgoWllMDhguL2AIByPYQtk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0-beta.3 h1:C2ok9YgfllIRAJt1DteTWAnEXi1cywc6tBFP8OwiCTg=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0-beta.3/go.mod h1:tN8Aon9i+H4GMuEF2LAr8F2nMgFLev0gsrzi9hM5vEA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0 h1:Dd+RhdJn0OTtVGaeDLZpcumkIVCtA/3/Fo42+eoYvVM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0/go.mod h1:5kakwfW5CjC9KK+Q4wjXAg+ShuIm2mBMua0ZFj2C8PE=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1 h1:WpB/QDNLpMw72xHJc34BNNykqSOeEJDAWkhf0u12/Jk=
Expand Down
112 changes: 4 additions & 108 deletions pkg/azure/access/helpers/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ import (
"context"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"k8s.io/klog/v2"

"github.com/gardener/machine-controller-manager-provider-azure/pkg/azure/access/errors"
"github.com/gardener/machine-controller-manager-provider-azure/pkg/azure/instrument"
"github.com/gardener/machine-controller-manager-provider-azure/pkg/azure/utils"
)

// labels used for recording prometheus metrics
Expand Down Expand Up @@ -104,119 +100,19 @@ func CreateVirtualMachine(ctx context.Context, vmAccess *armcompute.VirtualMachi

// SetCascadeDeleteForNICsAndDisks sets cascade deletion for NICs and Disks (OSDisk and DataDisks) associated to passed virtual machine.
// NOTE: All calls to this Azure API are instrumented as prometheus metric.
func SetCascadeDeleteForNICsAndDisks(ctx context.Context, vmClient *armcompute.VirtualMachinesClient, resourceGroup string, vm *armcompute.VirtualMachine) (err error) {
func SetCascadeDeleteForNICsAndDisks(ctx context.Context, vmClient *armcompute.VirtualMachinesClient, resourceGroup string, vmName string, vmUpdateParams *armcompute.VirtualMachineUpdate) (err error) {
defer instrument.RecordAzAPIMetric(err, vmUpdateServiceLabel, time.Now())
vmUpdateDesc := createVirtualMachineUpdateDescription(vm) // TODO: Rename this method it returns a VM not "params"
if vmUpdateDesc == nil {
klog.Infof("All configured NICs, OSDisk and DataDisks have cascade delete already set. Skipping update of VM: [ResourceGroup: %s, Name: %s]", resourceGroup, *vm.Name)
return
}
updCtx, cancelFn := context.WithTimeout(ctx, defaultUpdateVMTimeout)
defer cancelFn()
poller, err := vmClient.BeginUpdate(updCtx, resourceGroup, *vm.Name, *vmUpdateDesc, nil)
poller, err := vmClient.BeginUpdate(updCtx, resourceGroup, vmName, *vmUpdateParams, nil)
if err != nil {
errors.LogAzAPIError(err, "Failed to trigger update of VM [ResourceGroup: %s, VMName: %s]", resourceGroup, *vm.Name)
errors.LogAzAPIError(err, "Failed to trigger update of VM [ResourceGroup: %s, VMName: %s]", resourceGroup, vmName)
return
}
_, err = poller.PollUntilDone(updCtx, nil)
if err != nil {
errors.LogAzAPIError(err, "Polling failed while waiting for update of VM: %s for ResourceGroup: %s", *vm.Name, resourceGroup)
errors.LogAzAPIError(err, "Polling failed while waiting for update of VM: %s for ResourceGroup: %s", vmName, resourceGroup)
return
}

return
}

// createVirtualMachineUpdateDescription creates armcompute.VirtualMachineUpdate with delta changes to
// delete option for associated NICs and Disks of a given virtual machine.
func createVirtualMachineUpdateDescription(vm *armcompute.VirtualMachine) *armcompute.VirtualMachineUpdate {
var (
vmUpdateParams = armcompute.VirtualMachineUpdate{Properties: &armcompute.VirtualMachineProperties{}}
cascadeDeleteChangesPending bool
)

updatedNicRefs := getNetworkInterfaceReferencesToUpdate(vm.Properties.NetworkProfile)
if !utils.IsSliceNilOrEmpty(updatedNicRefs) {
cascadeDeleteChangesPending = true
vmUpdateParams.Properties.NetworkProfile = &armcompute.NetworkProfile{
NetworkInterfaces: updatedNicRefs,
}
}

vmUpdateParams.Properties.StorageProfile = &armcompute.StorageProfile{}
updatedOSDisk := getOSDiskToUpdate(vm.Properties.StorageProfile)
if updatedOSDisk != nil {
cascadeDeleteChangesPending = true
vmUpdateParams.Properties.StorageProfile.OSDisk = updatedOSDisk
}

updatedDataDisks := getDataDisksToUpdate(vm.Properties.StorageProfile)
if !utils.IsSliceNilOrEmpty(updatedDataDisks) {
cascadeDeleteChangesPending = true
vmUpdateParams.Properties.StorageProfile.DataDisks = updatedDataDisks
}

if !cascadeDeleteChangesPending {
return nil
}
return &vmUpdateParams
}

func getNetworkInterfaceReferencesToUpdate(networkProfile *armcompute.NetworkProfile) []*armcompute.NetworkInterfaceReference {
if networkProfile == nil || utils.IsSliceNilOrEmpty(networkProfile.NetworkInterfaces) {
return nil
}
updatedNicRefs := make([]*armcompute.NetworkInterfaceReference, 0, len(networkProfile.NetworkInterfaces))
for _, nicRef := range networkProfile.NetworkInterfaces {
updatedNicRef := &armcompute.NetworkInterfaceReference{ID: nicRef.ID}
if !isNicCascadeDeleteSet(nicRef) {
if updatedNicRef.Properties == nil {
updatedNicRef.Properties = &armcompute.NetworkInterfaceReferenceProperties{}
}
updatedNicRef.Properties.DeleteOption = to.Ptr(armcompute.DeleteOptionsDelete)
updatedNicRefs = append(updatedNicRefs, updatedNicRef)
}
}
return updatedNicRefs
}

func isNicCascadeDeleteSet(nicRef *armcompute.NetworkInterfaceReference) bool {
if nicRef.Properties == nil {
return false
}
deleteOption := nicRef.Properties.DeleteOption
return deleteOption != nil && *deleteOption == armcompute.DeleteOptionsDelete
}

func getOSDiskToUpdate(storageProfile *armcompute.StorageProfile) *armcompute.OSDisk {
var updatedOSDisk *armcompute.OSDisk
if storageProfile != nil && storageProfile.OSDisk != nil {
existingOSDisk := storageProfile.OSDisk
existingDeleteOption := existingOSDisk.DeleteOption
if existingDeleteOption == nil || *existingDeleteOption != armcompute.DiskDeleteOptionTypesDelete {
updatedOSDisk = &armcompute.OSDisk{
Name: existingOSDisk.Name,
DeleteOption: to.Ptr(armcompute.DiskDeleteOptionTypesDelete),
}
}
}
return updatedOSDisk
}

func getDataDisksToUpdate(storageProfile *armcompute.StorageProfile) []*armcompute.DataDisk {
var updatedDataDisks []*armcompute.DataDisk
if storageProfile != nil && !utils.IsSliceNilOrEmpty(storageProfile.DataDisks) {
updatedDataDisks = make([]*armcompute.DataDisk, 0, len(storageProfile.DataDisks))
for _, dataDisk := range storageProfile.DataDisks {
if dataDisk.DeleteOption == nil || *dataDisk.DeleteOption != armcompute.DiskDeleteOptionTypesDelete {
updatedDataDisk := &armcompute.DataDisk{
Lun: dataDisk.Lun,
DeleteOption: to.Ptr(armcompute.DiskDeleteOptionTypesDelete),
Name: dataDisk.Name,
}
updatedDataDisks = append(updatedDataDisks, updatedDataDisk)
}
}
}
return updatedDataDisks
}
Loading