|
| 1 | +package postprovision |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/gophercloud/gophercloud" |
| 8 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags" |
| 9 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" |
| 10 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 11 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" |
| 12 | + capo "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + |
| 15 | + "github.com/openshift/installer/pkg/asset/installconfig" |
| 16 | + "github.com/openshift/installer/pkg/asset/manifests/capiutils" |
| 17 | + openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults" |
| 18 | +) |
| 19 | + |
| 20 | +// FloatingIPs creates and attaches a Floating IP to the Bootstrap Machine. |
| 21 | +func FloatingIPs(ctx context.Context, c client.Client, cluster *capo.OpenStackCluster, installConfig *installconfig.InstallConfig, infraID string) error { |
| 22 | + bootstrapMachine := &capo.OpenStackMachine{} |
| 23 | + key := client.ObjectKey{ |
| 24 | + Name: capiutils.GenerateBoostrapMachineName(infraID), |
| 25 | + Namespace: capiutils.Namespace, |
| 26 | + } |
| 27 | + if err := c.Get(ctx, key, bootstrapMachine); err != nil { |
| 28 | + return fmt.Errorf("failed to get bootstrap Machine: %w", err) |
| 29 | + } |
| 30 | + |
| 31 | + networkClient, err := openstackdefaults.NewServiceClient("network", openstackdefaults.DefaultClientOpts(installConfig.Config.Platform.OpenStack.Cloud)) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + bootstrapPort, err := getPortForInstance(networkClient, *bootstrapMachine.Spec.InstanceID) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + network, err := getNetworkByName(networkClient, cluster.Spec.ExternalNetworkID) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + _, err = createAndAttachFIP(networkClient, "bootstrap", infraID, network.ID, bootstrapPort.ID) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// Get the first port associated with an instance. |
| 55 | +// |
| 56 | +// This is used to attach a FIP to the instance. |
| 57 | +func getPortForInstance(client *gophercloud.ServiceClient, instanceID string) (*ports.Port, error) { |
| 58 | + listOpts := ports.ListOpts{ |
| 59 | + DeviceID: instanceID, |
| 60 | + } |
| 61 | + allPages, err := ports.List(client, listOpts).AllPages() |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to list ports: %w", err) |
| 64 | + } |
| 65 | + allPorts, err := ports.ExtractPorts(allPages) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to extract ports: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + if len(allPorts) < 1 { |
| 71 | + return nil, fmt.Errorf("bootstrap machine has no associated ports") |
| 72 | + } |
| 73 | + |
| 74 | + return &allPorts[0], nil |
| 75 | +} |
| 76 | + |
| 77 | +// Get a network by name. |
| 78 | +func getNetworkByName(client *gophercloud.ServiceClient, name string) (*networks.Network, error) { |
| 79 | + listOpts := networks.ListOpts{ |
| 80 | + Name: name, |
| 81 | + } |
| 82 | + allPages, err := networks.List(client, listOpts).AllPages() |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to list networks: %w", err) |
| 85 | + } |
| 86 | + allNetworks, err := networks.ExtractNetworks(allPages) |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("failed to extract networks: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + if len(allNetworks) < 1 { |
| 92 | + return nil, fmt.Errorf("found no matches for network %s", name) |
| 93 | + } else if len(allNetworks) > 1 { |
| 94 | + return nil, fmt.Errorf("found more than one match for network %s", name) |
| 95 | + } |
| 96 | + |
| 97 | + return &allNetworks[0], nil |
| 98 | +} |
| 99 | + |
| 100 | +// Create a floating IP. |
| 101 | +func createAndAttachFIP(client *gophercloud.ServiceClient, role, infraID, networkID, portID string) (*floatingips.FloatingIP, error) { |
| 102 | + createOpts := floatingips.CreateOpts{ |
| 103 | + Description: fmt.Sprintf("%s-%s-fip", infraID, role), |
| 104 | + FloatingNetworkID: networkID, |
| 105 | + PortID: portID, |
| 106 | + } |
| 107 | + |
| 108 | + floatingIP, err := floatingips.Create(client, createOpts).Extract() |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + tag := fmt.Sprintf("openshiftClusterID=%s", infraID) |
| 114 | + err = attributestags.Add(client, "floatingips", floatingIP.ID, tag).ExtractErr() |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + return floatingIP, err |
| 120 | +} |
0 commit comments