Skip to content

Commit

Permalink
azure: fix cleaning up resources
Browse files Browse the repository at this point in the history
nic and disk were not getting cleaned up when instance was deleted.

Fixes: #612
Signed-off-by: Kautilya Tripathi <ktripathi@microsoft.com>
  • Loading branch information
knrt10 committed Feb 21, 2023
1 parent 5d18b67 commit c9af529
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions pkg/adaptor/cloud/azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"net"
"os"
"regexp"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
Expand All @@ -24,6 +25,7 @@ import (

var logger = log.New(log.Writer(), "[adaptor/cloud/azure] ", log.LstdFlags|log.Lmsgprefix)
var errNotReady = errors.New("address not ready")
var errNotFound = errors.New("VM name not found")

const (
maxInstanceNameLen = 63
Expand Down Expand Up @@ -251,7 +253,17 @@ func (p *azureProvider) DeleteInstance(ctx context.Context, instanceID string) e
return fmt.Errorf("creating VM client: %w", err)
}

pollerResponse, err := vmClient.BeginDelete(ctx, p.serviceConfig.ResourceGroupName, instanceID, nil)
// instanceID in the form of /subscriptions/<subID>/resourceGroups/<resource_name>/providers/Microsoft.Compute/virtualMachines/<VM_Name>.
re := regexp.MustCompile(`^/subscriptions/[^/]+/resourceGroups/[^/]+/providers/Microsoft\.Compute/virtualMachines/(.*)$`)
match := re.FindStringSubmatch(instanceID)
if len(match) < 1 {
logger.Print("finding VM name using regexp:", match)
return errNotFound
}

vmName := match[1]

pollerResponse, err := vmClient.BeginDelete(ctx, p.serviceConfig.ResourceGroupName, vmName, nil)
if err != nil {
return fmt.Errorf("beginning VM deletion: %w", err)
}
Expand All @@ -260,7 +272,63 @@ func (p *azureProvider) DeleteInstance(ctx context.Context, instanceID string) e
return fmt.Errorf("waiting for the VM deletion: %w", err)
}

logger.Printf("deleted VM successfully: %s", instanceID)
logger.Printf("deleted VM successfully: %s", vmName)

diskName := fmt.Sprintf("%s-disk", vmName)
if err := p.deleteDisk(ctx, diskName); err != nil {
err = fmt.Errorf("deleting disk: %w", err)
logger.Print(err)
return err
}

nicName := fmt.Sprintf("%s-net", vmName)
if err := p.deleteNetworkInterface(ctx, nicName); err != nil {
err = fmt.Errorf("deleting network interface: %w", err)
logger.Print(err)
return err
}

return nil
}

func (p *azureProvider) deleteDisk(ctx context.Context, diskName string) error {
diskClient, err := armcompute.NewDisksClient(p.serviceConfig.SubscriptionId, p.azureClient, nil)
if err != nil {
return fmt.Errorf("creating disk client: %w", err)
}

pollerResponse, err := diskClient.BeginDelete(ctx, p.serviceConfig.ResourceGroupName, diskName, nil)
if err != nil {
return fmt.Errorf("beginning disk deletion: %w", err)
}

_, err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return fmt.Errorf("waiting for the disk deletion: %w", err)
}

logger.Printf("deleted disk successfully: %s", diskName)

return nil
}

func (p *azureProvider) deleteNetworkInterface(ctx context.Context, nicName string) error {
nicClient, err := armnetwork.NewInterfacesClient(p.serviceConfig.SubscriptionId, p.azureClient, nil)
if err != nil {
return fmt.Errorf("creating network interface client: %w", err)
}

pollerResponse, err := nicClient.BeginDelete(ctx, p.serviceConfig.ResourceGroupName, nicName, nil)
if err != nil {
return fmt.Errorf("beginning network interface deletion: %w", err)
}

_, err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return fmt.Errorf("waiting for network interface deletion: %w", err)
}

logger.Printf("deleted network interface successfully: %s", nicName)

return nil
}
Expand Down

0 comments on commit c9af529

Please sign in to comment.