Sub topic 11
Controlling which routes can attach
This chapter assumes you can read Gateway and HTTPRoute manifests and know how parentRefs points a route at a Gateway. Here you will see how a Gateway decides which routes it accepts.
Why attachment needs a gate
A route names its parent with parentRefs. That alone does not bind it. The Gateway must also allow the route. Without this check, any team that can create an HTTPRoute could attach to a shared Gateway and claim its hostnames.
Each listener has an allowedRoutes field. It sets two limits: which namespaces may attach, and which route kinds may attach.
Restricting namespaces
The allowedRoutes.namespaces.from field takes one of three values.
| Value | Routes accepted from |
|---|---|
| `Same` | Only the Gateway's own namespace. This is the default. |
| `All` | Every namespace in the cluster. |
| `Selector` | Namespaces whose labels match a `selector`. |
The Gateway from the earlier chapters uses Selector:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: web-gateway
namespace: infra
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
shared-gateway-access: "true"Only routes in namespaces labeled shared-gateway-access=true can attach to the http listener. The shop-team namespace needs that label before its HTTPRoute binds:
kubectl label namespace shop-team shared-gateway-access=trueThe cluster operator controls namespace labels, so this puts the decision in the operator's hands rather than the application team's.
Restricting route kinds
Add kinds to limit which route types a listener serves. This listener accepts only HTTPRoutes:
allowedRoutes:
kinds:
- kind: HTTPRouteIf you omit kinds, the listener accepts the route kinds that fit its protocol. An HTTP listener, for example, accepts HTTPRoute.
How the check works
The listener also checks that the route's hostnames overlap with the listener's hostname, if the listener sets one. A route can pass the namespace check and still fail on hostname.
Common errors
- A missing namespace label. The route exists, but nothing serves it. The cause is usually a namespace that lacks the label the selector requires.
- Assuming
Sameis open. A route inshop-teamcannot attach to a Gateway ininfraunless the listener setsAllorSelector. - Setting
Allon a shared Gateway. Every namespace can then attach. UseSelectorunless you trust every team in the cluster.
A rejected route does not produce an error when you apply it. The failure shows up in the route's status, as the next chapter explains. To check now, run:
kubectl describe httproute -n shop-team shopLook under the parent status for the Accepted condition. A False value with the reason NotAllowedByListeners means the listener's allowedRoutes rejected the route.
Exercise
An HTTPRoute in namespace team-a names a Gateway in namespace infra in its parentRefs. The Gateway's listener sets no allowedRoutes. The route never binds. Why, and what are two ways to fix it?
Show answer
With no allowedRoutes, the listener uses the default namespaces.from: Same, so only routes in infra can attach. One fix is to set from: All on the listener. The safer fix is from: Selector with a label such as shared-gateway-access: "true", then label the team-a namespace to match.