@@ -40,8 +40,8 @@ import (
40
40
"sigs.k8s.io/controller-runtime/pkg/handler"
41
41
"sigs.k8s.io/controller-runtime/pkg/reconcile"
42
42
43
- "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1"
44
- "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
43
+ infrav1alpha1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1"
44
+ infrav1alpha7 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
45
45
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha8"
46
46
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
47
47
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
@@ -76,7 +76,7 @@ type OpenStackFloatingIPPoolReconciler struct {
76
76
77
77
func (r * OpenStackFloatingIPPoolReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (_ ctrl.Result , reterr error ) {
78
78
log := ctrl .LoggerFrom (ctx )
79
- pool := & v1alpha1 .OpenStackFloatingIPPool {}
79
+ pool := & infrav1alpha1 .OpenStackFloatingIPPool {}
80
80
if err := r .Client .Get (ctx , req .NamespacedName , pool ); err != nil {
81
81
return ctrl.Result {}, client .IgnoreNotFound (err )
82
82
}
@@ -93,7 +93,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
93
93
94
94
if pool .ObjectMeta .DeletionTimestamp .IsZero () {
95
95
// Add finalizer if it does not exist
96
- if controllerutil .AddFinalizer (pool , v1alpha1 .OpenStackFloatingIPPoolFinalizer ) {
96
+ if controllerutil .AddFinalizer (pool , infrav1alpha1 .OpenStackFloatingIPPoolFinalizer ) {
97
97
return ctrl.Result {}, r .Client .Update (ctx , pool )
98
98
}
99
99
} else {
@@ -119,7 +119,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
119
119
}
120
120
121
121
claims := & ipamv1.IPAddressClaimList {}
122
- if err := r .Client .List (context .Background (), claims , client .InNamespace (req .Namespace ), client.MatchingFields {v1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
122
+ if err := r .Client .List (context .Background (), claims , client .InNamespace (req .Namespace ), client.MatchingFields {infrav1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
123
123
return ctrl.Result {}, err
124
124
}
125
125
@@ -137,7 +137,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
137
137
return ctrl.Result {}, err
138
138
}
139
139
if apierrors .IsNotFound (err ) {
140
- ip , err := r .getIP (scope , pool )
140
+ ip , err := r .getIP (ctx , scope , pool )
141
141
if err != nil {
142
142
return ctrl.Result {}, err
143
143
}
@@ -147,7 +147,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
147
147
Name : claim .Name ,
148
148
Namespace : claim .Namespace ,
149
149
Finalizers : []string {
150
- v1alpha1 .DeleteFloatingIPFinalizer ,
150
+ infrav1alpha1 .DeleteFloatingIPFinalizer ,
151
151
},
152
152
OwnerReferences : []metav1.OwnerReference {
153
153
{
@@ -163,7 +163,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
163
163
Name : claim .Name ,
164
164
},
165
165
PoolRef : corev1.TypedLocalObjectReference {
166
- APIGroup : pointer .String (v1alpha1 .GroupVersion .Group ),
166
+ APIGroup : pointer .String (infrav1alpha1 .GroupVersion .Group ),
167
167
Kind : pool .Kind ,
168
168
Name : pool .Name ,
169
169
},
@@ -173,7 +173,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
173
173
}
174
174
175
175
// Retry creating the IPAddress object
176
- err = wait .ExponentialBackoff ( backoff , func () (bool , error ) {
176
+ err = wait .ExponentialBackoffWithContext ( ctx , backoff , func (ctx context. Context ) (bool , error ) {
177
177
if err := r .Client .Create (ctx , ipAddress ); err != nil {
178
178
return false , err
179
179
}
@@ -196,10 +196,10 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
196
196
return ctrl.Result {}, r .Client .Status ().Update (ctx , pool )
197
197
}
198
198
199
- func (r * OpenStackFloatingIPPoolReconciler ) reconcileDelete (ctx context.Context , scope scope.Scope , pool * v1alpha1 .OpenStackFloatingIPPool ) error {
199
+ func (r * OpenStackFloatingIPPoolReconciler ) reconcileDelete (ctx context.Context , scope scope.Scope , pool * infrav1alpha1 .OpenStackFloatingIPPool ) error {
200
200
log := ctrl .LoggerFrom (ctx )
201
201
ipAddresses := & ipamv1.IPAddressList {}
202
- if err := r .Client .List (ctx , ipAddresses , client .InNamespace (pool .Namespace ), client.MatchingFields {v1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
202
+ if err := r .Client .List (ctx , ipAddresses , client .InNamespace (pool .Namespace ), client.MatchingFields {infrav1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
203
203
return err
204
204
}
205
205
@@ -223,7 +223,7 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileDelete(ctx context.Context,
223
223
pool .Status .AvailableIPs = diff (pool .Status .AvailableIPs , []string {ip })
224
224
}
225
225
226
- if controllerutil .RemoveFinalizer (pool , v1alpha1 .OpenStackFloatingIPPoolFinalizer ) {
226
+ if controllerutil .RemoveFinalizer (pool , infrav1alpha1 .OpenStackFloatingIPPoolFinalizer ) {
227
227
log .Info ("Removing finalizer from OpenStackFloatingIPPool" )
228
228
return r .Client .Update (ctx , pool )
229
229
}
@@ -260,9 +260,9 @@ func diff(a []string, b []string) []string {
260
260
return result
261
261
}
262
262
263
- func (r * OpenStackFloatingIPPoolReconciler ) reconcileIPAddresses (ctx context.Context , scope scope.Scope , pool * v1alpha1 .OpenStackFloatingIPPool ) error {
263
+ func (r * OpenStackFloatingIPPoolReconciler ) reconcileIPAddresses (ctx context.Context , scope scope.Scope , pool * infrav1alpha1 .OpenStackFloatingIPPool ) error {
264
264
ipAddresses := & ipamv1.IPAddressList {}
265
- if err := r .Client .List (ctx , ipAddresses , client .InNamespace (pool .Namespace ), client.MatchingFields {v1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
265
+ if err := r .Client .List (ctx , ipAddresses , client .InNamespace (pool .Namespace ), client.MatchingFields {infrav1alpha1 .OpenStackFloatingIPPoolNameIndex : pool .Name }); err != nil {
266
266
return err
267
267
}
268
268
@@ -282,16 +282,16 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileIPAddresses(ctx context.Con
282
282
continue
283
283
}
284
284
285
- if controllerutil .ContainsFinalizer (ipAddress , v1alpha1 .DeleteFloatingIPFinalizer ) {
286
- if pool .Spec .ReclaimPolicy == v1alpha1 .ReclaimDelete && ! contains (pool .Spec .PreAllocatedFloatingIPs , ipAddress .Spec .Address ) {
285
+ if controllerutil .ContainsFinalizer (ipAddress , infrav1alpha1 .DeleteFloatingIPFinalizer ) {
286
+ if pool .Spec .ReclaimPolicy == infrav1alpha1 .ReclaimDelete && ! contains (pool .Spec .PreAllocatedFloatingIPs , ipAddress .Spec .Address ) {
287
287
if err = networkingService .DeleteFloatingIP (pool , ipAddress .Spec .Address ); err != nil {
288
288
return fmt .Errorf ("delete floating IP %q: %w" , ipAddress .Spec .Address , err )
289
289
}
290
290
} else {
291
291
pool .Status .AvailableIPs = append (pool .Status .AvailableIPs , ipAddress .Spec .Address )
292
292
}
293
293
}
294
- controllerutil .RemoveFinalizer (ipAddress , v1alpha1 .DeleteFloatingIPFinalizer )
294
+ controllerutil .RemoveFinalizer (ipAddress , infrav1alpha1 .DeleteFloatingIPFinalizer )
295
295
if err := r .Client .Update (ctx , ipAddress ); err != nil {
296
296
return err
297
297
}
@@ -302,7 +302,7 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileIPAddresses(ctx context.Con
302
302
return nil
303
303
}
304
304
305
- func (r * OpenStackFloatingIPPoolReconciler ) getIP (scope scope.Scope , pool * v1alpha1 .OpenStackFloatingIPPool ) (string , error ) {
305
+ func (r * OpenStackFloatingIPPoolReconciler ) getIP (ctx context. Context , scope scope.Scope , pool * infrav1alpha1 .OpenStackFloatingIPPool ) (string , error ) {
306
306
// There's a potential leak of IPs here, if the reconcile loop fails after we claim an IP but before we create the IPAddress object.
307
307
var ip string
308
308
@@ -314,17 +314,19 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *v1alp
314
314
315
315
// Get tagged floating IPs and add them to the available IPs if they are not present in either the available IPs or the claimed IPs
316
316
// This is done to prevent leaking floating IPs if to prevent leaking floating IPs if the floating IP was created but the IPAddress object was not
317
- taggedIPs , err := networkingService .GetFloatingIPsByTag (pool .GetFloatingIPTag ())
318
- if err != nil {
319
- scope .Logger ().Error (err , "Failed to get floating IPs by tag" , "pool" , pool .Name )
320
- return "" , err
321
- }
322
- for _ , taggedIP := range taggedIPs {
323
- if contains (pool .Status .AvailableIPs , taggedIP .FloatingIP ) || contains (pool .Status .ClaimedIPs , taggedIP .FloatingIP ) {
324
- continue
317
+ if len (pool .Status .AvailableIPs ) == 0 {
318
+ taggedIPs , err := networkingService .GetFloatingIPsByTag (pool .GetFloatingIPTag ())
319
+ if err != nil {
320
+ scope .Logger ().Error (err , "Failed to get floating IPs by tag" , "pool" , pool .Name )
321
+ return "" , err
322
+ }
323
+ for _ , taggedIP := range taggedIPs {
324
+ if contains (pool .Status .AvailableIPs , taggedIP .FloatingIP ) || contains (pool .Status .ClaimedIPs , taggedIP .FloatingIP ) {
325
+ continue
326
+ }
327
+ scope .Logger ().Info ("Tagged floating IP found that was not known to the pool, adding it to the pool" , "ip" , taggedIP .FloatingIP )
328
+ pool .Status .AvailableIPs = append (pool .Status .AvailableIPs , taggedIP .FloatingIP )
325
329
}
326
- scope .Logger ().Info ("Tagged floating IP found that was not known to the pool, adding it to the pool" , "ip" , taggedIP .FloatingIP )
327
- pool .Status .AvailableIPs = append (pool .Status .AvailableIPs , taggedIP .FloatingIP )
328
330
}
329
331
330
332
if len (pool .Status .AvailableIPs ) > 0 {
@@ -345,7 +347,7 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *v1alp
345
347
fp , err := networkingService .CreateFloatingIPForPool (pool )
346
348
if err != nil {
347
349
scope .Logger ().Error (err , "Failed to create floating IP" , "pool" , pool .Name )
348
- conditions .MarkFalse (pool , v1alpha1 .OpenstackFloatingIPPoolReadyCondition , infrav1 .OpenStackErrorReason , clusterv1 .ConditionSeverityError , "Failed to create floating IP: %v" , err )
350
+ conditions .MarkFalse (pool , infrav1alpha1 .OpenstackFloatingIPPoolReadyCondition , infrav1 .OpenStackErrorReason , clusterv1 .ConditionSeverityError , "Failed to create floating IP: %v" , err )
349
351
if ip != "" {
350
352
pool .Status .FailedIPs = append (pool .Status .FailedIPs , ip )
351
353
}
@@ -354,7 +356,7 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *v1alp
354
356
defer func () {
355
357
tag := pool .GetFloatingIPTag ()
356
358
357
- err := wait .ExponentialBackoff ( backoff , func () (bool , error ) {
359
+ err := wait .ExponentialBackoffWithContext ( ctx , backoff , func (ctx context. Context ) (bool , error ) {
358
360
if err := networkingService .TagFloatingIP (fp .FloatingIP , tag ); err != nil {
359
361
scope .Logger ().Error (err , "Failed to tag floating, retrying" , "ip" , fp .FloatingIP , "tag" , tag )
360
362
return false , err
@@ -366,14 +368,14 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *v1alp
366
368
}
367
369
}()
368
370
369
- conditions .MarkTrue (pool , v1alpha1 .OpenstackFloatingIPPoolReadyCondition )
371
+ conditions .MarkTrue (pool , infrav1alpha1 .OpenstackFloatingIPPoolReadyCondition )
370
372
371
373
ip = fp .FloatingIP
372
374
pool .Status .ClaimedIPs = append (pool .Status .ClaimedIPs , ip )
373
375
return ip , nil
374
376
}
375
377
376
- func (r * OpenStackFloatingIPPoolReconciler ) reconcileFloatingIPNetwork (scope scope.Scope , pool * v1alpha1 .OpenStackFloatingIPPool ) error {
378
+ func (r * OpenStackFloatingIPPoolReconciler ) reconcileFloatingIPNetwork (scope scope.Scope , pool * infrav1alpha1 .OpenStackFloatingIPPool ) error {
377
379
// If the pool already has a network, we don't need to do anything
378
380
if pool .Status .FloatingIPNetwork != nil {
379
381
return nil
@@ -397,7 +399,7 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileFloatingIPNetwork(scope sco
397
399
return fmt .Errorf ("found multiple networks, expects filter to match one (result: %v)" , networkList )
398
400
}
399
401
400
- pool .Status .FloatingIPNetwork = & v1alpha7 .NetworkStatus {
402
+ pool .Status .FloatingIPNetwork = & infrav1alpha7 .NetworkStatus {
401
403
ID : networkList [0 ].ID ,
402
404
Name : networkList [0 ].Name ,
403
405
Tags : networkList [0 ].Tags ,
@@ -442,7 +444,7 @@ func (r *OpenStackFloatingIPPoolReconciler) ipAddressToPoolMapper(_ context.Cont
442
444
}
443
445
444
446
func (r * OpenStackFloatingIPPoolReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager ) error {
445
- if err := mgr .GetFieldIndexer ().IndexField (ctx , & ipamv1.IPAddressClaim {}, v1alpha1 .OpenStackFloatingIPPoolNameIndex , func (rawObj client.Object ) []string {
447
+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & ipamv1.IPAddressClaim {}, infrav1alpha1 .OpenStackFloatingIPPoolNameIndex , func (rawObj client.Object ) []string {
446
448
claim := rawObj .(* ipamv1.IPAddressClaim )
447
449
if claim .Spec .PoolRef .Kind != openStackFloatingIPPool {
448
450
return nil
@@ -452,15 +454,15 @@ func (r *OpenStackFloatingIPPoolReconciler) SetupWithManager(ctx context.Context
452
454
return err
453
455
}
454
456
455
- if err := mgr .GetFieldIndexer ().IndexField (ctx , & ipamv1.IPAddress {}, v1alpha1 .OpenStackFloatingIPPoolNameIndex , func (rawObj client.Object ) []string {
457
+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & ipamv1.IPAddress {}, infrav1alpha1 .OpenStackFloatingIPPoolNameIndex , func (rawObj client.Object ) []string {
456
458
ip := rawObj .(* ipamv1.IPAddress )
457
459
return []string {ip .Spec .PoolRef .Name }
458
460
}); err != nil {
459
461
return err
460
462
}
461
463
462
464
return ctrl .NewControllerManagedBy (mgr ).
463
- For (& v1alpha1 .OpenStackFloatingIPPool {}).
465
+ For (& infrav1alpha1 .OpenStackFloatingIPPool {}).
464
466
Watches (
465
467
& ipamv1.IPAddressClaim {},
466
468
handler .EnqueueRequestsFromMapFunc (r .ipAddressClaimToPoolMapper ),
0 commit comments