88 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
99 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
1010 "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
11+ "github.com/gophercloud/gophercloud/pagination"
1112 capo "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
1213 "sigs.k8s.io/controller-runtime/pkg/client"
1314
@@ -35,7 +36,7 @@ func FloatingIPs(ctx context.Context, c client.Client, cluster *capo.OpenStackCl
3536 return err
3637 }
3738
38- bootstrapPort , err := getPortForInstance (networkClient , * bootstrapMachine .Status .InstanceID )
39+ bootstrapPort , err := getPortForInstance (networkClient , * bootstrapMachine .Status .InstanceID , cluster . Status . Network . ID )
3940 if err != nil {
4041 return err
4142 }
@@ -48,27 +49,27 @@ func FloatingIPs(ctx context.Context, c client.Client, cluster *capo.OpenStackCl
4849 return nil
4950}
5051
51- // Get the first port associated with an instance.
52- //
53- // This is used to attach a FIP to the instance.
54- func getPortForInstance (client * gophercloud.ServiceClient , instanceID string ) (* ports.Port , error ) {
55- listOpts := ports.ListOpts {
56- DeviceID : instanceID ,
52+ // Return the first port associated with the given instance and existing on the
53+ // given network.
54+ func getPortForInstance (client * gophercloud.ServiceClient , instanceID , networkID string ) (* ports.Port , error ) {
55+ var port * ports.Port
56+ if err := ports .List (client , ports.ListOpts {DeviceID : instanceID , NetworkID : networkID }).EachPage (func (page pagination.Page ) (bool , error ) {
57+ ports , err := ports .ExtractPorts (page )
58+ if err != nil {
59+ return false , err
60+ }
61+ if len (ports ) > 0 {
62+ port = & ports [0 ]
63+ return false , nil
64+ }
65+ return true , nil
66+ }); err != nil {
67+ return nil , fmt .Errorf ("failed to list the ports of the bootstrap server: %w" , err )
5768 }
58- allPages , err := ports .List (client , listOpts ).AllPages ()
59- if err != nil {
60- return nil , fmt .Errorf ("failed to list ports: %w" , err )
61- }
62- allPorts , err := ports .ExtractPorts (allPages )
63- if err != nil {
64- return nil , fmt .Errorf ("failed to extract ports: %w" , err )
69+ if port == nil {
70+ return nil , fmt .Errorf ("the bootstrap server has no associated ports" )
6571 }
66-
67- if len (allPorts ) < 1 {
68- return nil , fmt .Errorf ("bootstrap machine has no associated ports" )
69- }
70-
71- return & allPorts [0 ], nil
72+ return port , nil
7273}
7374
7475// Create a floating IP.
0 commit comments