Sub topic 14
Cross-namespace references with ReferenceGrant
This chapter assumes you can write a Gateway and an HTTPRoute, and that you know how Namespaces, Services, and Secrets relate. It covers how the Gateway API stops one namespace from pointing at another namespace's objects, and how an owner opts in.
The problem
Gateway API objects often live in different namespaces. A Gateway sits in infra. A route sits in shop-team. A Service sits in backend. If any route could name any Service, a developer in one namespace could expose another team's Service through a shared Gateway. The same goes for a Gateway that reads a TLS Secret from another namespace.
The API blocks this by default. A reference to an object in another namespace is ignored unless the namespace that owns the target object says it is allowed.
What ReferenceGrant does
A ReferenceGrant is created in the namespace of the target object. It lists who may refer to it (from) and what they may refer to (to). The owner of the target namespace controls it, so the consent comes from the right party.
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: allow-shop-routes
namespace: backend
spec:
from:
- group: gateway.networking.k8s.io
kind: HTTPRoute
namespace: shop-team
to:
- group: ""
kind: ServiceThis grant lives in backend. It allows HTTPRoutes in shop-team to reference Services in backend. The empty group means the core API group, where Service is defined.
Each from entry needs a group, kind, and namespace. Each to entry needs a group and kind. A to entry may also set name to limit the grant to a single object. Without name, the grant covers every object of that kind in the namespace.
Using the grant from a route
With the grant in place, the route names the Service's namespace in backendRefs:
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:
- backendRefs:
- name: cart-svc
namespace: backend
port: 8080Without the grant, the controller rejects this backend reference.
Where grants are needed
| Reference | From | To | Grant needed |
|---|---|---|---|
| `backendRefs` to a Service in another namespace | HTTPRoute | Service | Yes |
| `certificateRefs` to a Secret in another namespace | Gateway | Secret | Yes |
| `parentRefs` to a Gateway in another namespace | HTTPRoute | Gateway | No. The Gateway's `allowedRoutes` controls this. |
The last row matters. Attaching a route to a Gateway is governed by the Gateway's allowedRoutes, not by ReferenceGrant. A route needs both when it attaches to a Gateway in one namespace and sends traffic to a Service in a third.
Common errors
- Grant in the wrong namespace. It must be in the target's namespace, here
backend, not inshop-team. - Wrong
fromnamespace. Thefromnamespace is where the referring object lives. - Wrong kind or group. A Service uses
group: "". A Secret does too. - Missing grant for a Secret. The HTTPS listener fails to be programmed.
Check the result in the status. A missing grant shows on the route as ResolvedRefs set to False with reason RefNotPermitted:
kubectl describe httproute -n shop-team shopExercise
A Gateway in infra has a listener that uses certificateRefs to a Secret named shop-cert in the certs namespace. Where do you create the ReferenceGrant, and what do its from and to entries contain?
Show answer
Create it in the certs namespace, because the grant belongs to the namespace that owns the target. The from entry is group gateway.networking.k8s.io, kind Gateway, namespace infra. The to entry is group "", kind Secret, and optionally name shop-cert to limit the grant to that one Secret.