Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gc for error instance's port #975

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 32 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,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 {
Expand Down