|
| 1 | +package postdestroy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/gophercloud/gophercloud/v2" |
| 10 | + "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + |
| 13 | + openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults" |
| 14 | +) |
| 15 | + |
| 16 | +// FloatingIPs deletes the bootstrap floating IP that was previously disassociated |
| 17 | +// (but not deleted) when the bootstrap machine was destroyed by CAPO. |
| 18 | +func FloatingIPs(ctx context.Context, cloud string, infraID string) error { |
| 19 | + clusterTag := fmt.Sprintf("openshiftClusterID=%s", infraID) |
| 20 | + roleTag := "openshiftRole=bootstrap" |
| 21 | + logrus.Debugf("Searching for bootstrap floating IP with tags: %s, %s", clusterTag, roleTag) |
| 22 | + |
| 23 | + networkClient, err := openstackdefaults.NewServiceClient(ctx, "network", openstackdefaults.DefaultClientOpts(cloud)) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("failed to create network client: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + // Search for floating IPs with both cluster ID and role tags |
| 29 | + // OpenStack tags filter requires ALL specified tags to match (AND logic) |
| 30 | + allPages, err := floatingips.List(networkClient, floatingips.ListOpts{ |
| 31 | + Tags: fmt.Sprintf("%s,%s", clusterTag, roleTag), |
| 32 | + }).AllPages(ctx) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to list floating IPs: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + fips, err := floatingips.ExtractFloatingIPs(allPages) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to extract floating IPs: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + if len(fips) == 0 { |
| 43 | + logrus.Debug("No bootstrap floating IP found (may have already been deleted)") |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + // Should only find one FIP with both tags, but delete all if multiple exist |
| 48 | + var errs []error |
| 49 | + for _, fip := range fips { |
| 50 | + logrus.Infof("Deleting bootstrap floating IP %s (ID: %s)", fip.FloatingIP, fip.ID) |
| 51 | + |
| 52 | + err = floatingips.Delete(ctx, networkClient, fip.ID).ExtractErr() |
| 53 | + if err != nil { |
| 54 | + // Check if it's a "not found" error, which is acceptable |
| 55 | + if gophercloud.ResponseCodeIs(err, http.StatusNotFound) { |
| 56 | + logrus.Debugf("Bootstrap floating IP %s already deleted", fip.ID) |
| 57 | + continue |
| 58 | + } |
| 59 | + logrus.Errorf("Failed to delete bootstrap floating IP %s: %v", fip.FloatingIP, err) |
| 60 | + errs = append(errs, fmt.Errorf("failed to delete bootstrap floating IP %s: %w", fip.FloatingIP, err)) |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + logrus.Infof("Successfully deleted bootstrap floating IP %s", fip.FloatingIP) |
| 65 | + } |
| 66 | + |
| 67 | + return errors.Join(errs...) |
| 68 | +} |
0 commit comments