Skip to content

Commit 53d4f9b

Browse files
committed
refactor backend abstraction to allow for different backend types
1 parent 0c4c186 commit 53d4f9b

File tree

6 files changed

+230
-175
lines changed

6 files changed

+230
-175
lines changed

controllers/gateway/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gateway
33
import (
44
"context"
55
"fmt"
6+
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
67
"sort"
78
"strconv"
89
"strings"
@@ -15,7 +16,6 @@ import (
1516
"k8s.io/apimachinery/pkg/util/sets"
1617
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
1718
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils"
18-
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1919
"sigs.k8s.io/controller-runtime/pkg/client"
2020
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
2121
)
@@ -171,7 +171,9 @@ func getServicesFromRoutes(listenerRouteMap map[int32][]routeutils.RouteDescript
171171
for _, route := range routes {
172172
for _, rr := range route.GetAttachedRules() {
173173
for _, be := range rr.GetBackends() {
174-
res.Insert(k8s.NamespacedName(be.Service))
174+
if be.ServiceBackend != nil {
175+
res.Insert(k8s.NamespacedName(be.ServiceBackend.Service))
176+
}
175177
}
176178
}
177179
}

pkg/gateway/model/model_build_listener_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,11 @@ func Test_BuildListenerRules(t *testing.T) {
10151015
},
10161016
BackendRefs: []routeutils.Backend{
10171017
{
1018-
Service: &corev1.Service{},
1019-
ServicePort: &corev1.ServicePort{Name: "svcport"},
1020-
Weight: 1,
1018+
ServiceBackend: &routeutils.ServiceBackendConfig{
1019+
Service: &corev1.Service{},
1020+
ServicePort: &corev1.ServicePort{Name: "svcport"},
1021+
},
1022+
Weight: 1,
10211023
},
10221024
},
10231025
},
@@ -1089,9 +1091,11 @@ func Test_BuildListenerRules(t *testing.T) {
10891091
},
10901092
BackendRefs: []routeutils.Backend{
10911093
{
1092-
Service: &corev1.Service{},
1093-
ServicePort: &corev1.ServicePort{Name: "svcport"},
1094-
Weight: 1,
1094+
ServiceBackend: &routeutils.ServiceBackendConfig{
1095+
Service: &corev1.Service{},
1096+
ServicePort: &corev1.ServicePort{Name: "svcport"},
1097+
},
1098+
Weight: 1,
10951099
},
10961100
},
10971101
},
@@ -1150,9 +1154,11 @@ func Test_BuildListenerRules(t *testing.T) {
11501154
},
11511155
BackendRefs: []routeutils.Backend{
11521156
{
1153-
Service: &corev1.Service{},
1154-
ServicePort: &corev1.ServicePort{Name: "svcport"},
1155-
Weight: 1,
1157+
ServiceBackend: &routeutils.ServiceBackendConfig{
1158+
Service: &corev1.Service{},
1159+
ServicePort: &corev1.ServicePort{Name: "svcport"},
1160+
},
1161+
Weight: 1,
11561162
},
11571163
},
11581164
ListenerRuleConfig: &elbv2gw.ListenerRuleConfiguration{
@@ -1226,9 +1232,11 @@ func Test_BuildListenerRules(t *testing.T) {
12261232
},
12271233
BackendRefs: []routeutils.Backend{
12281234
{
1229-
Service: &corev1.Service{},
1230-
ServicePort: &corev1.ServicePort{Name: "svcport"},
1231-
Weight: 1,
1235+
ServiceBackend: &routeutils.ServiceBackendConfig{
1236+
Service: &corev1.Service{},
1237+
ServicePort: &corev1.ServicePort{Name: "svcport"},
1238+
},
1239+
Weight: 1,
12321240
},
12331241
},
12341242
ListenerRuleConfig: &elbv2gw.ListenerRuleConfiguration{

pkg/gateway/model/model_build_target_group.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func newTargetGroupBuilder(clusterName string, vpcId string, tagHelper tagHelper
100100
func (t *targetGroupBuilderImpl) buildTargetGroup(stack core.Stack,
101101
gw *gwv1.Gateway, lbConfig elbv2gw.LoadBalancerConfiguration, lbIPType elbv2model.IPAddressType, routeDescriptor routeutils.RouteDescriptor, backend routeutils.Backend, backendSGIDToken core.StringToken) (*elbv2model.TargetGroup, error) {
102102

103-
targetGroupProps := backend.ELBV2TargetGroupProps
104-
tgResID := t.buildTargetGroupResourceID(k8s.NamespacedName(gw), k8s.NamespacedName(backend.Service), routeDescriptor.GetRouteNamespacedName(), routeDescriptor.GetRouteKind(), backend.ServicePort.TargetPort)
103+
targetGroupProps := backend.ServiceBackend.ELBV2TargetGroupProps
104+
tgResID := t.buildTargetGroupResourceID(k8s.NamespacedName(gw), k8s.NamespacedName(backend.ServiceBackend.Service), routeDescriptor.GetRouteNamespacedName(), routeDescriptor.GetRouteKind(), backend.ServiceBackend.ServicePort.TargetPort)
105105
if tg, exists := t.tgByResID[tgResID]; exists {
106106
return tg, nil
107107
}
@@ -126,9 +126,9 @@ func (t *targetGroupBuilderImpl) buildTargetGroup(stack core.Stack,
126126

127127
func (builder *targetGroupBuilderImpl) buildTargetGroupBindingSpec(gw *gwv1.Gateway, tgProps *elbv2gw.TargetGroupProps, tgSpec elbv2model.TargetGroupSpec, nodeSelector *metav1.LabelSelector, backend routeutils.Backend, backendSGIDToken core.StringToken) elbv2modelk8s.TargetGroupBindingResourceSpec {
128128
targetType := elbv2api.TargetType(tgSpec.TargetType)
129-
targetPort := backend.ServicePort.TargetPort
129+
targetPort := backend.ServiceBackend.ServicePort.TargetPort
130130
if targetType == elbv2api.TargetTypeInstance {
131-
targetPort = intstr.FromInt32(backend.ServicePort.NodePort)
131+
targetPort = intstr.FromInt32(backend.ServiceBackend.ServicePort.NodePort)
132132
}
133133
tgbNetworking := builder.buildTargetGroupBindingNetworking(targetPort, *tgSpec.HealthCheckConfig.Port, tgSpec.Protocol, backendSGIDToken)
134134

@@ -154,7 +154,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingSpec(gw *gwv1.Gate
154154
return elbv2modelk8s.TargetGroupBindingResourceSpec{
155155
Template: elbv2modelk8s.TargetGroupBindingTemplate{
156156
ObjectMeta: metav1.ObjectMeta{
157-
Namespace: backend.Service.Namespace,
157+
Namespace: backend.ServiceBackend.Service.Namespace,
158158
Name: tgSpec.Name,
159159
Annotations: annotations,
160160
Labels: labels,
@@ -163,8 +163,8 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupBindingSpec(gw *gwv1.Gate
163163
TargetGroupARN: nil, // This should get filled in later!
164164
TargetType: &targetType,
165165
ServiceRef: elbv2api.ServiceReference{
166-
Name: backend.Service.Name,
167-
Port: intstr.FromInt32(backend.ServicePort.Port),
166+
Name: backend.ServiceBackend.Service.Name,
167+
Port: intstr.FromInt32(backend.ServiceBackend.ServicePort.Port),
168168
},
169169
Networking: tgbNetworking,
170170
NodeSelector: nodeSelector,
@@ -274,7 +274,7 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
274274
return elbv2model.TargetGroupSpec{}, err
275275
}
276276
tgAttributesMap := builder.buildTargetGroupAttributes(targetGroupProps)
277-
ipAddressType, err := builder.buildTargetGroupIPAddressType(backend.Service, lbIPType)
277+
ipAddressType, err := builder.buildTargetGroupIPAddressType(backend.ServiceBackend.Service, lbIPType)
278278
if err != nil {
279279
return elbv2model.TargetGroupSpec{}, err
280280
}
@@ -283,8 +283,8 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
283283
if err != nil {
284284
return elbv2model.TargetGroupSpec{}, err
285285
}
286-
tgPort := builder.buildTargetGroupPort(targetType, *backend.ServicePort)
287-
name := builder.buildTargetGroupName(targetGroupProps, k8s.NamespacedName(gw), route.GetRouteNamespacedName(), route.GetRouteKind(), k8s.NamespacedName(backend.Service), tgPort, targetType, tgProtocol, tgProtocolVersion)
286+
tgPort := builder.buildTargetGroupPort(targetType, *backend.ServiceBackend.ServicePort)
287+
name := builder.buildTargetGroupName(targetGroupProps, k8s.NamespacedName(gw), route.GetRouteNamespacedName(), route.GetRouteKind(), k8s.NamespacedName(backend.ServiceBackend.Service), tgPort, targetType, tgProtocol, tgProtocolVersion)
288288

289289
if tgPort == 0 {
290290
if targetType == elbv2model.TargetTypeIP {
@@ -467,11 +467,11 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupHealthCheckConfig(targetG
467467
// add ServiceExternalTrafficPolicyLocal support
468468
var isServiceExternalTrafficPolicyTypeLocal = false
469469
if targetType == elbv2model.TargetTypeInstance &&
470-
backend.Service.Spec.ExternalTrafficPolicy == corev1.ServiceExternalTrafficPolicyTypeLocal &&
470+
backend.ServiceBackend.Service.Spec.ExternalTrafficPolicy == corev1.ServiceExternalTrafficPolicyTypeLocal &&
471471
builder.loadBalancerType == elbv2model.LoadBalancerTypeNetwork {
472472
isServiceExternalTrafficPolicyTypeLocal = true
473473
}
474-
healthCheckPort, err := builder.buildTargetGroupHealthCheckPort(targetGroupProps, targetType, backend.Service, isServiceExternalTrafficPolicyTypeLocal)
474+
healthCheckPort, err := builder.buildTargetGroupHealthCheckPort(targetGroupProps, targetType, backend.ServiceBackend.Service, isServiceExternalTrafficPolicyTypeLocal)
475475
if err != nil {
476476
return elbv2model.TargetGroupHealthCheckConfig{}, err
477477
}

0 commit comments

Comments
 (0)