From d05a5ee60f3ad836932869eba39bbae455ab0a70 Mon Sep 17 00:00:00 2001 From: Bo0km4n Date: Sun, 22 Aug 2021 00:28:40 +0900 Subject: [PATCH] Add delete port of error instance --- controllers/openstackcluster_controller.go | 2 +- controllers/openstackmachine_controller.go | 2 +- pkg/cloud/services/compute/instance.go | 36 +++++++++++++++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/controllers/openstackcluster_controller.go b/controllers/openstackcluster_controller.go index b0cced0c76..24104e3bf1 100644 --- a/controllers/openstackcluster_controller.go +++ b/controllers/openstackcluster_controller.go @@ -219,7 +219,7 @@ func deleteBastion(log logr.Logger, osProviderClient *gophercloud.ProviderClient } } - if err = computeService.DeleteInstance(openStackCluster, instanceStatus.InstanceIdentifier()); err != nil { + if err = computeService.DeleteInstance(openStackCluster, instanceStatus); err != nil { handleUpdateOSCError(openStackCluster, errors.Errorf("failed to delete bastion: %v", err)) return errors.Errorf("failed to delete bastion: %v", err) } diff --git a/controllers/openstackmachine_controller.go b/controllers/openstackmachine_controller.go index 5dd2eb52d1..88e90d0e97 100644 --- a/controllers/openstackmachine_controller.go +++ b/controllers/openstackmachine_controller.go @@ -236,7 +236,7 @@ func (r *OpenStackMachineReconciler) reconcileDelete(ctx context.Context, logger return ctrl.Result{}, nil } - if err = computeService.DeleteInstance(openStackMachine, instanceStatus.InstanceIdentifier()); err != nil { + if err = computeService.DeleteInstance(openStackMachine, instanceStatus); err != nil { handleUpdateMachineError(logger, openStackMachine, errors.Errorf("error deleting OpenStack instance %s with ID %s: %v", instanceStatus.Name(), instanceStatus.ID(), err)) return ctrl.Result{}, nil } diff --git a/pkg/cloud/services/compute/instance.go b/pkg/cloud/services/compute/instance.go index 1adcf1f263..05ee883db0 100644 --- a/pkg/cloud/services/compute/instance.go +++ b/pkg/cloud/services/compute/instance.go @@ -653,9 +653,30 @@ func (s *Service) GetManagementPort(instanceStatus *InstanceStatus) (*ports.Port return &allPorts[0], nil } -func (s *Service) DeleteInstance(eventObject runtime.Object, instance *InstanceIdentifier) error { +func (s *Service) garbageCollectErrorInstancesPort(eventObject runtime.Object, instance *InstanceStatus) error { + pager, err := ports.List(s.networkClient, ports.ListOpts{ + Name: instance.Name(), + }).AllPages() + if err != nil { + return err + } + portList, err := ports.ExtractPorts(pager) + if err != nil { + return err + } + for _, p := range portList { + if err := s.deletePort(eventObject, p.ID); err != nil { + return err + } + } + + return nil +} + +func (s *Service) DeleteInstance(eventObject runtime.Object, instance *InstanceStatus) error { mc := metrics.NewMetricPrometheusContext("server_os_interface", "list") - allInterfaces, err := attachinterfaces.List(s.computeClient, instance.ID).AllPages() + instanceIdentifier := instance.InstanceIdentifier() + allInterfaces, err := attachinterfaces.List(s.computeClient, instanceIdentifier.ID).AllPages() if mc.ObserveRequest(err) != nil { return err } @@ -670,7 +691,7 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instance *InstanceI } // get and delete trunks for _, port := range instanceInterfaces { - if err = s.deleteAttachInterface(eventObject, instance, port.PortID); err != nil { + if err = s.deleteAttachInterface(eventObject, instanceIdentifier, port.PortID); err != nil { return err } @@ -685,7 +706,14 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instance *InstanceI } } - return s.deleteInstance(eventObject, instance) + // delete port of error instance + if instance.State() == infrav1.InstanceStateError { + if err := s.garbageCollectErrorInstancesPort(eventObject, instance); err != nil { + return err + } + } + + return s.deleteInstance(eventObject, instanceIdentifier) } func (s *Service) deletePort(eventObject runtime.Object, portID string) error {