@@ -14,6 +14,8 @@ import (
14
14
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
15
15
16
16
apierrors "k8s.io/apimachinery/pkg/api/errors"
17
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
+ "k8s.io/apimachinery/pkg/labels"
17
19
"k8s.io/apimachinery/pkg/util/runtime"
18
20
"k8s.io/client-go/tools/cache"
19
21
"k8s.io/klog/v2"
@@ -195,7 +197,7 @@ func (c *Controller) syncGateway(key string) error {
195
197
196
198
// Update configuration
197
199
resources := map [resourcev3.Type ][]envoyproxytypes.Resource {}
198
-
200
+ conditions := []metav1. Condition {}
199
201
for _ , listener := range gw .Spec .Listeners {
200
202
// Determine the Envoy protocol based on the Gateway API protocol
201
203
var envoyProto corev3.SocketAddress_Protocol
@@ -217,9 +219,106 @@ func (c *Controller) syncGateway(key string) error {
217
219
}}},
218
220
})
219
221
222
+ // Process HTTP Routes
223
+ for _ , route := range c .getHTTPRoutesForListener (gw , listener ) {
224
+ klog .V (2 ).Infof ("Processing route %s/%s for gw %s/%s" , route .Namespace , route .Name , gw .Namespace , gw .Name )
225
+ conditions = append (conditions , metav1.Condition {})
226
+ }
227
+
228
+ for _ , route := range c .getHTTPRoutesForListener (gw , listener ) {
229
+ klog .V (2 ).Infof ("Processing route %s/%s for gw %s/%s" , route .Namespace , route .Name , gw .Namespace , gw .Name )
230
+ conditions = append (conditions , metav1.Condition {})
231
+ }
232
+
233
+ // Process GRPC Routes
220
234
}
221
235
return c .UpdateXDSServer (context .Background (), containerName , resources )
236
+ }
237
+
238
+ // getHTTPRoutesForListener returns a slice of HTTPRoutes that reference the given Gateway and listener.
239
+ func (c * Controller ) getHTTPRoutesForListener (gw * gatewayv1.Gateway , listener gatewayv1.Listener ) []* gatewayv1.HTTPRoute {
240
+ var matchingRoutes []* gatewayv1.HTTPRoute
241
+
242
+ // TODO: optimize this
243
+ // List all HTTPRoutes in all namespaces
244
+ httpRoutes , err := c .httprouteLister .List (labels .Everything ())
245
+ if err != nil {
246
+ klog .Infof ("failed to list HTTPRoutes: %v" , err )
247
+ return matchingRoutes
248
+ }
249
+
250
+ for _ , route := range httpRoutes {
251
+ // Check 1: Does the route *want* to attach to this specific listener?
252
+ // This verifies the route's parentRefs target this gateway and listener section/port.
253
+ if ! isRouteReferenced (gw , listener , route ) {
254
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: not referenced in ParentRefs" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
255
+ continue
256
+ }
257
+
258
+ // Check 2: Does the listener *allow* this route to attach?
259
+ // This verifies listener.spec.allowedRoutes (namespace and kind).
260
+ // Assumes c.namespaceLister is populated.
261
+ if ! isRouteAllowed (gw , listener , route , c .namespaceLister ) {
262
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: denied by AllowedRoutes" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
263
+ continue
264
+ }
265
+
266
+ // Check 3: Is the route kind compatible with the listener protocol?
267
+ // For this function specifically getting HTTPRoutes, the listener must accept HTTP or HTTPS.
268
+ if listener .Protocol != gatewayv1 .HTTPProtocolType && listener .Protocol != gatewayv1 .HTTPSProtocolType {
269
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: incompatible listener protocol %s" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name , listener .Protocol )
270
+ continue // Skip route if listener protocol isn't HTTP/HTTPS
271
+ }
272
+
273
+ // If all checks pass, add the route
274
+ matchingRoutes = append (matchingRoutes , route )
275
+ klog .V (4 ).Infof ("Route %s/%s matched for Gateway %s/%s Listener %s" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
276
+ }
277
+
278
+ return matchingRoutes
279
+ }
280
+
281
+ // getGRPCRoutesForListener returns a slice of GRPCRoutes that reference the given Gateway and listener.
282
+ func (c * Controller ) getGRPCRoutesForListener (gw * gatewayv1.Gateway , listener gatewayv1.Listener ) []* gatewayv1.GRPCRoute {
283
+ var matchingRoutes []* gatewayv1.GRPCRoute
284
+
285
+ // TODO: optimize this
286
+ // List all GRPCRoutes in all namespaces
287
+ grpcRoutes , err := c .grpcrouteLister .List (labels .Everything ())
288
+ if err != nil {
289
+ klog .Infof ("failed to list GRPCRoutes: %v" , err )
290
+ return matchingRoutes
291
+ }
292
+
293
+ for _ , route := range grpcRoutes {
294
+ // Check 1: Does the route *want* to attach to this specific listener?
295
+ // This verifies the route's parentRefs target this gateway and listener section/port.
296
+ if ! isRouteReferenced (gw , listener , route ) {
297
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: not referenced in ParentRefs" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
298
+ continue
299
+ }
300
+
301
+ // Check 2: Does the listener *allow* this route to attach?
302
+ // This verifies listener.spec.allowedRoutes (namespace and kind).
303
+ // Assumes c.namespaceLister is populated.
304
+ if ! isRouteAllowed (gw , listener , route , c .namespaceLister ) {
305
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: denied by AllowedRoutes" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
306
+ continue
307
+ }
308
+
309
+ // Check 3: Is the route kind compatible with the listener protocol?
310
+ // For this function specifically getting HTTPRoutes, the listener must accept HTTP or HTTPS.
311
+ if listener .Protocol != gatewayv1 .HTTPProtocolType && listener .Protocol != gatewayv1 .HTTPSProtocolType {
312
+ klog .V (5 ).Infof ("Route %s/%s skipped for Gateway %s/%s Listener %s: incompatible listener protocol %s" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name , listener .Protocol )
313
+ continue // Skip route if listener protocol isn't HTTP/HTTPS
314
+ }
315
+
316
+ // If all checks pass, add the route
317
+ matchingRoutes = append (matchingRoutes , route )
318
+ klog .V (4 ).Infof ("Route %s/%s matched for Gateway %s/%s Listener %s" , route .Namespace , route .Name , gw .Namespace , gw .Name , listener .Name )
319
+ }
222
320
321
+ return matchingRoutes
223
322
}
224
323
225
324
// handleErr checks if an error happened and makes sure we will retry later.
0 commit comments