Lesson 36 of 70 · 8 min read · Updated
Sharing a Gateway Across Namespaces: ReferenceGrant and GRPCRoute for .NET gRPC
On this page
PedalShare now has four teams behind one Gateway: stations, rides, pricing and fleet. This last lesson of the series covers two things that come up once several teams share an entry point. First, a team's route sends traffic to another team's Service, which the Gateway API allows only with a ReferenceGrant. Second, the fleet team exposes a .NET gRPC service through the same Gateway with a GRPCRoute.
Technical Requirements
- The
gateway-labcluster with NGINX Gateway Fabric 2.8.1, thepublicGateway with itshttpslistener and cert-manager wildcard certificate (HTTPS lesson), and the rides and pricing apps - grpcurl 1.9+ (
brew install grpcurl) scripts/port-forward.shrunning (NGF onlocalhost:8080andlocalhost:8443) andca.crtfrom the HTTPS lesson- Sample repository: Fcakiroglu16/dotnetacademy, branch gateway-api-referencegrant-grpcroute
Core Concepts
Two kinds of cross-namespace trust
The Gateway API uses two different handshakes. Both sides must agree in each case:
| Reference | Who points | Who agrees | How |
|---|---|---|---|
| Route to Gateway | Route parentRefs | Gateway owner | allowedRoutes on the listener |
| Route to a Service in another namespace | Route backendRefs | Service owner | ReferenceGrant in the Service's namespace |
| Gateway to a TLS Secret in another namespace | Listener certificateRefs | Secret owner | ReferenceGrant in the Secret's namespace |
Without that second handshake, anyone who can create a route could send Gateway traffic to any Service in the cluster.
ReferenceGrant
A ReferenceGrant lives in the namespace of the target. It lists who may refer (from: group, kind, namespace) and what they may refer to (to: group, kind and an optional name). It is one-way and narrow: a grant for pricing-api-v1 does not cover pricing-api-v2, and a grant for HTTPRoutes does not cover GRPCRoutes. Delete the grant, and the reference stops working.
ReferenceGrant reached v1 in Gateway API v1.5.
GRPCRoute
gRPC runs over HTTP/2, and its path is always /<package.Service>/<Method>. A GRPCRoute matches on those names instead of paths:
| Match | Support |
|---|---|
Exact with service and method | Core |
Exact with method only | Implementation-specific |
RegularExpression | Implementation-specific |
headers (gRPC metadata) | Core for Exact |
To attach a GRPCRoute to an HTTPS listener, the Gateway negotiates HTTP/2 with clients through ALPN. To the Pods, it speaks cleartext HTTP/2 (h2c). The Service port declares this with appProtocol: kubernetes.io/h2c.
Hands-On: Kubernetes Commands
NGF's listeners accept both kinds of route:
grpcurl needs the lab CA and the hostname that the listener and the certificate cover:
Step-by-Step Example
Step 1: A route to another team's Service
The rides app shows a fare next to each ride. Instead of copying the pricing API, the rides team routes /fares on its own hostname to the pricing team's Service:
k8s/07-referencegrant-grpcroute/01-rides-fare-route.yaml
The Gateway accepted the route, because the rides namespace is allowed on the listener. It refused the backend, because the pricing team has not agreed.
Step 2: The pricing team grants access
k8s/07-referencegrant-grpcroute/02-referencegrant.yaml
The grant names one Service. If the rides team points the route at pricing-api-v2, the status goes back to ResolvedRefs=False RefNotPermitted. If the pricing team deletes the grant, the next request returns 500 again. Access is controlled by the team that owns the backend.
Step 3: Deploy the gRPC service
The fleet service is an ASP.NET Core gRPC app with two RPCs, ReportPing and GetBikeStatus, plus server reflection. Kestrel listens with HTTP/2 only, without TLS, because the Gateway terminates TLS:
src/PedalShare.Telemetry/Program.cs
src/PedalShare.Telemetry/Protos/telemetry.proto
The Service declares the protocol on its port:
k8s/apps/fleet-telemetry.yaml
Step 4: The GRPCRoute
The route attaches to the https listener, whose wildcard certificate already covers fleet.pedalshare.local. Each rule lists exact service and method pairs:
k8s/07-referencegrant-grpcroute/03-grpcroute.yaml
Call it through the Gateway:
gRPC status codes from the service reach the client unchanged:
Step 5: A match that is not portable
The first version of this route matched on the service name only, which looks shorter. NGF 2.8.1 rejects it:
k8s/07-referencegrant-grpcroute/invalid-service-only-match.yaml
The API server accepts this YAML, but only an exact service and method pair is Core support. Other implementations may accept a service-only match, but a route that relies on it is not portable. List every method instead.
Results
| Check | Result |
|---|---|
HTTPRoute in pedalshare-rides to a Service in pedalshare-pricing, no grant | ResolvedRefs=False RefNotPermitted, requests return 500 |
Same route after the ReferenceGrant | ResolvedRefs=True, 200 from pricing-api v1 |
Route pointed at pricing-api-v2 (not in the grant) | RefNotPermitted |
| Grant deleted | Requests return 500 again |
GRPCRoute on the https listener | Accepted=True, grpcurl list, ReportPing and GetBikeStatus work |
gRPC errors (NotFound, InvalidArgument) | Reach the client unchanged |
| Service-only method match on NGF 2.8.1 | Accepted=False UnsupportedValue |
Summary
- Routes attach to a Gateway only if the listener's
allowedRouteslets them, and they reach another namespace's Service only if that namespace has aReferenceGrant. In each case, the owner of the target decides. - A
ReferenceGrantis narrow: one direction, specific kinds, and optionally one named object. Deleting it revokes access right away. GRPCRouteroutes on gRPC service and method names. On an HTTPS listener, the Gateway speaks HTTP/2 with clients and h2c with Pods that declareappProtocol: kubernetes.io/h2c.- Use exact service and method pairs for portable GRPCRoutes, and check
Acceptedafter each apply.
This completes the Gateway API series. The PedalShare edge now has two implementations to choose from (NGINX Gateway Fabric and Traefik), HTTP and HTTPS listeners with automatic certificates, header and weight-based routing, and safe sharing between teams. Remove the lab with scripts/cluster-down.sh.