Skip to content

Commit

Permalink
Add delete port of error instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo0km4n committed Aug 21, 2021
1 parent 6ef256a commit 0b8680a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 25 additions & 4 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -685,7 +706,7 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instance *InstanceI
}
}

return s.deleteInstance(eventObject, instance)
return s.deleteInstance(eventObject, instanceIdentifier)
}

func (s *Service) deletePort(eventObject runtime.Object, portID string) error {
Expand Down

0 comments on commit 0b8680a

Please sign in to comment.