@@ -26,6 +26,7 @@ import (
2626 "k8s.io/utils/ptr"
2727 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2828 "sigs.k8s.io/controller-runtime/pkg/client"
29+ logf "sigs.k8s.io/controller-runtime/pkg/log"
2930
3031 infrastructurev1beta2 "github.com/cloudscale-ch/cluster-api-provider-cloudscale/api/v1beta2"
3132 "github.com/cloudscale-ch/cluster-api-provider-cloudscale/internal/cloudscale"
@@ -41,13 +42,9 @@ func (r *CloudscaleClusterReconciler) reconcileFloatingIP(ctx context.Context, c
4142 return nil
4243 }
4344
44- var fipPending bool
45-
4645 defer func () {
4746 if reterr != nil {
4847 r .setCondition (clusterScope , infrastructurev1beta2 .FloatingIPReadyCondition , metav1 .ConditionFalse , infrastructurev1beta2 .FloatingIPErrorReason , reterr .Error ())
49- } else if fipPending {
50- r .setCondition (clusterScope , infrastructurev1beta2 .FloatingIPReadyCondition , metav1 .ConditionFalse , infrastructurev1beta2 .FloatingIPNotReadyReason , "Waiting for floating IP target" )
5148 } else {
5249 r .setCondition (clusterScope , infrastructurev1beta2 .FloatingIPReadyCondition , metav1 .ConditionTrue , infrastructurev1beta2 .FloatingIPProvisionedReason , "" )
5350 }
@@ -59,15 +56,14 @@ func (r *CloudscaleClusterReconciler) reconcileFloatingIP(ctx context.Context, c
5956 }
6057
6158 // Managed floating IP: create if needed, then assign
62- var err error
63- fipPending , err = r .reconcileManagedFloatingIP (ctx , clusterScope , fipSpec )
59+ err := r .reconcileManagedFloatingIP (ctx , clusterScope , fipSpec )
6460 return err
6561}
6662
6763func (r * CloudscaleClusterReconciler ) reconcileBYOFloatingIP (ctx context.Context , clusterScope * scope.ClusterScope , uuid string ) error {
6864 // Cached: status + endpoint already populated from a prior reconcile.
6965 // BYO FIPs are immutable from CAPCS' perspective, so no need to refetch.
70- if clusterScope .CloudscaleCluster .Status .FloatingIPHREF != "" &&
66+ if clusterScope .CloudscaleCluster .Status .FloatingIP != "" &&
7167 clusterScope .CloudscaleCluster .Spec .ControlPlaneEndpoint .Host != "" {
7268 return nil
7369 }
@@ -77,34 +73,37 @@ func (r *CloudscaleClusterReconciler) reconcileBYOFloatingIP(ctx context.Context
7773 return fmt .Errorf ("getting BYO floating IP %s: %w" , uuid , err )
7874 }
7975
80- clusterScope .CloudscaleCluster .Status .FloatingIPHREF = fip .HREF
76+ clusterScope .CloudscaleCluster .Status .FloatingIP = fip .IP ()
8177 r .setControlPlaneEndpointFromFIP (clusterScope , fip )
8278 return nil
8379}
8480
85- func (r * CloudscaleClusterReconciler ) reconcileManagedFloatingIP (ctx context.Context , clusterScope * scope.ClusterScope , fipSpec * infrastructurev1beta2.FloatingIPSpec ) (pending bool , _ error ) {
81+ func (r * CloudscaleClusterReconciler ) reconcileManagedFloatingIP (ctx context.Context , clusterScope * scope.ClusterScope , fipSpec * infrastructurev1beta2.FloatingIPSpec ) error {
82+ logger := logf .FromContext (ctx )
8683 tags := clusterOwnershipTags (clusterScope .CloudscaleCluster )
8784
85+ logger .Info ("reconcile managed floating IP" )
86+
8887 // Check if the floating IP already exists (by status ID or by tags)
8988 fip , id , err := ensureResource (ctx , clusterScope ,
90- clusterScope .CloudscaleCluster .Status .FloatingIPHREF ,
89+ clusterScope .CloudscaleCluster .Status .FloatingIP ,
9190 "floating IP" ,
9291 clusterScope .CloudscaleClient .FloatingIPs ,
93- func (fip cloudscalesdk.FloatingIP ) string { return fip .HREF },
92+ func (fip cloudscalesdk.FloatingIP ) string { return fip .IP () },
9493 tags ,
9594 )
9695 if err != nil {
97- return false , err
96+ return err
9897 }
99- clusterScope .CloudscaleCluster .Status .FloatingIPHREF = id
98+ clusterScope .CloudscaleCluster .Status .FloatingIP = id
10099
101100 if id != "" {
102101 // Existing floating IP: ensure it's assigned to the right target and set endpoint
103102 if err := r .ensureFloatingIPAssignment (ctx , clusterScope , fip ); err != nil {
104- return false , err
103+ return err
105104 }
106105 r .setControlPlaneEndpointFromFIP (clusterScope , fip )
107- return false , nil
106+ return nil
108107 }
109108
110109 // Create new floating IP
@@ -126,8 +125,7 @@ func (r *CloudscaleClusterReconciler) reconcileManagedFloatingIP(ctx context.Con
126125 // Assign to LB or CP server
127126 target , err := r .getFloatingIPTarget (ctx , clusterScope )
128127 if err != nil {
129- // No target available yet (LB or CP server not ready)
130- return true , nil
128+ logger .Info ("no target available yet" , "err" , err )
131129 }
132130 if target .lbUUID != "" {
133131 req .LoadBalancer = target .lbUUID
@@ -138,16 +136,17 @@ func (r *CloudscaleClusterReconciler) reconcileManagedFloatingIP(ctx context.Con
138136 clusterScope .Info ("Creating floating IP" , "ipVersion" , ipVersion , "target" , target )
139137 fip , err = clusterScope .CloudscaleClient .FloatingIPs .Create (ctx , req )
140138 if err != nil {
141- return false , fmt .Errorf ("creating floating IP: %w" , err )
139+ return fmt .Errorf ("creating floating IP: %w" , err )
142140 }
143141
144- clusterScope .CloudscaleCluster .Status .FloatingIPHREF = fip .HREF
145- clusterScope .Info ("Created floating IP" , "ip" , fip .Network , "id" , fip .HREF )
142+ ip := fip .IP ()
143+ clusterScope .CloudscaleCluster .Status .FloatingIP = ip
144+ clusterScope .Info ("Created floating IP" , "ip" , fip .Network , "id" , ip )
146145 r .recorder .Eventf (clusterScope .CloudscaleCluster , nil , corev1 .EventTypeNormal , "FloatingIPCreated" , "CreateFloatingIP" ,
147- "Created floating IP %s" , fip . IP () )
146+ "Created floating IP %s" , ip )
148147
149148 r .setControlPlaneEndpointFromFIP (clusterScope , fip )
150- return false , nil
149+ return nil
151150}
152151
153152type floatingIPTarget struct {
@@ -214,12 +213,13 @@ func (r *CloudscaleClusterReconciler) ensureFloatingIPAssignment(ctx context.Con
214213 }
215214
216215 if needsUpdate {
217- clusterScope .Info ("Reassigning floating IP" , "ip" , fip .IP (), "target" , target )
218- if err := clusterScope .CloudscaleClient .FloatingIPs .Update (ctx , fip .HREF , updateReq ); err != nil {
216+ floatingIP := clusterScope .CloudscaleCluster .Status .FloatingIP
217+ clusterScope .Info ("Reassigning floating IP" , "ip" , floatingIP , "target" , target )
218+ if err := clusterScope .CloudscaleClient .FloatingIPs .Update (ctx , floatingIP , updateReq ); err != nil {
219219 return fmt .Errorf ("updating floating IP assignment: %w" , err )
220220 }
221221 r .recorder .Eventf (clusterScope .CloudscaleCluster , nil , corev1 .EventTypeNormal , "FloatingIPReassigned" , "UpdateFloatingIP" ,
222- "Reassigned floating IP %s to %s" , fip . IP () , target )
222+ "Reassigned floating IP %s to %s" , floatingIP , target )
223223 }
224224
225225 return nil
@@ -230,13 +230,15 @@ func (r *CloudscaleClusterReconciler) setControlPlaneEndpointFromFIP(clusterScop
230230 return
231231 }
232232
233+ floatingIP := fip .IP ()
234+
233235 apiServerPort := clusterScope .CloudscaleCluster .Spec .ControlPlaneLoadBalancer .APIServerPort
234- clusterScope .CloudscaleCluster .Spec .ControlPlaneEndpoint .Host = fip . IP ()
236+ clusterScope .CloudscaleCluster .Spec .ControlPlaneEndpoint .Host = floatingIP
235237 clusterScope .CloudscaleCluster .Spec .ControlPlaneEndpoint .Port = apiServerPort
236238 clusterScope .Info ("Set control plane endpoint from floating IP" ,
237- "endpoint" , fip . IP () , "port" , apiServerPort )
239+ "endpoint" , floatingIP , "port" , apiServerPort )
238240 r .recorder .Eventf (clusterScope .CloudscaleCluster , nil , corev1 .EventTypeNormal , "ControlPlaneSet" , "SetControlPlaneEndpoint" ,
239- "Control plane endpoint set to %s:%d (floating IP)" , fip . IP () , apiServerPort )
241+ "Control plane endpoint set to %s:%d (floating IP)" , floatingIP , apiServerPort )
240242}
241243
242244// deleteFloatingIP deletes the floating IP if it's managed.
@@ -261,22 +263,22 @@ func (r *CloudscaleClusterReconciler) deleteFloatingIP(ctx context.Context, clus
261263 return nil
262264 }
263265
264- fipID := clusterScope .CloudscaleCluster .Status .FloatingIPHREF
265- if fipID == "" {
266+ floatingIP := clusterScope .CloudscaleCluster .Status .FloatingIP
267+ if floatingIP == "" {
266268 return nil
267269 }
268270
269- clusterScope .Info ("Deleting floating IP" , "id" , fipID )
270- if err := clusterScope .CloudscaleClient .FloatingIPs .Delete (ctx , fipID ); err != nil {
271+ clusterScope .Info ("Deleting floating IP" , "id" , floatingIP )
272+ if err := clusterScope .CloudscaleClient .FloatingIPs .Delete (ctx , floatingIP ); err != nil {
271273 if ! cloudscale .IsNotFound (err ) {
272274 return fmt .Errorf ("deleting floating IP: %w" , err )
273275 }
274- clusterScope .Info ("Floating IP already deleted" , "id" , fipID )
276+ clusterScope .Info ("Floating IP already deleted" , "id" , floatingIP )
275277 }
276278
277279 r .recorder .Eventf (clusterScope .CloudscaleCluster , nil , corev1 .EventTypeNormal , "FloatingIPDeleted" , "DeleteFloatingIP" ,
278- "Deleted floating IP %s" , fipID )
279- clusterScope .CloudscaleCluster .Status .FloatingIPHREF = ""
280+ "Deleted floating IP %s" , floatingIP )
281+ clusterScope .CloudscaleCluster .Status .FloatingIP = ""
280282
281283 return nil
282284}
0 commit comments