Skip to content

Commit 6c07dc2

Browse files
asaubermhofstetter
authored andcommitted
gateway-api: remove pre-filtered namespaces concept
Move all listener-related namespace logic into the building of ListenerWithContext, regardless of listener source (Gateway or ListenerSet). This allowed us to remove the special casing in each of the ingestion codepaths that deal with different types of routes (HTTPRoute, TLSRoute, TCPRoute etc.) This also allowed us to remove the fallback construction of MergedListeners within the ingestion codepath. This sets up a future refactor where the related route filtering functions are methods of the ListenerWithContext type. Signed-off-by: Andrew Sauber <andrew.sauber@isovalent.com>
1 parent 7f9558a commit 6c07dc2

3 files changed

Lines changed: 87 additions & 90 deletions

File tree

operator/pkg/gateway-api/gateway_reconcile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
351351
GRPCRoutes: grpcRoutes,
352352
TCPRoutes: tcpRoutes,
353353
UDPRoutes: udpRoutes,
354-
Namespaces: namespaces,
355354
Services: servicesList.Items,
356355
ServiceImports: serviceImportsList.Items,
357356
ReferenceGrants: grants.Items,
@@ -808,9 +807,10 @@ func (r *gatewayReconciler) mergeListeners(
808807
var merged []ingestion.ListenerWithContext
809808
for _, listener := range gw.Spec.Listeners {
810809
merged = append(merged, ingestion.ListenerWithContext{
811-
Listener: listener,
812-
Source: gwSource,
813-
SourceGeneration: gw.Generation,
810+
Listener: listener,
811+
Source: gwSource,
812+
SourceGeneration: gw.Generation,
813+
AllowedNamespaces: resolveAllowedNamespaces(ctx, r.Client, gw.GetNamespace(), listener, scopedLog),
814814
})
815815
}
816816

operator/pkg/model/ingestion/gateway.go

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ type Input struct {
141141
TCPRoutes []gatewayv1.TCPRoute
142142
UDPRoutes []gatewayv1.UDPRoute
143143
ReferenceGrants []gatewayv1.ReferenceGrant
144-
Namespaces []corev1.Namespace
145144
Services []corev1.Service
146145
ServiceImports []mcsapiv1beta1.ServiceImport
147146
BackendTLSPolicyMap helpers.BackendTLSPolicyServiceMap
@@ -177,26 +176,7 @@ func GatewayAPI(log *slog.Logger, input Input) *model.Model {
177176
}
178177
}
179178

180-
namespaceLabels := helpers.NewNamespaceLabelIndex(input.Namespaces)
181179
listeners := input.MergedListeners
182-
// When MergedListeners is not provided, build it from the direct
183-
// Gateway-listeners
184-
if listeners == nil {
185-
gwSource := model.FullyQualifiedResource{
186-
Name: input.Gateway.GetName(),
187-
Namespace: input.Gateway.GetNamespace(),
188-
Group: gatewayv1.GroupVersion.Group,
189-
Version: gatewayv1.GroupVersion.Version,
190-
Kind: "Gateway",
191-
UID: string(input.Gateway.GetUID()),
192-
}
193-
for _, l := range input.Gateway.Spec.Listeners {
194-
listeners = append(listeners, ListenerWithContext{
195-
Listener: l,
196-
Source: gwSource,
197-
})
198-
}
199-
}
200180

201181
// Find all the listener host names, so that we can match them with the routes
202182
// Gateway API spec guarantees that the hostnames are unique across all listeners
@@ -219,17 +199,8 @@ func GatewayAPI(log *slog.Logger, input Input) *model.Model {
219199

220200
var httpRoutes []model.HTTPRoute
221201

222-
// (ajs) Note well, we are using the existence of AllowedNamespace
223-
// as a hint that this listener has already performed filtering for
224-
// routes based on AllowedNamespaces. We need to refactor this type
225-
// of assumption to not apply only to ListenerSets, and be a true
226-
// invariant expected by this code path. That is, move all such
227-
// validation out of the ingestion codepath and into a combined
228-
// validate-and-record status phase of the reconcile pipeline.
229-
namespacesPreFiltered := l.AllowedNamespaces != nil
230-
231-
httpRoutes = append(httpRoutes, toHTTPRoutes(log, l.Listener, l.Source.Namespace, namespaceLabels, namespacesPreFiltered, listenerHostnamesByProtocol, filteredHTTPRoutes, input.Services, input.ServiceImports, input.ReferenceGrants, input.BackendTLSPolicyMap)...)
232-
httpRoutes = append(httpRoutes, toGRPCRoutes(l.Listener, l.Source.Namespace, namespaceLabels, namespacesPreFiltered, listenerHostnamesByProtocol, filteredGRPCRoutes, input.Services, input.ServiceImports, input.ReferenceGrants)...)
202+
httpRoutes = append(httpRoutes, toHTTPRoutes(log, l.Listener, listenerHostnamesByProtocol, filteredHTTPRoutes, input.Services, input.ServiceImports, input.ReferenceGrants, input.BackendTLSPolicyMap)...)
203+
httpRoutes = append(httpRoutes, toGRPCRoutes(l.Listener, listenerHostnamesByProtocol, filteredGRPCRoutes, input.Services, input.ServiceImports, input.ReferenceGrants)...)
233204
m.HTTP = append(m.HTTP, model.HTTPListener{
234205
Name: string(l.Name),
235206
Sources: []model.FullyQualifiedResource{l.Source},
@@ -248,32 +219,30 @@ func GatewayAPI(log *slog.Logger, input Input) *model.Model {
248219
Sources: []model.FullyQualifiedResource{l.Source},
249220
Port: uint32(l.Port),
250221
Hostname: toHostname(l.Hostname),
251-
Routes: toTLSRoutes(l.Listener, l.Source.Namespace, namespaceLabels, namespacesPreFiltered, listenerHostnamesByProtocol, l.FilterTLSRoutes(input.TLSRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
222+
Routes: toTLSRoutes(l.Listener, listenerHostnamesByProtocol, l.FilterTLSRoutes(input.TLSRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
252223
Infrastructure: infra,
253224
Service: toServiceModel(input.GatewayClassConfig),
254225
})
255226
}
256227

257228
case gatewayv1.TCPProtocolType:
258-
namespacesPreFiltered := l.AllowedNamespaces != nil
259229
m.L4 = append(m.L4, model.L4Listener{
260230
Name: string(l.Name),
261231
Sources: []model.FullyQualifiedResource{l.Source},
262232
Port: uint32(l.Port),
263233
Protocol: model.L4ProtocolTCP,
264-
Routes: toTCPRoutes(l.Listener, l.Source.Namespace, namespaceLabels, namespacesPreFiltered, l.FilterTCPRoutes(input.TCPRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
234+
Routes: toTCPRoutes(l.Listener, l.FilterTCPRoutes(input.TCPRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
265235
Infrastructure: infra,
266236
Service: toServiceModel(input.GatewayClassConfig),
267237
})
268238

269239
case gatewayv1.UDPProtocolType:
270-
namespacesPreFiltered := l.AllowedNamespaces != nil
271240
m.L4 = append(m.L4, model.L4Listener{
272241
Name: string(l.Name),
273242
Sources: []model.FullyQualifiedResource{l.Source},
274243
Port: uint32(l.Port),
275244
Protocol: model.L4ProtocolUDP,
276-
Routes: toUDPRoutes(l.Listener, l.Source.Namespace, namespaceLabels, namespacesPreFiltered, l.FilterUDPRoutes(input.UDPRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
245+
Routes: toUDPRoutes(l.Listener, l.FilterUDPRoutes(input.UDPRoutes), input.Services, input.ServiceImports, input.ReferenceGrants),
277246
Infrastructure: infra,
278247
Service: toServiceModel(input.GatewayClassConfig),
279248
})
@@ -331,9 +300,6 @@ func getBackendServiceName(namespace string, services []corev1.Service, serviceI
331300

332301
func toHTTPRoutes(log *slog.Logger,
333302
listener gatewayv1.Listener,
334-
gatewayNamespace string,
335-
namespaceLabels helpers.NamespaceLabelIndex,
336-
namespacesPreFiltered bool,
337303
listenerHostnamesByProtocol map[gatewayv1.ProtocolType][]string,
338304
input []gatewayv1.HTTPRoute,
339305
services []corev1.Service,
@@ -347,10 +313,6 @@ func toHTTPRoutes(log *slog.Logger,
347313
continue
348314
}
349315

350-
if !namespacesPreFiltered && !helpers.IsListenerNamespaceAllowed(listener, r.GetNamespace(), gatewayNamespace, namespaceLabels) {
351-
continue
352-
}
353-
354316
allProtocolHostnames := listenerHostnamesByProtocol[listener.Protocol]
355317

356318
computedHost := model.ComputeHosts(toStringSlice(r.Spec.Hostnames), (*string)(listener.Hostname), allProtocolHostnames)
@@ -757,9 +719,6 @@ func toHTTPRetry(retry *gatewayv1.HTTPRouteRetry) *model.HTTPRetry {
757719
}
758720

759721
func toGRPCRoutes(listener gatewayv1beta1.Listener,
760-
gatewayNamespace string,
761-
namespaceLabels helpers.NamespaceLabelIndex,
762-
namespacesPreFiltered bool,
763722
listenerHostnamesByProtocol map[gatewayv1.ProtocolType][]string,
764723
input []gatewayv1.GRPCRoute,
765724
services []corev1.Service,
@@ -772,10 +731,6 @@ func toGRPCRoutes(listener gatewayv1beta1.Listener,
772731
continue
773732
}
774733

775-
if !namespacesPreFiltered && !helpers.IsListenerNamespaceAllowed(listener, r.GetNamespace(), gatewayNamespace, namespaceLabels) {
776-
continue
777-
}
778-
779734
allProtocolHostnames := listenerHostnamesByProtocol[listener.Protocol]
780735

781736
computedHost := model.ComputeHosts(toStringSlice(r.Spec.Hostnames), (*string)(listener.Hostname), allProtocolHostnames)
@@ -909,17 +864,13 @@ func extractGRPCRoutes(hostnames []string, grpcr gatewayv1.GRPCRoute, services [
909864
return grpcRoutes
910865
}
911866

912-
func toTLSRoutes(listener gatewayv1beta1.Listener, gatewayNamespace string, namespaceLabels helpers.NamespaceLabelIndex, namespacesPreFiltered bool, listenerHostnamesByProtocol map[gatewayv1.ProtocolType][]string, input []gatewayv1.TLSRoute, services []corev1.Service, serviceImports []mcsapiv1beta1.ServiceImport, grants []gatewayv1.ReferenceGrant) []model.TLSPassthroughRoute {
867+
func toTLSRoutes(listener gatewayv1beta1.Listener, listenerHostnamesByProtocol map[gatewayv1.ProtocolType][]string, input []gatewayv1.TLSRoute, services []corev1.Service, serviceImports []mcsapiv1beta1.ServiceImport, grants []gatewayv1.ReferenceGrant) []model.TLSPassthroughRoute {
913868
var tlsRoutes []model.TLSPassthroughRoute
914869
for _, r := range input {
915870
if !parentRefsMatchListener(r.Spec.ParentRefs, listener) {
916871
continue
917872
}
918873

919-
if !namespacesPreFiltered && !helpers.IsListenerNamespaceAllowed(listener, r.GetNamespace(), gatewayNamespace, namespaceLabels) {
920-
continue
921-
}
922-
923874
allProtocolHostnames := listenerHostnamesByProtocol[listener.Protocol]
924875
computedHost := model.ComputeHosts(toStringSlice(r.Spec.Hostnames), (*string)(listener.Hostname), allProtocolHostnames)
925876
// No matching host, skip this route
@@ -997,9 +948,6 @@ func sortL4RoutesByAge[T any](routes []T, meta func(T) metav1.ObjectMeta) {
997948
}
998949

999950
func toTCPRoutes(listener gatewayv1beta1.Listener,
1000-
gatewayNamespace string,
1001-
namespaceLabels helpers.NamespaceLabelIndex,
1002-
namespacesPreFiltered bool,
1003951
input []gatewayv1.TCPRoute,
1004952
services []corev1.Service,
1005953
serviceImports []mcsapiv1beta1.ServiceImport,
@@ -1013,9 +961,6 @@ func toTCPRoutes(listener gatewayv1beta1.Listener,
1013961
// Accepted=True (handled by the status reconciler) but route no traffic.
1014962
attached := make([]gatewayv1.TCPRoute, 0, len(input))
1015963
for _, r := range input {
1016-
if !namespacesPreFiltered && !helpers.IsListenerNamespaceAllowed(listener, r.GetNamespace(), gatewayNamespace, namespaceLabels) {
1017-
continue
1018-
}
1019964
if parentRefsMatchListener(r.Spec.ParentRefs, listener) {
1020965
attached = append(attached, r)
1021966
}
@@ -1061,9 +1006,6 @@ func toTCPRoutes(listener gatewayv1beta1.Listener,
10611006
}
10621007

10631008
func toUDPRoutes(listener gatewayv1beta1.Listener,
1064-
gatewayNamespace string,
1065-
namespaceLabels helpers.NamespaceLabelIndex,
1066-
namespacesPreFiltered bool,
10671009
input []gatewayv1.UDPRoute,
10681010
services []corev1.Service,
10691011
serviceImports []mcsapiv1beta1.ServiceImport,
@@ -1072,9 +1014,6 @@ func toUDPRoutes(listener gatewayv1beta1.Listener,
10721014
// Keep only the oldest attaching UDPRoute. See toTCPRoutes for the rationale.
10731015
attached := make([]gatewayv1.UDPRoute, 0, len(input))
10741016
for _, r := range input {
1075-
if !namespacesPreFiltered && !helpers.IsListenerNamespaceAllowed(listener, r.GetNamespace(), gatewayNamespace, namespaceLabels) {
1076-
continue
1077-
}
10781017
if parentRefsMatchListener(r.Spec.ParentRefs, listener) {
10791018
attached = append(attached, r)
10801019
}

0 commit comments

Comments
 (0)