@@ -35,6 +35,7 @@ import (
3535	kerrors "k8s.io/apimachinery/pkg/util/errors" 
3636	"k8s.io/apimachinery/pkg/util/sets" 
3737	"k8s.io/apiserver/pkg/storage/names" 
38+ 	"k8s.io/utils/ptr" 
3839
3940	infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" 
4041	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors" 
@@ -55,6 +56,14 @@ const elbResourceType = "elasticloadbalancing:loadbalancer"
5556// see: https://docs.aws.amazon.com/elasticloadbalancing/2012-06-01/APIReference/API_DescribeTags.html 
5657const  maxELBsDescribeTagsRequest  =  20 
5758
59+ // apiServerTargetGroupPrefix is the target group name prefix used when creating a target group for the API server 
60+ // listener. 
61+ const  apiServerTargetGroupPrefix  =  "apiserver-target-" 
62+ 
63+ // additionalTargetGroupPrefix is the target group name prefix used when creating target groups for additional 
64+ // listeners. 
65+ const  additionalTargetGroupPrefix  =  "additional-listener-" 
66+ 
5867// ReconcileLoadbalancers reconciles the load balancers for the given cluster. 
5968func  (s  * Service ) ReconcileLoadbalancers () error  {
6069	s .scope .Debug ("Reconciling load balancers" )
@@ -271,7 +280,7 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
271280				Protocol : infrav1 .ELBProtocolTCP ,
272281				Port :     infrav1 .DefaultAPIServerPort ,
273282				TargetGroup : infrav1.TargetGroupSpec {
274- 					Name :        names .SimpleNameGenerator .GenerateName ("apiserver-target-" ),
283+ 					Name :        names .SimpleNameGenerator .GenerateName (apiServerTargetGroupPrefix ),
275284					Port :        infrav1 .DefaultAPIServerPort ,
276285					Protocol :    infrav1 .ELBProtocolTCP ,
277286					VpcID :       s .scope .VPC ().ID ,
@@ -296,7 +305,7 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
296305				Protocol : listener .Protocol ,
297306				Port :     listener .Port ,
298307				TargetGroup : infrav1.TargetGroupSpec {
299- 					Name :        names .SimpleNameGenerator .GenerateName ("additional-listener-" ),
308+ 					Name :        names .SimpleNameGenerator .GenerateName (additionalTargetGroupPrefix ),
300309					Port :        listener .Port ,
301310					Protocol :    listener .Protocol ,
302311					VpcID :       s .scope .VPC ().ID ,
@@ -1573,9 +1582,11 @@ func (s *Service) reconcileTargetGroupsAndListeners(lbARN string, spec *infrav1.
15731582	// https://github.com/kubernetes-sigs/cluster-api-provider-aws/issues/3899 
15741583	for  _ , ln  :=  range  spec .ELBListeners  {
15751584		var  group  * elbv2.TargetGroup 
1585+ 		tgSpec  :=  ln .TargetGroup 
15761586		for  _ , g  :=  range  existingTargetGroups .TargetGroups  {
1577- 			if  * g . TargetGroupName   ==   ln . TargetGroup . Name  {
1587+ 			if  isSDKTargetGroupEqualToTargetGroup ( g ,  & tgSpec )  {
15781588				group  =  g 
1589+ 				break 
15791590			}
15801591		}
15811592		// create the target group first 
@@ -1604,8 +1615,9 @@ func (s *Service) reconcileTargetGroupsAndListeners(lbARN string, spec *infrav1.
16041615
16051616		var  listener  * elbv2.Listener 
16061617		for  _ , l  :=  range  existingListeners .Listeners  {
1607- 			if  l .DefaultActions  !=  nil  &&  len (l .DefaultActions ) >  0  &&  l .DefaultActions [0 ].TargetGroupArn  ==  group .TargetGroupArn  {
1618+ 			if  l .DefaultActions  !=  nil  &&  len (l .DefaultActions ) >  0  &&  * l .DefaultActions [0 ].TargetGroupArn  ==  * group .TargetGroupArn  {
16081619				listener  =  l 
1620+ 				break 
16091621			}
16101622		}
16111623
@@ -1614,9 +1626,8 @@ func (s *Service) reconcileTargetGroupsAndListeners(lbARN string, spec *infrav1.
16141626			if  err  !=  nil  {
16151627				return  nil , nil , err 
16161628			}
1629+ 			createdListeners  =  append (createdListeners , listener )
16171630		}
1618- 
1619- 		createdListeners  =  append (createdListeners , listener )
16201631	}
16211632
16221633	return  createdTargetGroups , createdListeners , nil 
@@ -1785,3 +1796,23 @@ func shouldReconcileSGs(scope scope.ELBScope, lb *infrav1.LoadBalancer, specSGs
17851796	}
17861797	return  true 
17871798}
1799+ 
1800+ // isSDKTargetGroupEqualToTargetGroup checks if a given AWS SDK target group matches a target group spec. 
1801+ func  isSDKTargetGroupEqualToTargetGroup (elbTG  * elbv2.TargetGroup , spec  * infrav1.TargetGroupSpec ) bool  {
1802+ 	// We can't check only the target group's name because it's randomly generated every time we get a spec 
1803+ 	// But CAPA-created target groups are guaranteed to have the "apiserver-target-" or "additional-listener-" prefix. 
1804+ 	switch  {
1805+ 	case  strings .HasPrefix (* elbTG .TargetGroupName , apiServerTargetGroupPrefix ):
1806+ 		if  ! strings .HasPrefix (spec .Name , apiServerTargetGroupPrefix ) {
1807+ 			return  false 
1808+ 		}
1809+ 	case  strings .HasPrefix (* elbTG .TargetGroupName , additionalTargetGroupPrefix ):
1810+ 		if  ! strings .HasPrefix (spec .Name , additionalTargetGroupPrefix ) {
1811+ 			return  false 
1812+ 		}
1813+ 	default :
1814+ 		// Not created by CAPA 
1815+ 		return  false 
1816+ 	}
1817+ 	return  ptr .Deref (elbTG .Port , 0 ) ==  spec .Port  &&  strings .EqualFold (* elbTG .Protocol , spec .Protocol .String ())
1818+ }
0 commit comments