From 88e157d4548ff675a8e95cbd7bc0dbb759af149e Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Wed, 3 Aug 2022 15:17:38 +0530 Subject: [PATCH] azure: Add VM deletion code - Delete instance. - Delete disk. - Delete NIC. Fixes: #120 Signed-off-by: Suraj Deshmukh --- pkg/adaptor/hypervisor/azure/azure.go | 18 ++++++- pkg/adaptor/hypervisor/azure/service.go | 71 ++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/pkg/adaptor/hypervisor/azure/azure.go b/pkg/adaptor/hypervisor/azure/azure.go index 7eafa8f56..855554ea6 100644 --- a/pkg/adaptor/hypervisor/azure/azure.go +++ b/pkg/adaptor/hypervisor/azure/azure.go @@ -34,7 +34,23 @@ func CreateInstance(ctx context.Context, s *hypervisorService, parameters *armco return &resp.VirtualMachine, nil } -func DeleteInstance(c context.Context, cred azcore.TokenCredential, id string) error { +func DeleteInstance(ctx context.Context, s *hypervisorService, vmName string) error { + vmClient, err := armcompute.NewVirtualMachinesClient(s.serviceConfig.SubscriptionId, s.azureClient, nil) + if err != nil { + return fmt.Errorf("creating VM client: %w", err) + } + + pollerResponse, err := vmClient.BeginDelete(ctx, s.serviceConfig.ResourceGroupName, vmName, nil) + if err != nil { + return fmt.Errorf("beginning VM deletion: %w", err) + } + + if _, err = pollerResponse.PollUntilDone(ctx, nil); err != nil { + return fmt.Errorf("waiting for the VM deletion: %w", err) + } + + logger.Printf("deleted VM successfully: %s", vmName) + return nil } diff --git a/pkg/adaptor/hypervisor/azure/service.go b/pkg/adaptor/hypervisor/azure/service.go index 4b018a7b4..21c2ee315 100644 --- a/pkg/adaptor/hypervisor/azure/service.go +++ b/pkg/adaptor/hypervisor/azure/service.go @@ -83,6 +83,7 @@ type sandbox struct { netNSPath string podDirPath string vsi string + vmName string agentProxy proxy.AgentProxy podNetworkConfig *tunneler.Config } @@ -260,6 +261,7 @@ func (s *hypervisorService) StartVM(ctx context.Context, req *pb.StartVMRequest) // Set vsi to instance id sandbox.vsi = *vm.ID + sandbox.vmName = vmName logger.Printf("created an instance %s for sandbox %s", *vm.Name, req.Id) @@ -332,15 +334,30 @@ func getIPs(nic *armnetwork.Interface) ([]net.IP, error) { return podNodeIPs, nil } -func (s *hypervisorService) deleteInstance(ctx context.Context, id string) error { +func (s *hypervisorService) deleteInstance(ctx context.Context, vmName string) error { - err := DeleteInstance(ctx, s.azureClient, id) + if err := DeleteInstance(ctx, s, vmName); err != nil { + err = fmt.Errorf("failed to delete an instance: %w", err) + logger.Printf("%v", err) + return err + } - if err != nil { - logger.Printf("failed to delete an instance: %v", err) + logger.Printf("deleted an instance %s", vmName) + + diskName := fmt.Sprintf("%s-disk", vmName) + if err := s.deleteDisk(ctx, diskName); err != nil { + err = fmt.Errorf("failed to delete disk: %w", err) + logger.Print(err) + return err + } + + nicName := fmt.Sprintf("%s-net", vmName) + if err := s.deleteNetworkInterface(ctx, nicName); err != nil { + err = fmt.Errorf("failed to delete network interface: %w", err) + logger.Print(err) return err } - logger.Printf("deleted an instance %s", id) + return nil } @@ -354,7 +371,7 @@ func (s *hypervisorService) StopVM(ctx context.Context, req *pb.StopVMRequest) ( logger.Printf("failed to stop agent proxy: %v", err) } - if err := s.deleteInstance(ctx, sandbox.vsi); err != nil { + if err := s.deleteInstance(ctx, sandbox.vmName); err != nil { return nil, err } @@ -403,3 +420,45 @@ func (s *hypervisorService) createNetworkInterface(ctx context.Context, nicName return &resp.Interface, nil } + +func (s *hypervisorService) deleteNetworkInterface(ctx context.Context, nicName string) error { + nicClient, err := armnetwork.NewInterfacesClient(s.serviceConfig.SubscriptionId, s.azureClient, nil) + if err != nil { + return fmt.Errorf("creating network interfaces client: %w", err) + } + + pollerResponse, err := nicClient.BeginDelete(ctx, s.serviceConfig.ResourceGroupName, nicName, nil) + if err != nil { + return fmt.Errorf("beginning deletion of network interface: %w", err) + } + + _, err = pollerResponse.PollUntilDone(ctx, nil) + if err != nil { + return fmt.Errorf("polling network interface deletion: %w", err) + } + + logger.Printf("deleted network interface successfully: %s", nicName) + + return nil +} + +func (s *hypervisorService) deleteDisk(ctx context.Context, diskName string) error { + diskClient, err := armcompute.NewDisksClient(s.serviceConfig.SubscriptionId, s.azureClient, nil) + if err != nil { + return fmt.Errorf("creating disk client: %w", err) + } + + pollerResponse, err := diskClient.BeginDelete(ctx, s.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 +}