Skip to content

Commit

Permalink
Deal with terminal state vm in deleteMachine (#120)
Browse files Browse the repository at this point in the history
* deal with terminal state vm in deleteMachine

* avoid case sensitive failures

* skip disk detachment for terminating vm, print more logs

* cleanup .env file pushed by mistake earlier

* defualted variables in makefile

* introduced some newlines in Makefile

* run make check

* Update pkg/azure/utils.go

Co-authored-by: Rishabh Patel <[email protected]>

---------

Co-authored-by: Rishabh Patel <[email protected]>
  • Loading branch information
himanshu-kun and rishabh-11 authored Dec 21, 2023
1 parent d7c9959 commit 659e4ef
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/azure/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ const (
nicDeletionTimeout = 10 * time.Minute
)

// constant for provisioning states
const (
// provisioningStateFailed is the provisioning state of the VM set by the provider indicating that the VM is in terminal state.
provisioningStateFailed = "Failed"
// provisioningStateDeleting is the provisioning state of the VM set by the provider indicating that the VM is being deleted
provisioningStateDeleting = "Deleting"
)

func dependencyNameFromVMName(vmName, suffix string) string {
return vmName + suffix
}
Expand Down Expand Up @@ -595,12 +603,18 @@ func (d *MachinePlugin) createVMNicDisk(req *driver.CreateMachineRequest) (*comp
// deleteVMNicDisks deletes the VM and associated Disks and NIC
func (d *MachinePlugin) deleteVMNicDisks(ctx context.Context, clients spi.AzureDriverClientsInterface, resourceGroupName string, VMName string, nicName string, diskName string, dataDiskNames []string) error {

// We try to fetch the VM, detach its data disks and finally delete it
// We try to fetch the VM, detach its data disks(if VM is not in terminal state) and finally delete it
if vm, vmErr := clients.GetVM().Get(ctx, resourceGroupName, VMName, ""); vmErr == nil {

if detachmentErr := waitForDataDiskDetachment(ctx, clients, resourceGroupName, vm); detachmentErr != nil {
return detachmentErr
if isVirtualMachineInTerminalState(vm) {
klog.V(2).Infof("VM %s is in terminal state.. Proceeding with deletion of VM", VMName)
} else if !isVirtualMachineMarkedForDeletion(vm) {
klog.V(2).Infof("VM %s is neither in terminal state nor marked for deletion. Proceeding with disk detachments..", VMName)
if detachmentErr := waitForDataDiskDetachment(ctx, clients, resourceGroupName, vm); detachmentErr != nil {
return detachmentErr
}
}

if deleteErr := DeleteVM(ctx, clients, resourceGroupName, VMName); deleteErr != nil {
return deleteErr
}
Expand Down Expand Up @@ -756,6 +770,16 @@ func deleteDisk(ctx context.Context, clients spi.AzureDriverClientsInterface, re
return nil
}

// isVirtualMachineInTerminalState checks if the provisioningState of the VM is set to Failed.
func isVirtualMachineInTerminalState(vm compute.VirtualMachine) bool {
return vm.ProvisioningState != nil && (strings.ToLower(*vm.ProvisioningState) == strings.ToLower(provisioningStateFailed))
}

// isVirtualMachineMarkedForDeletion checks if the provisioningState of the VM is set to Deleting.
func isVirtualMachineMarkedForDeletion(vm compute.VirtualMachine) bool {
return vm.ProvisioningState != nil && (strings.ToLower(*vm.ProvisioningState) == strings.ToLower(provisioningStateDeleting))
}

// GetDeleterForDisk executes the deletion of the attached disk
func GetDeleterForDisk(ctx context.Context, clients spi.AzureDriverClientsInterface, resourceGroupName string, diskName string) func() error {
return func() error {
Expand Down

0 comments on commit 659e4ef

Please sign in to comment.