Sub topic 7
Attaching a Route to a Gateway with parentRefs
This chapter assumes you can apply YAML with kubectl and know what a Namespace and a Service are. It also assumes you have read the earlier chapters on GatewayClass, Gateway, and listeners.
What parentRefs does
A route does nothing on its own. An HTTPRoute becomes active only when it names a parent, which is the Gateway (or a listener on that Gateway) it wants to attach to. You declare the parent in the spec.parentRefs list of the route.
The route owner writes the reference. The Gateway owner then decides whether to accept it, using the allowedRoutes field on the listener. Both sides must agree before traffic flows.
A minimal reference
This HTTPRoute attaches to the Gateway web-gateway in the infra namespace.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop
namespace: shop-team
spec:
parentRefs:
- name: web-gateway
namespace: infra
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /cart
backendRefs:
- name: cart-svc
port: 8080The name field is required. The namespace field defaults to the route's own namespace. Here the Gateway lives in infra, so the route must set it explicitly. If you leave it out, the controller looks for web-gateway in shop-team and finds nothing.
Targeting one listener
A Gateway can have several listeners. By default a route attaches to every listener it is allowed to use. To pick one, add sectionName with the listener's name.
parentRefs:
- name: web-gateway
namespace: infra
sectionName: httpThe value http must match the name of a listener on the Gateway. If no listener has that name, the route does not attach. You can also set port to select a listener by port number.
How attachment is decided
The route is bound to a listener only if all of these checks pass:
- The parent reference points to a Gateway that exists.
- If
sectionNameorportis set, a listener matches it. - The listener's
allowedRoutespermits the route's namespace and kind. - The route's
hostnamesoverlap with the listener'shostname, if the listener sets one.
Attaching to more than one parent
parentRefs is a list. One route can attach to several Gateways, for example one for public traffic and one for internal traffic.
parentRefs:
- name: public-gateway
namespace: infra
- name: internal-gateway
namespace: infraThe controller reports the result for each parent separately in the route's status. One parent can accept the route while the other rejects it.
Common errors
- Missing namespace. The route is in a different namespace from the Gateway, but
namespaceis not set in the parent reference. - Wrong sectionName. The name does not match any listener, so the route attaches to nothing.
- Blocked by allowedRoutes. The listener only accepts routes from namespaces with a certain label, and the route's namespace does not have it.
- Hostname mismatch. The listener says
hostname: shop.example.combut the route listsapi.example.com.
Exercise
A Gateway named edge in namespace infra has listeners named http and https. You want an HTTPRoute in namespace apps to attach only to the https listener. Which fields do you put in parentRefs?
Show answer
Set name: edge, namespace: infra, and sectionName: https. The namespace is required because the Gateway is not in the route's namespace. The listener's allowedRoutes must also permit the apps namespace, or the route will not bind.