Skip to content

Commit

Permalink
azure: Delete network interface synchronously
Browse files Browse the repository at this point in the history
Delete network interface synchronously if vm creation is failed

Fixes: #1842
Signed-off-by: Kartik Joshi <[email protected]>
  • Loading branch information
kartikjoshi21 committed Jul 5, 2024
1 parent 1308eda commit 55905cd
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/cloud-providers/azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -274,39 +274,38 @@ 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)
}
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
}

Expand Down

0 comments on commit 55905cd

Please sign in to comment.