@@ -232,6 +232,8 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
232
232
backendProtocol := ""
233
233
b .annotationParser .ParseStringAnnotation (annotations .SvcLBSuffixBEProtocol , & backendProtocol , b .service .Annotations )
234
234
235
+ targetGroupMap := map [string ]* elbv2.TargetGroup {}
236
+
235
237
for _ , port := range b .service .Spec .Ports {
236
238
hc , err := b .buildTargetHealthCheck (ctx )
237
239
tgProtocol := elbv2 .Protocol (port .Protocol )
@@ -247,32 +249,36 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
247
249
listenerProtocol = elbv2 .ProtocolTLS
248
250
}
249
251
tgName := b .targetGroupName (b .service , b .key , port .TargetPort , string (tgProtocol ), hc )
250
-
251
- targetGroup := elbv2 .NewTargetGroup (stack , tgName , elbv2.TargetGroupSpec {
252
- Name : tgName ,
253
- TargetType : elbv2 .TargetTypeIP ,
254
- Port : int64 (port .TargetPort .IntVal ),
255
- Protocol : tgProtocol ,
256
- HealthCheckConfig : hc ,
257
- TargetGroupAttributes : tgAttrs ,
258
- })
259
- var targetType elbv2api.TargetType = elbv2api .TargetTypeIP
260
- _ = elbv2 .NewTargetGroupBindingResource (stack , tgName , elbv2.TargetGroupBindingResourceSpec {
261
- TargetGroupARN : targetGroup .TargetGroupARN (),
262
- Template : elbv2.TargetGroupBindingTemplate {
263
- ObjectMeta : metav1.ObjectMeta {
264
- Namespace : b .service .Namespace ,
265
- GenerateName : tgName ,
266
- },
267
- Spec : elbv2api.TargetGroupBindingSpec {
268
- TargetType : & targetType ,
269
- ServiceRef : elbv2api.ServiceReference {
270
- Name : b .service .Name ,
271
- Port : port .TargetPort ,
252
+ targetGroup , exists := targetGroupMap [port .TargetPort .String ()]
253
+ if ! exists {
254
+ targetGroup = elbv2 .NewTargetGroup (stack , tgName , elbv2.TargetGroupSpec {
255
+ Name : tgName ,
256
+ TargetType : elbv2 .TargetTypeIP ,
257
+ Port : int64 (port .TargetPort .IntVal ),
258
+ Protocol : tgProtocol ,
259
+ HealthCheckConfig : hc ,
260
+ TargetGroupAttributes : tgAttrs ,
261
+ })
262
+ targetGroupMap [port .TargetPort .String ()] = targetGroup
263
+
264
+ var targetType elbv2api.TargetType = elbv2api .TargetTypeIP
265
+ _ = elbv2 .NewTargetGroupBindingResource (stack , tgName , elbv2.TargetGroupBindingResourceSpec {
266
+ TargetGroupARN : targetGroup .TargetGroupARN (),
267
+ Template : elbv2.TargetGroupBindingTemplate {
268
+ ObjectMeta : metav1.ObjectMeta {
269
+ Namespace : b .service .Namespace ,
270
+ GenerateName : tgName ,
271
+ },
272
+ Spec : elbv2api.TargetGroupBindingSpec {
273
+ TargetType : & targetType ,
274
+ ServiceRef : elbv2api.ServiceReference {
275
+ Name : b .service .Name ,
276
+ Port : port .TargetPort ,
277
+ },
272
278
},
273
279
},
274
- },
275
- })
280
+ })
281
+ }
276
282
277
283
var sslPolicy * string = nil
278
284
sslPolicyStr := ""
@@ -281,8 +287,10 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
281
287
}
282
288
283
289
certificates := []elbv2.Certificate {}
284
- for _ , cert := range certificateARNs {
285
- certificates = append (certificates , elbv2.Certificate {CertificateARN : & cert })
290
+ if listenerProtocol == elbv2 .ProtocolTLS {
291
+ for _ , cert := range certificateARNs {
292
+ certificates = append (certificates , elbv2.Certificate {CertificateARN : & cert })
293
+ }
286
294
}
287
295
288
296
_ = elbv2 .NewListener (stack , strconv .Itoa (int (port .Port )), elbv2.ListenerSpec {
0 commit comments