Skip to content

Commit c88d423

Browse files
authored
NLB IP model builder check existing target group (#1383)
* Check existing target group * Add more UTs
1 parent 28cee8f commit c88d423

File tree

3 files changed

+461
-30
lines changed

3 files changed

+461
-30
lines changed

pkg/service/nlb/builder.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
232232
backendProtocol := ""
233233
b.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixBEProtocol, &backendProtocol, b.service.Annotations)
234234

235+
targetGroupMap := map[string]*elbv2.TargetGroup{}
236+
235237
for _, port := range b.service.Spec.Ports {
236238
hc, err := b.buildTargetHealthCheck(ctx)
237239
tgProtocol := elbv2.Protocol(port.Protocol)
@@ -247,32 +249,36 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
247249
listenerProtocol = elbv2.ProtocolTLS
248250
}
249251
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+
},
272278
},
273279
},
274-
},
275-
})
280+
})
281+
}
276282

277283
var sslPolicy *string = nil
278284
sslPolicyStr := ""
@@ -281,8 +287,10 @@ func (b *nlbBuilder) buildListeners(ctx context.Context, stack core.Stack, lb *e
281287
}
282288

283289
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+
}
286294
}
287295

288296
_ = elbv2.NewListener(stack, strconv.Itoa(int(port.Port)), elbv2.ListenerSpec{

0 commit comments

Comments
 (0)