Skip to content

Commit

Permalink
fixup! cluster-api: Create bootstrap FIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenfin committed Feb 19, 2024
1 parent ecf5aa0 commit 67153fd
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pkg/infrastructure/openstack/postprovision/floatingips.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
capo "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -37,7 +38,12 @@ func FloatingIPs(ctx context.Context, c client.Client, cluster *capo.OpenStackCl
return err
}

_, err = createAndAttachFIP(networkClient, "bootstrap", infraID, cluster.Spec.ExternalNetworkID, bootstrapPort.ID)
network, err := getNetworkByName(networkClient, cluster.Spec.ExternalNetworkID)
if err != nil {
return err
}

_, err = createAndAttachFIP(networkClient, "bootstrap", infraID, network.ID, bootstrapPort.ID)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,6 +74,29 @@ func getPortForInstance(client *gophercloud.ServiceClient, instanceID string) (*
return &allPorts[0], nil
}

// Get a network by name
func getNetworkByName(client *gophercloud.ServiceClient, name string) (*networks.Network, error) {
listOpts := networks.ListOpts{
Name: name,
}
allPages, err := networks.List(client, listOpts).AllPages()
if err != nil {
return nil, fmt.Errorf("failed to list networks: %w", err)
}
allNetworks, err := networks.ExtractNetworks(allPages)
if err != nil {
return nil, fmt.Errorf("failed to extract networks: %w", err)
}

if len(allNetworks) < 1 {
return nil, fmt.Errorf("found no matches for network %s", name)
} else if len(allNetworks) > 1 {
return nil, fmt.Errorf("found more than one match for network %s", name)
}

return &allNetworks[0], nil
}

// Create a floating IP.
func createAndAttachFIP(client *gophercloud.ServiceClient, role, infraID, networkID, portID string) (*floatingips.FloatingIP, error) {
createOpts := floatingips.CreateOpts{
Expand Down

0 comments on commit 67153fd

Please sign in to comment.