@@ -89,7 +89,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroup(tgByResID *map[string]bu
89
89
return buildTargetGroupOutput {}, err
90
90
}
91
91
nodeSelector := builder .buildTargetGroupBindingNodeSelector (targetGroupProps , tgSpec .TargetType )
92
- bindingSpec := builder .buildTargetGroupBindingSpec (lbConfig , targetGroupProps , tgSpec , nodeSelector , backend , backendSGIDToken )
92
+ bindingSpec := builder .buildTargetGroupBindingSpec (targetGroupProps , tgSpec , nodeSelector , backend , backendSGIDToken )
93
93
94
94
output := buildTargetGroupOutput {
95
95
targetGroupSpec : tgSpec ,
@@ -109,7 +109,7 @@ func (builder *targetGroupBuilderImpl) getTargetGroupProps(routeDescriptor route
109
109
return targetGroupProps
110
110
}
111
111
112
- func (builder * targetGroupBuilderImpl ) buildTargetGroupBindingSpec (lbConfig * elbv2gw. LoadBalancerConfiguration , tgProps * elbv2gw.TargetGroupProps , tgSpec elbv2model.TargetGroupSpec , nodeSelector * metav1.LabelSelector , backend routeutils.Backend , backendSGIDToken core.StringToken ) elbv2model.TargetGroupBindingResourceSpec {
112
+ func (builder * targetGroupBuilderImpl ) buildTargetGroupBindingSpec (tgProps * elbv2gw.TargetGroupProps , tgSpec elbv2model.TargetGroupSpec , nodeSelector * metav1.LabelSelector , backend routeutils.Backend , backendSGIDToken core.StringToken ) elbv2model.TargetGroupBindingResourceSpec {
113
113
targetType := elbv2api .TargetType (tgSpec .TargetType )
114
114
targetPort := backend .ServicePort .TargetPort
115
115
if targetType == elbv2api .TargetTypeInstance {
@@ -142,14 +142,14 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingSpec(lbConfig *elb
142
142
}
143
143
}
144
144
145
- func (builder * targetGroupBuilderImpl ) buildTargetGroupBindingNetworking (targetPort intstr.IntOrString , healthCheckPort intstr.IntOrString , port corev1.ServicePort , backendSGIDToken core.StringToken ) * elbv2model.TargetGroupBindingNetworking {
145
+ func (builder * targetGroupBuilderImpl ) buildTargetGroupBindingNetworking (targetPort intstr.IntOrString , healthCheckPort intstr.IntOrString , svcPort corev1.ServicePort , backendSGIDToken core.StringToken ) * elbv2model.TargetGroupBindingNetworking {
146
146
if backendSGIDToken == nil {
147
147
return nil
148
148
}
149
149
protocolTCP := elbv2api .NetworkingProtocolTCP
150
150
protocolUDP := elbv2api .NetworkingProtocolUDP
151
151
152
- udpSupported := port .Protocol == corev1 .ProtocolUDP
152
+ udpSupported := svcPort .Protocol == corev1 .ProtocolUDP
153
153
154
154
if builder .disableRestrictedSGRules {
155
155
ports := []elbv2api.NetworkingPort {
@@ -183,7 +183,6 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingNetworking(targetP
183
183
}
184
184
185
185
var networkingPorts []elbv2api.NetworkingPort
186
- var networkingRules []elbv2model.NetworkingIngressRule
187
186
188
187
protocolToUse := & protocolTCP
189
188
if udpSupported {
@@ -209,6 +208,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingNetworking(targetP
209
208
})
210
209
}
211
210
211
+ var networkingRules []elbv2model.NetworkingIngressRule
212
212
for _ , port := range networkingPorts {
213
213
networkingRules = append (networkingRules , elbv2model.NetworkingIngressRule {
214
214
From : []elbv2model.NetworkingPeer {
@@ -232,7 +232,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
232
232
if err != nil {
233
233
return elbv2model.TargetGroupSpec {}, err
234
234
}
235
- tgProtocolVersion := builder .buildTargetGroupProtocolVersion (targetGroupProps )
235
+ tgProtocolVersion := builder .buildTargetGroupProtocolVersion (targetGroupProps , route )
236
236
237
237
healthCheckConfig , err := builder .buildTargetGroupHealthCheckConfig (targetGroupProps , tgProtocol , tgProtocolVersion , targetType , backend )
238
238
if err != nil {
@@ -249,8 +249,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
249
249
return elbv2model.TargetGroupSpec {}, err
250
250
}
251
251
tgPort := builder .buildTargetGroupPort (targetType , * backend .ServicePort )
252
- // TODO - backend.ServicePort.TargetPort might not be correct.
253
- name := builder .buildTargetGroupName (targetGroupProps , k8s .NamespacedName (gw ), route .GetRouteNamespacedName (), k8s .NamespacedName (backend .Service ), backend .ServicePort .TargetPort , tgPort , targetType , tgProtocol , tgProtocolVersion )
252
+ name := builder .buildTargetGroupName (targetGroupProps , k8s .NamespacedName (gw ), route .GetRouteNamespacedName (), k8s .NamespacedName (backend .Service ), tgPort , targetType , tgProtocol , tgProtocolVersion )
254
253
return elbv2model.TargetGroupSpec {
255
254
Name : name ,
256
255
TargetType : targetType ,
@@ -268,7 +267,7 @@ var invalidTargetGroupNamePattern = regexp.MustCompile("[[:^alnum:]]")
268
267
269
268
// buildTargetGroupName will calculate the targetGroup's name.
270
269
func (builder * targetGroupBuilderImpl ) buildTargetGroupName (targetGroupProps * elbv2gw.TargetGroupProps ,
271
- gwKey types.NamespacedName , routeKey types.NamespacedName , svcKey types.NamespacedName , port intstr. IntOrString , tgPort int32 ,
270
+ gwKey types.NamespacedName , routeKey types.NamespacedName , svcKey types.NamespacedName , tgPort int32 ,
272
271
targetType elbv2model.TargetType , tgProtocol elbv2model.Protocol , tgProtocolVersion * elbv2model.ProtocolVersion ) string {
273
272
274
273
if targetGroupProps != nil && targetGroupProps .TargetGroupName != "" {
@@ -283,7 +282,6 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupName(targetGroupProps *el
283
282
_ , _ = uuidHash .Write ([]byte (routeKey .Name ))
284
283
_ , _ = uuidHash .Write ([]byte (svcKey .Namespace ))
285
284
_ , _ = uuidHash .Write ([]byte (svcKey .Name ))
286
- _ , _ = uuidHash .Write ([]byte (port .String ()))
287
285
_ , _ = uuidHash .Write ([]byte (strconv .Itoa (int (tgPort ))))
288
286
_ , _ = uuidHash .Write ([]byte (targetType ))
289
287
_ , _ = uuidHash .Write ([]byte (tgProtocol ))
@@ -365,10 +363,7 @@ func (builder *targetGroupBuilderImpl) buildL7TargetGroupProtocol(targetGroupPro
365
363
}
366
364
367
365
func (builder * targetGroupBuilderImpl ) buildL4TargetGroupProtocol (targetGroupProps * elbv2gw.TargetGroupProps , route routeutils.RouteDescriptor ) (elbv2model.Protocol , error ) {
368
- // TODO, auto infer?
369
366
if targetGroupProps == nil || targetGroupProps .Protocol == nil {
370
- // infer this somehow!?
371
- // use the backend config to get the protocol type.
372
367
return builder .inferTargetGroupProtocolFromRoute (route ), nil
373
368
}
374
369
@@ -406,7 +401,12 @@ func (builder *targetGroupBuilderImpl) inferTargetGroupProtocolFromRoute(route r
406
401
return elbv2model .ProtocolTCP
407
402
}
408
403
409
- func (builder * targetGroupBuilderImpl ) buildTargetGroupProtocolVersion (targetGroupProps * elbv2gw.TargetGroupProps ) * elbv2model.ProtocolVersion {
404
+ var (
405
+ http1 = elbv2model .ProtocolVersionHTTP1
406
+ grpc = elbv2model .ProtocolVersionGRPC
407
+ )
408
+
409
+ func (builder * targetGroupBuilderImpl ) buildTargetGroupProtocolVersion (targetGroupProps * elbv2gw.TargetGroupProps , route routeutils.RouteDescriptor ) * elbv2model.ProtocolVersion {
410
410
// NLB doesn't support protocol version
411
411
if builder .loadBalancerType == elbv2model .LoadBalancerTypeNetwork {
412
412
return nil
@@ -416,7 +416,11 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupProtocolVersion(targetGro
416
416
pv := elbv2model .ProtocolVersion (* targetGroupProps .ProtocolVersion )
417
417
return & pv
418
418
}
419
- http1 := elbv2model .ProtocolVersionHTTP1
419
+
420
+ if route .GetRouteKind () == routeutils .GRPCRouteKind {
421
+ return & grpc
422
+ }
423
+
420
424
return & http1
421
425
}
422
426
@@ -425,13 +429,13 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckConfig(targetG
425
429
// https://github.com/kubernetes-sigs/gateway-api/issues/451
426
430
// Gateway API doesn't have the same ServiceExternalTrafficPolicyLocal support.
427
431
// TODO - Maybe a TargetGroupConfig attribute to support the same behavior?
428
- healthCheckPort , err := builder .buildTargetGroupHealthCheckPort (targetGroupProps , targetType , backend )
432
+ healthCheckPort , err := builder .buildTargetGroupHealthCheckPort (targetGroupProps , targetType , backend . Service )
429
433
if err != nil {
430
434
return elbv2model.TargetGroupHealthCheckConfig {}, err
431
435
}
432
436
healthCheckProtocol := builder .buildTargetGroupHealthCheckProtocol (targetGroupProps , tgProtocol )
433
437
healthCheckPath := builder .buildTargetGroupHealthCheckPath (targetGroupProps , tgProtocolVersion , healthCheckProtocol )
434
- healthCheckMatcher := builder .buildTargetGroupHealthCheckMatcher (targetGroupProps , healthCheckProtocol )
438
+ healthCheckMatcher := builder .buildTargetGroupHealthCheckMatcher (targetGroupProps , tgProtocolVersion , healthCheckProtocol )
435
439
healthCheckIntervalSeconds := builder .buildTargetGroupHealthCheckIntervalSeconds (targetGroupProps )
436
440
healthCheckTimeoutSeconds := builder .buildTargetGroupHealthCheckTimeoutSeconds (targetGroupProps )
437
441
healthCheckHealthyThresholdCount := builder .buildTargetGroupHealthCheckHealthyThresholdCount (targetGroupProps )
@@ -450,22 +454,28 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckConfig(targetG
450
454
return hcConfig , nil
451
455
}
452
456
453
- func (builder * targetGroupBuilderImpl ) buildTargetGroupHealthCheckPort (targetGroupProps * elbv2gw.TargetGroupProps , targetType elbv2model.TargetType , backend routeutils.Backend ) (intstr.IntOrString , error ) {
454
- if targetGroupProps == nil || targetGroupProps .HealthCheckConfig == nil || targetGroupProps .HealthCheckConfig .HealthCheckPort == nil || * targetGroupProps .HealthCheckConfig .HealthCheckPort == shared_constants .HealthCheckPortTrafficPort {
457
+ func (builder * targetGroupBuilderImpl ) buildTargetGroupHealthCheckPort (targetGroupProps * elbv2gw.TargetGroupProps , targetType elbv2model.TargetType , svc * corev1.Service ) (intstr.IntOrString , error ) {
458
+
459
+ portConfigNotExist := targetGroupProps == nil || targetGroupProps .HealthCheckConfig == nil || targetGroupProps .HealthCheckConfig .HealthCheckPort == nil
460
+ if portConfigNotExist || * targetGroupProps .HealthCheckConfig .HealthCheckPort == shared_constants .HealthCheckPortTrafficPort {
455
461
return intstr .FromString (shared_constants .HealthCheckPortTrafficPort ), nil
456
462
}
457
463
458
464
healthCheckPort := intstr .Parse (* targetGroupProps .HealthCheckConfig .HealthCheckPort )
459
465
if healthCheckPort .Type == intstr .Int {
460
466
return healthCheckPort , nil
461
467
}
468
+ hcSvcPort , err := k8s .LookupServicePort (svc , healthCheckPort )
469
+ if err != nil {
470
+ return intstr .FromString ("" ), err
471
+ }
462
472
463
- /* TODO - Zac revisit this? */
464
473
if targetType == elbv2model .TargetTypeInstance {
465
- return intstr .FromInt (int (backend . ServicePort .NodePort )), nil
474
+ return intstr .FromInt (int (hcSvcPort .NodePort )), nil
466
475
}
467
- if backend .ServicePort .TargetPort .Type == intstr .Int {
468
- return backend .ServicePort .TargetPort , nil
476
+
477
+ if hcSvcPort .TargetPort .Type == intstr .Int {
478
+ return hcSvcPort .TargetPort , nil
469
479
}
470
480
return intstr.IntOrString {}, errors .New ("cannot use named healthCheckPort for IP TargetType when service's targetPort is a named port" )
471
481
}
@@ -487,7 +497,8 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckProtocol(targe
487
497
case elbv2gw .TargetGroupHealthCheckProtocolHTTPS :
488
498
return elbv2model .ProtocolHTTPS
489
499
default :
490
- return tgProtocol
500
+ // This should never happen, the CRD validation takes care of this.
501
+ return elbv2model .ProtocolHTTP
491
502
}
492
503
}
493
504
@@ -507,23 +518,25 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckPath(targetGro
507
518
return & builder .defaultHealthCheckPathHTTP
508
519
}
509
520
510
- func (builder * targetGroupBuilderImpl ) buildTargetGroupHealthCheckMatcher (targetGroupProps * elbv2gw.TargetGroupProps , hcProtocol elbv2model.Protocol ) * elbv2model.HealthCheckMatcher {
521
+ func (builder * targetGroupBuilderImpl ) buildTargetGroupHealthCheckMatcher (targetGroupProps * elbv2gw.TargetGroupProps , tgProtocolVersion * elbv2model. ProtocolVersion , hcProtocol elbv2model.Protocol ) * elbv2model.HealthCheckMatcher {
511
522
512
523
if hcProtocol == elbv2model .ProtocolTCP {
513
524
return nil
514
525
}
515
526
516
- if targetGroupProps != nil && targetGroupProps .ProtocolVersion != nil && string (* targetGroupProps .ProtocolVersion ) == string (elbv2model .ProtocolVersionGRPC ) {
527
+ useGRPC := tgProtocolVersion != nil && * tgProtocolVersion == elbv2model .ProtocolVersionGRPC
528
+
529
+ if useGRPC {
517
530
matcher := builder .defaultHealthCheckMatcherGRPCCode
518
- if targetGroupProps . ProtocolVersion != nil && targetGroupProps .HealthCheckConfig != nil && targetGroupProps .HealthCheckConfig .Matcher != nil && targetGroupProps .HealthCheckConfig .Matcher .GRPCCode != nil {
531
+ if targetGroupProps != nil && targetGroupProps .HealthCheckConfig != nil && targetGroupProps .HealthCheckConfig .Matcher != nil && targetGroupProps .HealthCheckConfig .Matcher .GRPCCode != nil {
519
532
matcher = * targetGroupProps .HealthCheckConfig .Matcher .GRPCCode
520
533
}
521
534
return & elbv2model.HealthCheckMatcher {
522
535
GRPCCode : & matcher ,
523
536
}
524
537
}
525
538
matcher := builder .defaultHealthCheckMatcherHTTPCode
526
- if targetGroupProps != nil && targetGroupProps .ProtocolVersion != nil && targetGroupProps . HealthCheckConfig != nil && targetGroupProps .HealthCheckConfig .Matcher != nil && targetGroupProps .HealthCheckConfig .Matcher .HTTPCode != nil {
539
+ if targetGroupProps != nil && targetGroupProps .HealthCheckConfig != nil && targetGroupProps .HealthCheckConfig .Matcher != nil && targetGroupProps .HealthCheckConfig .Matcher .HTTPCode != nil {
527
540
matcher = * targetGroupProps .HealthCheckConfig .Matcher .HTTPCode
528
541
}
529
542
return & elbv2model.HealthCheckMatcher {
@@ -570,9 +583,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupAttributes(targetGroupPro
570
583
attributeMap [attr .Key ] = attr .Value
571
584
}
572
585
573
- if builder .loadBalancerType == elbv2model .LoadBalancerTypeNetwork {
574
- builder .buildL4TargetGroupAttributes (& attributeMap , targetGroupProps )
575
- }
586
+ // TODO -- buildPreserveClientIPFlag Might need special logic
576
587
577
588
return attributeMap
578
589
}
@@ -588,22 +599,12 @@ func (builder *targetGroupBuilderImpl) convertMapToAttributes(attributeMap map[s
588
599
return convertedAttributes
589
600
}
590
601
591
- func (builder * targetGroupBuilderImpl ) buildL4TargetGroupAttributes (attributeMap * map [string ]string , targetGroupProps * elbv2gw.TargetGroupProps ) {
592
- if targetGroupProps == nil {
593
- return
594
- }
595
- // TODO -- buildPreserveClientIPFlag
596
- }
597
-
598
602
func (builder * targetGroupBuilderImpl ) buildTargetGroupResourceID (gwKey types.NamespacedName , svcKey types.NamespacedName , routeKey types.NamespacedName , port intstr.IntOrString ) string {
599
603
return fmt .Sprintf ("%s/%s:%s-%s:%s-%s:%s" , gwKey .Namespace , gwKey .Name , routeKey .Namespace , routeKey .Name , svcKey .Namespace , svcKey .Name , port .String ())
600
604
}
601
605
602
606
func (builder * targetGroupBuilderImpl ) buildTargetGroupBindingNodeSelector (tgProps * elbv2gw.TargetGroupProps , targetType elbv2model.TargetType ) * metav1.LabelSelector {
603
- if targetType != elbv2model .TargetTypeInstance {
604
- return nil
605
- }
606
- if tgProps == nil {
607
+ if targetType != elbv2model .TargetTypeInstance || tgProps == nil {
607
608
return nil
608
609
}
609
610
return tgProps .NodeSelector
0 commit comments