@@ -6,13 +6,14 @@ import (
66 "k8s.io/apimachinery/pkg/types"
77 "k8s.io/apimachinery/pkg/util/sets"
88 elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
9+ "sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/constants"
910 "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1011 "sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
1112 "sigs.k8s.io/controller-runtime/pkg/client"
1213 gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1314)
1415
15- func RemoveLoadBalancerConfigurationFinalizers (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass , k8sClient client.Client , manager k8s.FinalizerManager , controllerNames sets. Set [ string ] ) error {
16+ func RemoveLoadBalancerConfigurationFinalizers (ctx context.Context , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass , k8sClient client.Client , manager k8s.FinalizerManager ) error {
1617 // remove finalizer from lbConfig - gatewayClass
1718 if gwClass != nil {
1819 gatewayClassLBConfig , err := ResolveLoadBalancerConfig (ctx , k8sClient , gwClass .Spec .ParametersRef )
@@ -22,7 +23,7 @@ func RemoveLoadBalancerConfigurationFinalizers(ctx context.Context, gw *gwv1.Gat
2223 // remove finalizer if it exists and it not in use
2324 if gatewayClassLBConfig != nil &&
2425 k8s .HasFinalizer (gatewayClassLBConfig , shared_constants .LoadBalancerConfigurationFinalizer ) &&
25- ! IsLBConfigInUse (ctx , gatewayClassLBConfig , gw , gwClass , k8sClient , controllerNames ) {
26+ ! IsLBConfigInUse (ctx , gatewayClassLBConfig , gw , gwClass , k8sClient ) {
2627 if err := manager .RemoveFinalizers (ctx , gatewayClassLBConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
2728 return err
2829 }
@@ -38,7 +39,7 @@ func RemoveLoadBalancerConfigurationFinalizers(ctx context.Context, gw *gwv1.Gat
3839 // remove finalizer if it exists and it is not in use
3940 if gatewayLBConfig != nil &&
4041 k8s .HasFinalizer (gatewayLBConfig , shared_constants .LoadBalancerConfigurationFinalizer ) &&
41- ! IsLBConfigInUse (ctx , gatewayLBConfig , gw , gwClass , k8sClient , controllerNames ) {
42+ ! IsLBConfigInUse (ctx , gatewayLBConfig , gw , gwClass , k8sClient ) {
4243 if err := manager .RemoveFinalizers (ctx , gatewayLBConfig , shared_constants .LoadBalancerConfigurationFinalizer ); err != nil {
4344 return err
4445 }
@@ -68,10 +69,14 @@ func ResolveLoadBalancerConfig(ctx context.Context, k8sClient client.Client, ref
6869 return lbConf , err
6970}
7071
71- func IsLBConfigInUse (ctx context.Context , lbConfig * elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass , k8sClient client.Client , controllerNames sets.Set [string ]) bool {
72+ func IsLBConfigInUse (ctx context.Context , lbConfig * elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , gwClass * gwv1.GatewayClass , k8sClient client.Client ) bool {
73+ //we want to make sure that we check the lb config is being used either by L4 or L7 gateways
74+ controllerNames := sets .New (constants .ALBGatewayController , constants .NLBGatewayController )
7275 return IsLBConfigInUseByGatewayClass (ctx , lbConfig , gw , gwClass , k8sClient , controllerNames ) ||
7376 IsLBConfigInUseByGateway (ctx , lbConfig , gw , k8sClient , controllerNames )
7477}
78+
79+ // checks if the lbconfig is indirectly being used by any gateways of the gwclass
7580func IsLBConfigInUseByGatewayClass (ctx context.Context , lbConfig * elbv2gw.LoadBalancerConfiguration , currGw * gwv1.Gateway , gwClass * gwv1.GatewayClass , k8sClient client.Client , controllerNames sets.Set [string ]) bool {
7681 // fetch all the gateway classes referenced by lb config
7782 gwClassesUsingLBConfig := GetImpactedGatewayClassesFromLbConfig (ctx , k8sClient , lbConfig , controllerNames )
@@ -94,24 +99,27 @@ func IsLBConfigInUseByGatewayClass(ctx context.Context, lbConfig *elbv2gw.LoadBa
9499 // iterate through each GatewayClass identified as referencing the LoadBalancerConfiguration
95100 // the lbconfig is deemed to be in active use if any of these GatewayClasses
96101 // are found to be managing one or more active Gateway resources.
102+ gwsUsingLBConfig := make ([]* gwv1.Gateway , 0 )
97103 for _ , controllerName := range controllerNames .UnsortedList () {
98104 for _ , gwClassUsingLBConfig := range gwClassesUsingLBConfig {
99105 gwList := GetGatewaysManagedByGatewayClass (ctx , k8sClient , gwClassUsingLBConfig , controllerName )
100- if currGw == nil {
101- return len (gwList ) > 0
102- }
103- //skip the current gw from the list if it is not nil
104- for _ , gw := range gwList {
105- if k8s .NamespacedName (currGw ) != k8s .NamespacedName (gw ) {
106- return true
107- }
108- }
106+ gwsUsingLBConfig = append (gwsUsingLBConfig , gwList ... )
107+ }
108+ }
109+ if currGw == nil {
110+ return len (gwsUsingLBConfig ) > 0
111+ }
112+ //skip the current gw from the list if it is not nil
113+ for _ , gw := range gwsUsingLBConfig {
114+ if k8s .NamespacedName (currGw ) != k8s .NamespacedName (gw ) {
115+ return true
109116 }
110117 }
111118
112119 return false
113120}
114121
122+ // checks if lbconfig is directly being used by any gateways
115123func IsLBConfigInUseByGateway (ctx context.Context , lbConfig * elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , k8sClient client.Client , controllerNames sets.Set [string ]) bool {
116124 var gwsUsingLBConfig []* gwv1.Gateway
117125 for _ , controllerName := range controllerNames .UnsortedList () {
0 commit comments