@@ -223,17 +223,17 @@ func (r *gatewayReconciler) reconcileHelper(ctx context.Context, req reconcile.R
223
223
}
224
224
225
225
if lb == nil {
226
- err = r .reconcileDelete (ctx , gw , stack , allRoutes )
226
+ err = r .reconcileDelete (ctx , gw , gwClass , stack , allRoutes )
227
227
if err != nil {
228
228
r .logger .Error (err , "Failed to process gateway delete" )
229
229
}
230
230
return err
231
231
}
232
232
233
- return r .reconcileUpdate (ctx , gw , stack , lb , backendSGRequired )
233
+ return r .reconcileUpdate (ctx , gw , gwClass , stack , lb , backendSGRequired )
234
234
}
235
235
236
- func (r * gatewayReconciler ) reconcileDelete (ctx context.Context , gw * gwv1.Gateway , stack core.Stack , routes map [int32 ][]routeutils.RouteDescriptor ) error {
236
+ func (r * gatewayReconciler ) reconcileDelete (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1. GatewayClass , stack core.Stack , routes map [int32 ][]routeutils.RouteDescriptor ) error {
237
237
for _ , routeList := range routes {
238
238
if len (routeList ) != 0 {
239
239
err := errors .Errorf ("Gateway deletion invoked with routes attached [%s]" , generateRouteList (routes ))
@@ -250,21 +250,34 @@ func (r *gatewayReconciler) reconcileDelete(ctx context.Context, gw *gwv1.Gatewa
250
250
if err := r .backendSGProvider .Release (ctx , networking .ResourceTypeGateway , []types.NamespacedName {k8s .NamespacedName (gw )}); err != nil {
251
251
return err
252
252
}
253
+ // remove load balancer configuration finalizer
254
+ if err := r .removeLoadBalancerConfigurationFinalizers (ctx , gw , gwClass ); err != nil {
255
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedRemoveFinalizer , fmt .Sprintf ("Failed remove load balancer configuration finalizer due to %v" , err ))
256
+ return err
257
+ }
258
+ // remove gateway finalizer
253
259
if err := r .finalizerManager .RemoveFinalizers (ctx , gw , r .finalizer ); err != nil {
254
- r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .GatewayEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed remove finalizer due to %v" , err ))
260
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .GatewayEventReasonFailedRemoveFinalizer , fmt .Sprintf ("Failed remove gateway finalizer due to %v" , err ))
255
261
return err
256
262
}
257
263
}
258
264
return nil
259
265
}
260
266
261
- func (r * gatewayReconciler ) reconcileUpdate (ctx context.Context , gw * gwv1.Gateway , stack core.Stack ,
267
+ func (r * gatewayReconciler ) reconcileUpdate (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1. GatewayClass , stack core.Stack ,
262
268
lb * elbv2model.LoadBalancer , backendSGRequired bool ) error {
263
-
269
+ // add gateway finalizer
264
270
if err := r .finalizerManager .AddFinalizers (ctx , gw , r .finalizer ); err != nil {
265
- r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .GatewayEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed add finalizer due to %v" , err ))
271
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .GatewayEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed add gateway finalizer due to %v" , err ))
266
272
return err
267
273
}
274
+
275
+ // add load balancer configuration finalizer
276
+ if err := r .addLoadBalancerConfigurationFinalizers (ctx , gw , gwClass ); err != nil {
277
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed add load balancer configuration finalizer due to %v" , err ))
278
+ return err
279
+ }
280
+
268
281
err := r .deployModel (ctx , gw , stack )
269
282
if err != nil {
270
283
return err
@@ -284,6 +297,93 @@ func (r *gatewayReconciler) reconcileUpdate(ctx context.Context, gw *gwv1.Gatewa
284
297
return nil
285
298
}
286
299
300
+ // add finalizer to load balancer configuration when it is in use by gateway or gatewayClass
301
+ func (r * gatewayReconciler ) addLoadBalancerConfigurationFinalizers (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass ) error {
302
+ // add finalizer to lbConfig referred by gatewayClass
303
+ if gwClass .Spec .ParametersRef != nil && string (gwClass .Spec .ParametersRef .Kind ) == constants .LoadBalancerConfiguration {
304
+ lbConfig := & elbv2gw.LoadBalancerConfiguration {}
305
+ if err := r .k8sClient .Get (ctx , types.NamespacedName {
306
+ Namespace : string (* gwClass .Spec .ParametersRef .Namespace ),
307
+ Name : gwClass .Spec .ParametersRef .Name ,
308
+ }, lbConfig ); err != nil {
309
+ return client .IgnoreNotFound (err )
310
+ }
311
+ if err := r .finalizerManager .AddFinalizers (ctx , lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
312
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed to add load balancer configuration finalizer due to %v" , err ))
313
+ return err
314
+ }
315
+ }
316
+
317
+ // add finalizer to lbConfig referred by gateway
318
+ if gw .Spec .Infrastructure != nil && gw .Spec .Infrastructure .ParametersRef != nil && string (gw .Spec .Infrastructure .ParametersRef .Kind ) == constants .LoadBalancerConfiguration {
319
+ lbConfig := & elbv2gw.LoadBalancerConfiguration {}
320
+ if err := r .k8sClient .Get (ctx , types.NamespacedName {
321
+ Namespace : gw .Namespace ,
322
+ Name : gw .Spec .Infrastructure .ParametersRef .Name ,
323
+ }, lbConfig ); err != nil {
324
+ return client .IgnoreNotFound (err )
325
+ }
326
+ if err := r .finalizerManager .AddFinalizers (ctx , lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
327
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedAddFinalizer , fmt .Sprintf ("Failed to add load balancer configuration finalizer due to %v" , err ))
328
+ return err
329
+ }
330
+ }
331
+ return nil
332
+ }
333
+
334
+ func (r * gatewayReconciler ) removeLoadBalancerConfigurationFinalizers (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass ) error {
335
+ // remove finalizer from lbConfig - gatewayClass
336
+ if gwClass .Spec .ParametersRef != nil && string (gwClass .Spec .ParametersRef .Kind ) == constants .LoadBalancerConfiguration {
337
+ lbConfig := & elbv2gw.LoadBalancerConfiguration {}
338
+ if err := r .k8sClient .Get (ctx , types.NamespacedName {
339
+ Namespace : string (* gwClass .Spec .ParametersRef .Namespace ),
340
+ Name : gwClass .Spec .ParametersRef .Name ,
341
+ }, lbConfig ); err != nil {
342
+ return client .IgnoreNotFound (err )
343
+ }
344
+ // remove finalizer if it exists and it not in use
345
+ if k8s .HasFinalizer (lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ) && ! r .isLBConfigInUse (ctx , lbConfig , gw ) {
346
+ if err := r .finalizerManager .RemoveFinalizers (ctx , lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
347
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedRemoveFinalizer , fmt .Sprintf ("Failed to remove load balancer configuration finalizer due to %v" , err ))
348
+ return err
349
+ }
350
+ }
351
+ }
352
+
353
+ // remove finalizer from lbConfig - gateway
354
+ if gw .Spec .Infrastructure != nil && gw .Spec .Infrastructure .ParametersRef != nil && string (gw .Spec .Infrastructure .ParametersRef .Kind ) == constants .LoadBalancerConfiguration {
355
+ lbConfig := & elbv2gw.LoadBalancerConfiguration {}
356
+ if err := r .k8sClient .Get (ctx , types.NamespacedName {
357
+ Namespace : gw .Namespace ,
358
+ Name : gw .Spec .Infrastructure .ParametersRef .Name ,
359
+ }, lbConfig ); err != nil {
360
+ return client .IgnoreNotFound (err )
361
+ }
362
+ // remove finalizer if it exists and it is not in use
363
+ if k8s .HasFinalizer (lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ) && ! r .isLBConfigInUse (ctx , lbConfig , gw ) {
364
+ if err := r .finalizerManager .RemoveFinalizers (ctx , lbConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
365
+ r .eventRecorder .Event (gw , corev1 .EventTypeWarning , k8s .LoadBalancerConfigurationEventReasonFailedRemoveFinalizer , fmt .Sprintf ("Failed to remove load balancer configuration finalizer due to %v" , err ))
366
+ return err
367
+ }
368
+ }
369
+ }
370
+ return nil
371
+ }
372
+
373
+ func (r * gatewayReconciler ) isLBConfigInUse (ctx context.Context , lbConfig * elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway ) bool {
374
+ // check if lbConfig is referred by other gateway
375
+ gwsUsingLBConfig := eventhandlers .GetImpactedGatewaysFromLbConfig (ctx , r .k8sClient , lbConfig , r .controllerName )
376
+ for _ , gwUsingLBConfig := range gwsUsingLBConfig {
377
+ if gwUsingLBConfig .Name != gw .Name || gwUsingLBConfig .Namespace != gw .Namespace {
378
+ return true
379
+ }
380
+ }
381
+
382
+ // check if lbConfig is referred by other gatewayClass
383
+ gwClassesUsingLBConfig := eventhandlers .GetImpactedGatewayClassesFromLbConfig (ctx , r .k8sClient , lbConfig , sets .New (r .controllerName ))
384
+ return len (gwClassesUsingLBConfig ) > 0
385
+ }
386
+
287
387
func (r * gatewayReconciler ) deployModel (ctx context.Context , gw * gwv1.Gateway , stack core.Stack ) error {
288
388
if err := r .stackDeployer .Deploy (ctx , stack , r .metricsCollector , r .controllerName , nil ); err != nil {
289
389
var requeueNeededAfter * runtime.RequeueNeededAfter
0 commit comments