From 2af3519e7a90938ab84dd8bc4b382a8d269d0c16 Mon Sep 17 00:00:00 2001 From: Kartik Joshi Date: Mon, 10 Jun 2024 15:36:36 +0530 Subject: [PATCH] azure: Delete network interface synchronously Delete network interface synchronously if vm creation is failed Fixes: #1842 Signed-off-by: Kartik Joshi --- src/cloud-providers/azure/provider.go | 47 +++++++++++++-------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/cloud-providers/azure/provider.go b/src/cloud-providers/azure/provider.go index 440127699b..bd176a259f 100644 --- a/src/cloud-providers/azure/provider.go +++ b/src/cloud-providers/azure/provider.go @@ -201,7 +201,7 @@ func (p *azureProvider) CreateInstance(ctx context.Context, podName, sandboxID s if err := p.deleteDisk(context.Background(), diskName); err != nil { logger.Printf("deleting disk (%s): %s", diskName, err) } - if err := p.deleteNetworkInterfaceAsync(context.Background(), nicName); err != nil { + if err := p.deleteNetworkInterface(context.Background(), nicName); err != nil { logger.Printf("deleting nic async (%s): %s", nicName, err) } return nil, fmt.Errorf("Creating instance (%v): %s", result, err) @@ -274,7 +274,7 @@ func (p *azureProvider) deleteDisk(ctx context.Context, diskName string) error { return nil } -func (p *azureProvider) deleteNetworkInterfaceAsync(ctx context.Context, nicName string) error { +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) @@ -282,31 +282,30 @@ func (p *azureProvider) deleteNetworkInterfaceAsync(ctx context.Context, nicName rg := p.serviceConfig.ResourceGroupName // retry with exponential backoff - go func() { - err := retry.Do(func() error { - pollerResponse, err := nicClient.BeginDelete(ctx, rg, 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) - } - return nil - }, - retry.Context(ctx), - retry.Attempts(4), - retry.Delay(180*time.Second), - retry.MaxDelay(180*time.Second), - retry.LastErrorOnly(true), - ) + err = retry.Do(func() error { + pollerResponse, err := nicClient.BeginDelete(ctx, rg, nicName, nil) + if err != nil { + return fmt.Errorf("beginning network interface deletion: %w", err) + } + _, err = pollerResponse.PollUntilDone(ctx, nil) if err != nil { - logger.Printf("deleting network interface in background (%s): %s", nicName, err) - } else { - logger.Printf("successfully deleted nic (%s) in background", nicName) + return fmt.Errorf("waiting for network interface deletion: %w", err) } - }() + return nil + }, + retry.Context(ctx), + retry.Attempts(4), + retry.Delay(180*time.Second), + retry.MaxDelay(180*time.Second), + retry.LastErrorOnly(true), + ) + + if err != nil { + logger.Printf("deleting network interface (%s): %s", nicName, err) + return err + } + logger.Printf("successfully deleted nic (%s)", nicName) return nil }