Sub topic 10
Cross-namespace references and ReferenceGrant
This chapter assumes you know Kubernetes Namespaces and Secrets, and that you have read the chapters on Gateways, listeners and route attachment. It explains how a Gateway API resource can safely point at an object in another namespace.
The problem with Ingress
An Ingress could only use a TLS Secret from its own namespace. That rule was implicit, so nothing in the API described it. Shared platforms worked around it by copying certificates into every application namespace.
In the Gateway API, the platform team usually owns the Gateway in its own namespace, for example gateway-system. Application teams own routes in namespaces like shop. Some references now cross namespace lines, so the API needs an explicit rule for when that is allowed.
Two kinds of cross-namespace links
The model separates two cases, and each has its own control.
| Link | Direction | Who consents | Mechanism |
|---|---|---|---|
| Route to Gateway | Route points at a Gateway elsewhere | The Gateway owner | `allowedRoutes` on the listener |
| Gateway or Route to another object | Resource points at a Secret or Service elsewhere | The owner of the target namespace | `ReferenceGrant` in the target namespace |
You already used allowedRoutes in the Gateway example. The listener there sets namespaces.from: All, so routes from any namespace may attach. The second row is the new part.
ReferenceGrant
A ReferenceGrant lives in the namespace of the object being referenced. It lists who may reference (from) and what they may reference (to). The owner of the target namespace creates it, so a team cannot reach into another namespace on its own.
Suppose the certificate Secret is in a certs namespace, but the Gateway is in gateway-system:
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: allow-gateway-to-read-cert
namespace: certs
spec:
from:
- group: gateway.networking.k8s.io
kind: Gateway
namespace: gateway-system
to:
- group: ""
kind: SecretThe Gateway listener then names the Secret with an explicit namespace:
tls:
mode: Terminate
certificateRefs:
- name: app-example-com-tls
namespace: certsWithout the grant, the reference is refused. If the Secret is in the Gateway's own namespace, as in the earlier example, no grant is needed.
A grant can also let a route use a Service in another namespace. In that case from has kind: HTTPRoute and to has kind: Service. You can add name under to to limit the grant to one object. Without it, the grant covers every object of that kind in the namespace.
Common errors
- Grant in the wrong namespace. It must be in the namespace of the target, not the namespace of the referrer.
- Mismatched
fromfields.group,kindandnamespacemust all match the referring resource. - Silent failure. A missing grant does not fail at apply time. The Gateway or route reports
ResolvedRefs: Falsein its status, with reasonRefNotPermitted. Check it withkubectl describe.
Exercise
A HTTPRoute in namespace shop references a Service payments in namespace billing. The route shows ResolvedRefs: False. Which resource do you create, in which namespace, and what do its from and to contain?
Show answer
Create a ReferenceGrant in billing, the namespace of the target. from has group gateway.networking.k8s.io, kind HTTPRoute, namespace shop. to has group "" and kind Service, optionally with name: payments.