Skip to content

Commit db23fd9

Browse files
committed
add documentation
1 parent 0271ea8 commit db23fd9

16 files changed

+91
-13
lines changed

pkg/gateway/routeutils/backend.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1111
)
1212

13+
// Backend an abstraction on the Gateway Backend, meant to hide the underlying backend type from consumers (unless they really want to see it :))
1314
type Backend struct {
1415
Service *corev1.Service
1516
ServicePort *corev1.ServicePort
@@ -18,7 +19,12 @@ type Backend struct {
1819
// Add TG config here //
1920
}
2021

22+
// TODOs:
23+
// 1/ Add reference grant checking
24+
// 2/ Add target group configuration resolution
25+
2126
// NOTE: Currently routeKind is not used, however, we will need it to load TG specific configuration.
27+
// commonBackendLoader this function will load the services and target group configurations associated with this gateway backend.
2228
func commonBackendLoader(ctx context.Context, k8sClient client.Client, typeSpecificBackend interface{}, backendRef gwv1.BackendRef, routeIdentifier types.NamespacedName, routeKind string) (*Backend, error) {
2329

2430
// We only support references of type service.

pkg/gateway/routeutils/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
77
)
88

9+
// Route Kinds
910
const (
1011
TCPRouteKind = "TCPRoute"
1112
UDPRouteKind = "UDPRoute"
@@ -14,6 +15,7 @@ const (
1415
GRPCRouteKind = "GRPCRoute"
1516
)
1617

18+
// RouteKind to Route Loader. These functions will pull data directly from the kube api or local cache.
1719
var allRoutes = map[string]func(context context.Context, client client.Client) ([]preLoadRouteDescriptor, error){
1820
TCPRouteKind: ListTCPRoutes,
1921
UDPRouteKind: ListUDPRoutes,
@@ -22,6 +24,7 @@ var allRoutes = map[string]func(context context.Context, client client.Client) (
2224
GRPCRouteKind: ListGRPCRoutes,
2325
}
2426

27+
// Default protocol map used to infer accepted route kinds when a listener doesn't specify the `allowedRoutes` field.
2528
var defaultProtocolToRouteKindMap = map[gwv1.ProtocolType]string{
2629
gwv1.TCPProtocolType: TCPRouteKind,
2730
gwv1.UDPProtocolType: UDPRouteKind,

pkg/gateway/routeutils/descriptor.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
88
)
99

10+
// routeMetadataDescriptor a common set of functions that will describe a route.
11+
// These are intentionally meant to be type agnostic;
12+
// however, consumers can use `GetRawRoute()` to inspect the actual route fields if needed.
1013
type routeMetadataDescriptor interface {
1114
GetRouteNamespacedName() types.NamespacedName
1215
GetRouteKind() string
@@ -15,11 +18,17 @@ type routeMetadataDescriptor interface {
1518
GetRawRoute() interface{}
1619
}
1720

21+
// preLoadRouteDescriptor this object is used to represent a route description that has not loaded its child data (services, tg config)
22+
// generally use this interface to represent broad data, filter that data down to the absolutely required data, and the call
23+
// loadAttachedRules() to generate a full route description.
1824
type preLoadRouteDescriptor interface {
1925
routeMetadataDescriptor
2026
loadAttachedRules(context context.Context, k8sClient client.Client) (RouteDescriptor, error)
2127
}
2228

29+
// RouteDescriptor is a type agnostic representation of a Gateway Route.
30+
// This interface holds all data necessary to construct
31+
// an ELBv2 object out of Kubernetes objects.
2332
type RouteDescriptor interface {
2433
routeMetadataDescriptor
2534
GetAttachedRules() []RouteRule

pkg/gateway/routeutils/grpc.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
99
)
1010

11+
/*
12+
This class holds the representation of a GRPC route.
13+
Generally, outside consumers will use GetRawRouteRule to inspect the
14+
GRPC specific features of the route.
15+
*/
16+
1117
/* Route Rule */
1218

1319
var _ RouteRule = &convertedGRPCRouteRule{}

pkg/gateway/routeutils/http.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
99
)
1010

11+
/*
12+
This class holds the representation of an HTTP route.
13+
Generally, outside consumers will use GetRawRouteRule to inspect the
14+
HTTP specific features of the route.
15+
*/
16+
1117
/* Route Rule */
1218

1319
var _ RouteRule = &convertedHTTPRouteRule{}

pkg/gateway/routeutils/listener_attachment_helper.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,43 @@ package routeutils
22

33
import (
44
"context"
5-
"fmt"
65
"k8s.io/apimachinery/pkg/util/sets"
76
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
87
)
98

9+
// listenerAttachmentHelper is an internal utility interface that can be used to determine if a listener will allow
10+
// a route to attach to it.
1011
type listenerAttachmentHelper interface {
1112
listenerAllowsAttachment(ctx context.Context, gw gwv1.Gateway, listener gwv1.Listener, route preLoadRouteDescriptor) (bool, error)
1213
}
1314

1415
var _ listenerAttachmentHelper = &listenerAttachmentHelperImpl{}
1516

17+
// listenerAttachmentHelperImpl implements the listenerAttachmentHelper interface.
1618
type listenerAttachmentHelperImpl struct {
1719
namespaceSelector namespaceSelector
1820
}
1921

22+
// listenerAllowsAttachment utility method to determine if a listener will allow a route to connect using
23+
// Gateway API rules to determine compatibility between lister and route.
2024
func (attachmentHelper *listenerAttachmentHelperImpl) listenerAllowsAttachment(ctx context.Context, gw gwv1.Gateway, listener gwv1.Listener, route preLoadRouteDescriptor) (bool, error) {
2125
namespaceOK, err := attachmentHelper.namespaceCheck(ctx, gw, listener, route)
2226
if err != nil {
2327
return false, err
2428
}
2529

2630
if !namespaceOK {
27-
fmt.Printf("name ok is false\n")
2831
return false, nil
2932
}
3033

3134
if !attachmentHelper.kindCheck(listener, route) {
32-
fmt.Printf("kind ok is false\n")
3335
return false, nil
3436
}
3537
return true, nil
3638
}
3739

40+
// namespaceCheck namespace check implements the Gateway API spec for namespace matching between listener
41+
// and route to determine compatibility.
3842
func (attachmentHelper *listenerAttachmentHelperImpl) namespaceCheck(ctx context.Context, gw gwv1.Gateway, listener gwv1.Listener, route preLoadRouteDescriptor) (bool, error) {
3943
var allowedNamespaces gwv1.FromNamespaces
4044

@@ -71,6 +75,8 @@ func (attachmentHelper *listenerAttachmentHelperImpl) namespaceCheck(ctx context
7175
}
7276
}
7377

78+
// kindCheck kind check implements the Gateway API spec for kindCheck matching between listener
79+
// and route to determine compatibility.
7480
func (attachmentHelper *listenerAttachmentHelperImpl) kindCheck(listener gwv1.Listener, route preLoadRouteDescriptor) bool {
7581

7682
var allowedRoutes sets.Set[string]

pkg/gateway/routeutils/loader.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
99
)
1010

11+
// LoadRouteFilter is an interface that consumers can use to tell the loader which routes to load.
1112
type LoadRouteFilter interface {
1213
IsApplicable(kind string) bool
1314
}
1415

16+
// routeFilterImpl implements LoadRouteFilter
1517
type routeFilterImpl struct {
1618
acceptedKinds sets.Set[string]
1719
}
@@ -41,18 +43,21 @@ var L7RouteFilter LoadRouteFilter = &routeFilterImpl{
4143
acceptedKinds: sets.New(HTTPRouteKind, GRPCRouteKind),
4244
}
4345

46+
// Loader will load all data Kubernetes that are pertinent to a gateway (Routes, Services, Target Group Configurations).
47+
// It will output the data using a map which maps listener port to the various routing rules for that port.
4448
type Loader interface {
4549
LoadRoutesForGateway(ctx context.Context, gw gwv1.Gateway, filter LoadRouteFilter) (map[int][]RouteDescriptor, error)
4650
}
4751

4852
var _ Loader = &loaderImpl{}
4953

5054
type loaderImpl struct {
51-
mapper ListenerToRouteMapper
55+
mapper listenerToRouteMapper
5256
k8sClient client.Client
5357
allRouteLoaders map[string]func(context context.Context, client client.Client) ([]preLoadRouteDescriptor, error)
5458
}
5559

60+
// LoadRoutesForGateway loads all relevant data for a single Gateway.
5661
func (l *loaderImpl) LoadRoutesForGateway(ctx context.Context, gw gwv1.Gateway, filter LoadRouteFilter) (map[int][]RouteDescriptor, error) {
5762
// 1. Load all relevant routes according to the filter
5863
loadedRoutes := make([]preLoadRouteDescriptor, 0)
@@ -68,7 +73,7 @@ func (l *loaderImpl) LoadRoutesForGateway(ctx context.Context, gw gwv1.Gateway,
6873

6974
// 2. Remove routes that aren't granted attachment by the listener.
7075
// Map any routes that are granted attachment to the listener port that allows the attachment.
71-
mappedRoutes, err := l.mapper.Map(ctx, gw, loadedRoutes)
76+
mappedRoutes, err := l.mapper.mapGatewayAndRoutes(ctx, gw, loadedRoutes)
7277
if err != nil {
7378
return nil, err
7479
}
@@ -77,6 +82,7 @@ func (l *loaderImpl) LoadRoutesForGateway(ctx context.Context, gw gwv1.Gateway,
7782
return l.loadChildResources(ctx, mappedRoutes)
7883
}
7984

85+
// loadChildResources responsible for loading all resources that a route descriptor references.
8086
func (l *loaderImpl) loadChildResources(ctx context.Context, preloadedRoutes map[int][]preLoadRouteDescriptor) (map[int][]RouteDescriptor, error) {
8187
// Cache to reduce duplicate route look ups.
8288
// Kind -> [NamespacedName:Previously Loaded Descriptor]

pkg/gateway/routeutils/loader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type mockMapper struct {
1616
mapToReturn map[int][]preLoadRouteDescriptor
1717
}
1818

19-
func (m *mockMapper) Map(context context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error) {
19+
func (m *mockMapper) mapGatewayAndRoutes(context context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error) {
2020
assert.ElementsMatch(m.t, m.expectedRoutes, routes)
2121
return m.mapToReturn, nil
2222
}

pkg/gateway/routeutils/namespace_selector.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/client"
99
)
1010

11+
// namespaceSelector is an internal utility
12+
// that is responsible for transforming a label selector into the all relevant namespaces
13+
// that match the selector criteria.
1114
type namespaceSelector interface {
1215
getNamespacesFromSelector(context context.Context, selector *metav1.LabelSelector) (sets.Set[string], error)
1316
}
@@ -18,6 +21,7 @@ type namespaceSelectorImpl struct {
1821
k8sClient client.Client
1922
}
2023

24+
// getNamespacesFromSelector queries the Kubernetes API for all namespaces that match a selector.
2125
func (n *namespaceSelectorImpl) getNamespacesFromSelector(context context.Context, selector *metav1.LabelSelector) (sets.Set[string], error) {
2226
namespaceList := v1.NamespaceList{}
2327

pkg/gateway/routeutils/route_attachment_helper.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
55
)
66

7+
// routeAttachmentHelper is an internal utility that is responsible for providing functionality related to route filtering.
78
type routeAttachmentHelper interface {
89
doesRouteAttachToGateway(gw gwv1.Gateway, route preLoadRouteDescriptor) bool
910
routeAllowsAttachmentToListener(listener gwv1.Listener, route preLoadRouteDescriptor) bool
@@ -14,6 +15,8 @@ var _ routeAttachmentHelper = &routeAttachmentHelperImpl{}
1415
type routeAttachmentHelperImpl struct {
1516
}
1617

18+
// doesRouteAttachToGateway is responsible for determining if a route and gateway should be connected.
19+
// This function implements the Gateway API spec for determining Gateway -> Route attachment.
1720
func (rah *routeAttachmentHelperImpl) doesRouteAttachToGateway(gw gwv1.Gateway, route preLoadRouteDescriptor) bool {
1821
for _, parentRef := range route.GetParentRefs() {
1922

@@ -38,6 +41,10 @@ func (rah *routeAttachmentHelperImpl) doesRouteAttachToGateway(gw gwv1.Gateway,
3841
return false
3942
}
4043

44+
// routeAllowsAttachmentToListener is responsible for determining if a route and listener should be connected. This function is slightly different than
45+
// listenerAttachmentHelper as it handles listener -> route relationships. This utility handles route -> listener relationships.
46+
// In order for a relationship to be established, both listener and route must agree to the connection.
47+
// This function implements the Gateway API spec for route -> listener attachment.
4148
// This function assumes that the caller has already validated that the gateway that owns the listener allows for route
4249
// attachment.
4350
func (rah *routeAttachmentHelperImpl) routeAllowsAttachmentToListener(listener gwv1.Listener, route preLoadRouteDescriptor) bool {

pkg/gateway/routeutils/route_listener_mapper.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,37 @@ import (
55
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
66
)
77

8-
type ListenerToRouteMapper interface {
9-
Map(context context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error)
8+
// listenerToRouteMapper is an internal utility that will map a list of routes to the listeners of a gateway
9+
// if the gateway and/or route are incompatible, then route is discarded.
10+
type listenerToRouteMapper interface {
11+
mapGatewayAndRoutes(context context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error)
1012
}
1113

12-
var _ ListenerToRouteMapper = &listenerToRouteMapperImpl{}
14+
var _ listenerToRouteMapper = &listenerToRouteMapperImpl{}
1315

1416
type listenerToRouteMapperImpl struct {
1517
listenerAttachmentHelper listenerAttachmentHelper
1618
routeAttachmentHelper routeAttachmentHelper
1719
}
1820

19-
func (ltr *listenerToRouteMapperImpl) Map(ctx context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error) {
21+
// mapGatewayAndRoutes will map route to the corresponding listener ports using the Gateway API spec rules.
22+
func (ltr *listenerToRouteMapperImpl) mapGatewayAndRoutes(ctx context.Context, gw gwv1.Gateway, routes []preLoadRouteDescriptor) (map[int][]preLoadRouteDescriptor, error) {
2023
result := make(map[int][]preLoadRouteDescriptor)
2124

25+
// First filter out any routes that are not intended for this Gateway.
2226
routesForGateway := make([]preLoadRouteDescriptor, 0)
2327
for _, route := range routes {
2428
if ltr.routeAttachmentHelper.doesRouteAttachToGateway(gw, route) {
2529
routesForGateway = append(routesForGateway, route)
2630
}
2731
}
2832

29-
// Approach is to greedily add as many relevant routes to each listener.
33+
// Next, greedily looking for the route to attach to.
3034
for _, listener := range gw.Spec.Listeners {
3135
for _, route := range routesForGateway {
3236

37+
// We need to check both paths (route -> listener) and (listener -> route)
38+
// for connection viability.
3339
if !ltr.routeAttachmentHelper.routeAllowsAttachmentToListener(listener, route) {
3440
continue
3541
}

pkg/gateway/routeutils/route_listener_mapper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (m *mockRouteAttachmentHelper) routeAllowsAttachmentToListener(listener gwv
4343
return m.routeListenerMap[k]
4444
}
4545

46-
func Test_Map(t *testing.T) {
46+
func Test_mapGatewayAndRoutes(t *testing.T) {
4747

4848
route1 := convertHTTPRoute(gwv1.HTTPRoute{
4949
ObjectMeta: metav1.ObjectMeta{
@@ -311,7 +311,7 @@ func Test_Map(t *testing.T) {
311311
},
312312
}
313313

314-
result, err := mapper.Map(context.Background(), tc.gw, tc.routes)
314+
result, err := mapper.mapGatewayAndRoutes(context.Background(), tc.gw, tc.routes)
315315

316316
if tc.expectErr {
317317
assert.Error(t, err)

pkg/gateway/routeutils/route_rule.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
55
)
66

7+
// RouteRule is a type agnostic representation of Routing Rules.
78
type RouteRule interface {
89
GetRawRouteRule() interface{}
910
GetSectionName() *gwv1.SectionName

pkg/gateway/routeutils/tcp.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
gwalpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1010
)
1111

12+
/*
13+
This class holds the representation of an TCP route.
14+
Generally, outside consumers will use GetRawRouteRule to inspect the
15+
TCP specific features of the route.
16+
*/
17+
1218
/* Route Rule */
1319

1420
var _ RouteRule = &convertedTCPRouteRule{}

pkg/gateway/routeutils/tls.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
gwalpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1010
)
1111

12+
/*
13+
This class holds the representation of an TLS route.
14+
Generally, outside consumers will use GetRawRouteRule to inspect the
15+
TLS specific features of the route.
16+
*/
17+
1218
/* Route Rule */
1319

1420
var _ RouteRule = &convertedTLSRouteRule{}

pkg/gateway/routeutils/udp.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
gwalpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1010
)
1111

12+
/*
13+
This class holds the representation of an UDP route.
14+
Generally, outside consumers will use GetRawRouteRule to inspect the
15+
UDP specific features of the route.
16+
*/
17+
1218
/* Route Rule */
1319

1420
var _ RouteRule = &convertedUDPRouteRule{}

0 commit comments

Comments
 (0)