Skip to content

Commit 7007927

Browse files
zac-nixonniclask25
authored and
niclask25
committed
[gw api] tg creation
1 parent 1ffc3e7 commit 7007927

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

apis/gateway/v1beta1/loadbalancerconfig_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ type LoadBalancerConfigurationSpec struct {
243243
// when you specify securityGroups
244244
// +optional
245245
ManageBackendSecurityGroupRules bool `json:"manageBackendSecurityGroupRules,omitempty"`
246+
247+
// EnableMultiCluster [Application / Network LoadBalancer]
248+
// All TargetGroupBindings attached to this Load Balancer will have multi cluster support enabled.
249+
// +optional
250+
EnableMultiCluster bool `json:"enableMultiCluster,omitempty"`
246251
}
247252

248253
// TODO -- these can be used to set what generation the gateway is currently on to track progress on reconcile.

apis/gateway/v1beta1/targetgroupconfig_types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ const (
112112
type TargetGroupHealthCheckProtocol string
113113

114114
const (
115-
TargetGroupHealthCheckProtocolHTTP TargetGroupHealthCheckProtocol = "http"
116-
TargetGroupHealthCheckProtocolHTTPS TargetGroupHealthCheckProtocol = "https"
117-
TargetGroupHealthCheckProtocolTCP TargetGroupHealthCheckProtocol = "tcp"
115+
TargetGroupHealthCheckProtocolHTTP TargetGroupHealthCheckProtocol = "HTTP"
116+
TargetGroupHealthCheckProtocolHTTPS TargetGroupHealthCheckProtocol = "HTTPS"
117+
TargetGroupHealthCheckProtocolTCP TargetGroupHealthCheckProtocol = "TCP"
118118
)
119119

120120
// +kubebuilder:validation:Enum=HTTP;HTTPS;TCP;TLS;UDP;TCP_UDP
@@ -133,9 +133,9 @@ const (
133133
type ProtocolVersion string
134134

135135
const (
136-
ProtocolVersionHTTP1 ProtocolVersion = "http1"
137-
ProtocolVersionHTTP2 ProtocolVersion = "http2"
138-
ProtocolVersionGRPC ProtocolVersion = "grpc"
136+
ProtocolVersionHTTP1 ProtocolVersion = "HTTP1"
137+
ProtocolVersionHTTP2 ProtocolVersion = "HTTP2"
138+
ProtocolVersionGRPC ProtocolVersion = "GRPC"
139139
)
140140

141141
// +kubebuilder:validation:Enum=none;prefer-route-specific;prefer-default

pkg/gateway/model/base_model_builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type baseModelBuilder struct {
5858
lbBuilder loadBalancerBuilder
5959
logger logr.Logger
6060

61+
tgByResID map[string]*elbv2model.TargetGroup
62+
6163
subnetBuilder subnetModelBuilder
6264
securityGroupBuilder securityGroupBuilder
6365
tgBuilder targetGroupBuilder

pkg/gateway/model/model_build_target_group.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroup(tgByResID *map[string]bu
8989
return buildTargetGroupOutput{}, err
9090
}
9191
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)
9393

9494
output := buildTargetGroupOutput{
9595
targetGroupSpec: tgSpec,
@@ -109,7 +109,7 @@ func (builder *targetGroupBuilderImpl) getTargetGroupProps(routeDescriptor route
109109
return targetGroupProps
110110
}
111111

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 {
113113
targetType := elbv2api.TargetType(tgSpec.TargetType)
114114
targetPort := backend.ServicePort.TargetPort
115115
if targetType == elbv2api.TargetTypeInstance {
@@ -142,14 +142,14 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingSpec(lbConfig *elb
142142
}
143143
}
144144

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 {
146146
if backendSGIDToken == nil {
147147
return nil
148148
}
149149
protocolTCP := elbv2api.NetworkingProtocolTCP
150150
protocolUDP := elbv2api.NetworkingProtocolUDP
151151

152-
udpSupported := port.Protocol == corev1.ProtocolUDP
152+
udpSupported := svcPort.Protocol == corev1.ProtocolUDP
153153

154154
if builder.disableRestrictedSGRules {
155155
ports := []elbv2api.NetworkingPort{
@@ -183,7 +183,6 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingNetworking(targetP
183183
}
184184

185185
var networkingPorts []elbv2api.NetworkingPort
186-
var networkingRules []elbv2model.NetworkingIngressRule
187186

188187
protocolToUse := &protocolTCP
189188
if udpSupported {
@@ -209,6 +208,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingNetworking(targetP
209208
})
210209
}
211210

211+
var networkingRules []elbv2model.NetworkingIngressRule
212212
for _, port := range networkingPorts {
213213
networkingRules = append(networkingRules, elbv2model.NetworkingIngressRule{
214214
From: []elbv2model.NetworkingPeer{
@@ -232,7 +232,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
232232
if err != nil {
233233
return elbv2model.TargetGroupSpec{}, err
234234
}
235-
tgProtocolVersion := builder.buildTargetGroupProtocolVersion(targetGroupProps)
235+
tgProtocolVersion := builder.buildTargetGroupProtocolVersion(targetGroupProps, route)
236236

237237
healthCheckConfig, err := builder.buildTargetGroupHealthCheckConfig(targetGroupProps, tgProtocol, tgProtocolVersion, targetType, backend)
238238
if err != nil {
@@ -249,8 +249,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
249249
return elbv2model.TargetGroupSpec{}, err
250250
}
251251
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)
254253
return elbv2model.TargetGroupSpec{
255254
Name: name,
256255
TargetType: targetType,
@@ -268,7 +267,7 @@ var invalidTargetGroupNamePattern = regexp.MustCompile("[[:^alnum:]]")
268267

269268
// buildTargetGroupName will calculate the targetGroup's name.
270269
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,
272271
targetType elbv2model.TargetType, tgProtocol elbv2model.Protocol, tgProtocolVersion *elbv2model.ProtocolVersion) string {
273272

274273
if targetGroupProps != nil && targetGroupProps.TargetGroupName != "" {
@@ -283,7 +282,6 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupName(targetGroupProps *el
283282
_, _ = uuidHash.Write([]byte(routeKey.Name))
284283
_, _ = uuidHash.Write([]byte(svcKey.Namespace))
285284
_, _ = uuidHash.Write([]byte(svcKey.Name))
286-
_, _ = uuidHash.Write([]byte(port.String()))
287285
_, _ = uuidHash.Write([]byte(strconv.Itoa(int(tgPort))))
288286
_, _ = uuidHash.Write([]byte(targetType))
289287
_, _ = uuidHash.Write([]byte(tgProtocol))
@@ -365,10 +363,7 @@ func (builder *targetGroupBuilderImpl) buildL7TargetGroupProtocol(targetGroupPro
365363
}
366364

367365
func (builder *targetGroupBuilderImpl) buildL4TargetGroupProtocol(targetGroupProps *elbv2gw.TargetGroupProps, route routeutils.RouteDescriptor) (elbv2model.Protocol, error) {
368-
// TODO, auto infer?
369366
if targetGroupProps == nil || targetGroupProps.Protocol == nil {
370-
// infer this somehow!?
371-
// use the backend config to get the protocol type.
372367
return builder.inferTargetGroupProtocolFromRoute(route), nil
373368
}
374369

@@ -406,7 +401,12 @@ func (builder *targetGroupBuilderImpl) inferTargetGroupProtocolFromRoute(route r
406401
return elbv2model.ProtocolTCP
407402
}
408403

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 {
410410
// NLB doesn't support protocol version
411411
if builder.loadBalancerType == elbv2model.LoadBalancerTypeNetwork {
412412
return nil
@@ -416,7 +416,11 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupProtocolVersion(targetGro
416416
pv := elbv2model.ProtocolVersion(*targetGroupProps.ProtocolVersion)
417417
return &pv
418418
}
419-
http1 := elbv2model.ProtocolVersionHTTP1
419+
420+
if route.GetRouteKind() == routeutils.GRPCRouteKind {
421+
return &grpc
422+
}
423+
420424
return &http1
421425
}
422426

@@ -425,13 +429,13 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckConfig(targetG
425429
// https://github.com/kubernetes-sigs/gateway-api/issues/451
426430
// Gateway API doesn't have the same ServiceExternalTrafficPolicyLocal support.
427431
// 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)
429433
if err != nil {
430434
return elbv2model.TargetGroupHealthCheckConfig{}, err
431435
}
432436
healthCheckProtocol := builder.buildTargetGroupHealthCheckProtocol(targetGroupProps, tgProtocol)
433437
healthCheckPath := builder.buildTargetGroupHealthCheckPath(targetGroupProps, tgProtocolVersion, healthCheckProtocol)
434-
healthCheckMatcher := builder.buildTargetGroupHealthCheckMatcher(targetGroupProps, healthCheckProtocol)
438+
healthCheckMatcher := builder.buildTargetGroupHealthCheckMatcher(targetGroupProps, tgProtocolVersion, healthCheckProtocol)
435439
healthCheckIntervalSeconds := builder.buildTargetGroupHealthCheckIntervalSeconds(targetGroupProps)
436440
healthCheckTimeoutSeconds := builder.buildTargetGroupHealthCheckTimeoutSeconds(targetGroupProps)
437441
healthCheckHealthyThresholdCount := builder.buildTargetGroupHealthCheckHealthyThresholdCount(targetGroupProps)
@@ -450,22 +454,28 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckConfig(targetG
450454
return hcConfig, nil
451455
}
452456

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 {
455461
return intstr.FromString(shared_constants.HealthCheckPortTrafficPort), nil
456462
}
457463

458464
healthCheckPort := intstr.Parse(*targetGroupProps.HealthCheckConfig.HealthCheckPort)
459465
if healthCheckPort.Type == intstr.Int {
460466
return healthCheckPort, nil
461467
}
468+
hcSvcPort, err := k8s.LookupServicePort(svc, healthCheckPort)
469+
if err != nil {
470+
return intstr.FromString(""), err
471+
}
462472

463-
/* TODO - Zac revisit this? */
464473
if targetType == elbv2model.TargetTypeInstance {
465-
return intstr.FromInt(int(backend.ServicePort.NodePort)), nil
474+
return intstr.FromInt(int(hcSvcPort.NodePort)), nil
466475
}
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
469479
}
470480
return intstr.IntOrString{}, errors.New("cannot use named healthCheckPort for IP TargetType when service's targetPort is a named port")
471481
}
@@ -487,7 +497,8 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckProtocol(targe
487497
case elbv2gw.TargetGroupHealthCheckProtocolHTTPS:
488498
return elbv2model.ProtocolHTTPS
489499
default:
490-
return tgProtocol
500+
// This should never happen, the CRD validation takes care of this.
501+
return elbv2model.ProtocolHTTP
491502
}
492503
}
493504

@@ -507,23 +518,25 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckPath(targetGro
507518
return &builder.defaultHealthCheckPathHTTP
508519
}
509520

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 {
511522

512523
if hcProtocol == elbv2model.ProtocolTCP {
513524
return nil
514525
}
515526

516-
if targetGroupProps != nil && targetGroupProps.ProtocolVersion != nil && string(*targetGroupProps.ProtocolVersion) == string(elbv2model.ProtocolVersionGRPC) {
527+
useGRPC := tgProtocolVersion != nil && *tgProtocolVersion == elbv2model.ProtocolVersionGRPC
528+
529+
if useGRPC {
517530
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 {
519532
matcher = *targetGroupProps.HealthCheckConfig.Matcher.GRPCCode
520533
}
521534
return &elbv2model.HealthCheckMatcher{
522535
GRPCCode: &matcher,
523536
}
524537
}
525538
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 {
527540
matcher = *targetGroupProps.HealthCheckConfig.Matcher.HTTPCode
528541
}
529542
return &elbv2model.HealthCheckMatcher{
@@ -570,9 +583,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupAttributes(targetGroupPro
570583
attributeMap[attr.Key] = attr.Value
571584
}
572585

573-
if builder.loadBalancerType == elbv2model.LoadBalancerTypeNetwork {
574-
builder.buildL4TargetGroupAttributes(&attributeMap, targetGroupProps)
575-
}
586+
// TODO -- buildPreserveClientIPFlag Might need special logic
576587

577588
return attributeMap
578589
}
@@ -588,22 +599,12 @@ func (builder *targetGroupBuilderImpl) convertMapToAttributes(attributeMap map[s
588599
return convertedAttributes
589600
}
590601

591-
func (builder *targetGroupBuilderImpl) buildL4TargetGroupAttributes(attributeMap *map[string]string, targetGroupProps *elbv2gw.TargetGroupProps) {
592-
if targetGroupProps == nil {
593-
return
594-
}
595-
// TODO -- buildPreserveClientIPFlag
596-
}
597-
598602
func (builder *targetGroupBuilderImpl) buildTargetGroupResourceID(gwKey types.NamespacedName, svcKey types.NamespacedName, routeKey types.NamespacedName, port intstr.IntOrString) string {
599603
return fmt.Sprintf("%s/%s:%s-%s:%s-%s:%s", gwKey.Namespace, gwKey.Name, routeKey.Namespace, routeKey.Name, svcKey.Namespace, svcKey.Name, port.String())
600604
}
601605

602606
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 {
607608
return nil
608609
}
609610
return tgProps.NodeSelector

0 commit comments

Comments
 (0)