Skip to content

Commit

Permalink
cluster-api: Create bootstrap FIP
Browse files Browse the repository at this point in the history
This is created in the preprovision step, since we'll attach it during
creation of the bootstrap machine.

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
  • Loading branch information
stephenfin authored and mandre committed Feb 15, 2024
1 parent 25a9170 commit 15b6489
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion pkg/infrastructure/openstack/clusterapi/clusterapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
return fmt.Errorf("failed to get OSPCluster: %w", err)
}

bootstrapMachine := &capo.OpenStackMachine{}
key = client.ObjectKey{
Name: capiutils.GenerateBoostrapMachineName(infraID),
Namespace: capiutils.Namespace,
}
if err := k8sClient.Get(ctx, key, bootstrapMachine); err != nil {
return fmt.Errorf("failed to get bootstrap Machine: %w", err)
}

networkClient, err := openstackdefaults.NewServiceClient("network", openstackdefaults.DefaultClientOpts(installConfig.Config.Platform.OpenStack.Cloud))
if err != nil {
return err
Expand Down Expand Up @@ -116,6 +125,21 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
}
}

bootstrapPort, err := getPortForInstance(networkClient, *bootstrapMachine.Spec.InstanceID)
if err != nil {
return err
}

bootstrapFIP, err := createFIP(networkClient, "bootstrap", infraID, ospCluster.Status.Network.ID)
if err != nil {
return err
}

err = assignFIP(networkClient, bootstrapFIP.ID, bootstrapPort)
if err != nil {
return err
}

return nil
}

Expand All @@ -142,7 +166,8 @@ func createPort(client *gophercloud.ServiceClient, role, infraID, networkID, sub
{
IPAddress: fixedIP,
SubnetID: subnetID,
}},
},
},
}

port, err := ports.Create(client, createOpts).Extract()
Expand All @@ -158,6 +183,50 @@ func createPort(client *gophercloud.ServiceClient, role, infraID, networkID, sub
return port, err
}

// Get the first port associated with an instance.
//
// This is used to attach a FIP to the instance.
func getPortForInstance(client *gophercloud.ServiceClient, instanceID string) (*ports.Port, error) {
listOpts := ports.ListOpts{
DeviceID: instanceID,
}
allPages, err := ports.List(client, listOpts).AllPages()
if err != nil {
return nil, fmt.Errorf("failed to list ports: %w", err)
}
allPorts, err := ports.ExtractPorts(allPages)
if err != nil {
return nil, fmt.Errorf("failed to extract ports: %w", err)
}

if len(allPorts) < 1 {
return nil, fmt.Errorf("bootstrap machine has no associated ports")
}

return &allPorts[0], nil
}

// Create a floating IP.
func createFIP(client *gophercloud.ServiceClient, role, infraID, networkID string) (*floatingips.FloatingIP, error) {
createOpts := floatingips.CreateOpts{
Description: fmt.Sprintf("%s-%s-fip", infraID, role),
FloatingNetworkID: networkID,
}

floatingIP, err := floatingips.Create(client, createOpts).Extract()
if err != nil {
return nil, err
}

tag := fmt.Sprintf("openshiftClusterID=%s", infraID)
err = attributestags.Add(client, "floatingips", floatingIP.ID, tag).ExtractErr()
if err != nil {
return nil, err
}

return floatingIP, err
}

func assignFIP(client *gophercloud.ServiceClient, address string, port *ports.Port) error {
listOpts := floatingips.ListOpts{
FloatingIP: address,
Expand Down

0 comments on commit 15b6489

Please sign in to comment.