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

🐛 delete port left over by err openstackmachine #1260

Merged
merged 1 commit into from
Jun 27, 2022
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
5 changes: 5 additions & 0 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func reconcileDelete(ctx context.Context, scope *scope.Scope, patchHelper *patch

clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)

if err = networkingService.DeletePorts(openStackCluster); err != nil {
handleUpdateOSCError(openStackCluster, errors.Errorf("failed to delete ports: %v", err))
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
return reconcile.Result{}, errors.Wrap(err, "failed to delete ports")
}

if openStackCluster.Spec.APIServerLoadBalancer.Enabled {
loadBalancerService, err := loadbalancer.NewService(scope)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions pkg/cloud/services/networking/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package networking

import (
"fmt"
"strings"
"time"

"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding"
Expand Down Expand Up @@ -243,6 +244,32 @@ func (s *Service) DeletePort(eventObject runtime.Object, portID string) error {
return nil
}

func (s *Service) DeletePorts(openStackCluster *infrav1.OpenStackCluster) error {
networkID := openStackCluster.Spec.Network.ID
portList, err := s.client.ListPort(ports.ListOpts{
NetworkID: networkID,
DeviceOwner: "",
})
if err != nil {
if capoerrors.IsNotFound(err) {
return nil
}
return fmt.Errorf("list ports of network %q: %v", networkID, err)
}

for _, port := range portList {
if strings.HasPrefix(port.Name, openStackCluster.Name) {
err := s.DeletePort(openStackCluster, port.ID)
if capoerrors.IsNotFound(err) {
continue
}
return fmt.Errorf("delete port %s of network %q failed : %v", port.ID, networkID, err)
}
}

return nil
}

func (s *Service) GarbageCollectErrorInstancesPort(eventObject runtime.Object, instanceName string) error {
portList, err := s.client.ListPort(ports.ListOpts{
Name: instanceName,
Expand Down