flagger icon indicating copy to clipboard operation
flagger copied to clipboard

Support Gateway API GRPCRoute for gRPC canary deployments

Open dgmbx opened this issue 5 months ago • 3 comments

Describe the feature

Flagger's Gateway API integration currently supports only HTTPRoute for traffic shifting during canary deployments. The Gateway API GRPCRoute resource reached GA in Gateway API v1.1 and is the first-class way to manage gRPC traffic with Gateway API.

While gRPC traffic can be routed using HTTPRoute, the Gateway API docs identify specific scenarios where GRPCRoute is the correct choice:

  • Service/method matching: GRPCRoute matches on gRPC service and method names (e.g., service: com.example.UserService, method: Login) directly, rather than mapping them to URI paths (e.g., /com.example.UserService/Login) as HTTPRoute requires.
  • gRPC-aware retry policies: Retries conditioned on gRPC status codes (CANCELLED, RESOURCE_EXHAUSTED, UNAVAILABLE, etc.) are only expressible on GRPCRoute. HTTPRoute retries operate on HTTP status codes only.
  • gRPC-oriented observability: Implementations that support GRPCRoute can expose gRPC-specific metrics; this is explicitly not available when routing gRPC over HTTPRoute.
  • Protocol isolation: The Gateway API guidance for controller implementers is to offer GRPCRoute alongside HTTPRoute so users can access protocol-specific features and to avoid adding gRPC-specific policies to HTTPRoute.

Without GRPCRoute support, Flagger users running gRPC services with a Gateway API-compatible controller (e.g., Envoy Gateway, Istio, Cilium) cannot perform canary analysis with gRPC-native semantics. They are forced to use HTTPRoute (losing the features above).

Proposed solution

Add a GRPCRoute router implementation to Flagger's Gateway API provider, mirroring how HTTPRoute is handled today.

Flagger should manage a GRPCRoute resource with the following capabilities, analogous to the existing HTTPRoute integration:

  1. Traffic splitting via backendRefs[].weight: On canary promotion, Flagger shifts traffic from the primary backend to the canary backend by adjusting weight on the two backendRefs entries within a single rule, identical in mechanism to how HTTPRoute canaries work.

  2. Service/method matching (rules[].matches): GRPCRoute matches on service (e.g., com.example.UserService) and method (e.g., Login) rather than URI path. Flagger should preserve user-defined match rules when reconciling the route, only modifying backendRefs weights.

  3. Header matching (rules[].matches[].headers): Support for gRPC metadata header matching, enabling header-based canary routing (e.g., routing a specific tenant or test header to the canary backend).

  4. Filters (rules[].filters): Passthrough of user-defined filters (e.g., RequestHeaderModifier) without interference during weight reconciliation.

  5. Hostname matching: Respect spec.hostnames for multi-host gateway setups, consistent with the HTTPRoute implementation.

Any alternatives you've considered?

  • Use HTTPRoute for gRPC: HTTPRoute can technically route gRPC-over-HTTP/2 traffic, but loses gRPC-specific match semantics (service/method names), gRPC-aware retry policies, and gRPC-oriented metrics. The Gateway API docs explicitly advise against adding gRPC-specific policies to HTTPRoute.
  • Use App Mesh: App Mesh appears to have GRPCRoute support in Flagger today, but is not a viable alternative for users on Gateway API-compatible controllers.
  • Use Istio's native VirtualService: Works for gRPC via HTTP/2, but bypasses Gateway API entirely and is Istio-specific.

dgmbx avatar Jun 19 '26 10:06 dgmbx

Had a look at what this would touch, since the shape of the change decides whether it's a small addition or a second router.

Flagger's Gateway API router is HTTPRoute-typed throughout. pkg/router/gateway_api.go builds a v1.HTTPRouteSpec with v1.HTTPRouteRules and writes through GatewayapiV1().HTTPRoutes(...). There's no GRPCRoute reference anywhere in the repo today, so this is a new router implementation rather than a branch inside the existing one.

The good news is the core mechanism carries over cleanly. Canary traffic shifting is weighted backendRefs, and GRPCBackendRef has the same weight semantics, so the promote/rollback loop is unchanged. Same two-backends-with-weights pattern, written to a different resource type.

Two things don't carry over, and they're worth deciding before someone starts.

Session affinity. I was wrong in an earlier draft of this comment to say there's no equivalent. GRPCRouteRule carries a first-class SessionPersistence *SessionPersistence field, the same shared type HTTPRouteRule uses, with SessionName documented as something that "may be reflected in the cookie or the header". So affinity is representable and the real question is mapping. Flagger's SessionAffinity is expressed today as CookieName and PrimaryCookieName, with Reconcile ignoring the cookie routes during drift detection, and that would need translating onto SessionPersistence rather than onto raw header matches. Support is implementation-specific per the spec, so what a given gateway actually does with it is worth checking before relying on it.

Timeouts. The router calls makeHTTPRouteTimeouts(canary) and sets Rules[N].Timeouts. GRPCRouteRule has no timeouts field, so that path has nothing to map onto and would have to be skipped rather than translated.

So a reasonable first cut is weighted traffic shifting only, with session affinity and timeouts explicitly unsupported and surfaced as a validation error if someone configures them against a GRPCRoute-backed canary, rather than silently ignored. That keeps the diff small and avoids the case where someone sets sessionAffinity, sees no error, and gets no affinity.

Worth confirming with maintainers whether they'd prefer a separate GatewayAPIGRPCRouter or a shared router with a route-kind switch. Since the spec types are distinct Go types rather than a common interface, a separate implementation with shared helpers for the weight math is probably less awkward, but that's their call.


Edited: I originally claimed GRPCRoute has no session-affinity equivalent. It does, via GRPCRouteRule.SessionPersistence. The timeouts point is unchanged and still holds.

1991santhu avatar Aug 04 '26 05:08 1991santhu

Agree with @1991santhu that a separate GatewayAPIGRPCRouter with shared helpers is the cleaner path. The Go types for GRPCRoute and HTTPRoute don't share a useful interface — GRPCRouteRule, GRPCRouteMatch, GRPCBackendRef are all distinct types even though the weight semantics are identical. Trying to genericise them into a shared router would mean type assertions everywhere, which is worse than two implementations with extracted weight-math helpers.

On what could be shared: the weight calculation logic in GetRoutes/SetRoutes (computing primary vs canary weight, the mirrored flag) and the backendRef construction (name, port, namespace from the canary spec) are router-agnostic. Those could live in a gateway_api_common.go helper without forcing a shared interface. The HTTPRoute-specific bits (timeouts, URI-path matching, cookie-based session affinity) stay in the HTTPRoute router.

For session affinity specifically — GRPCRouteRule.SessionPersistence exists but Flagger's current affinity model is cookie-based with header-match routing rules, which is HTTP-specific. gRPC metadata headers could carry the equivalent, but the mapping isn't 1:1 and is implementation-dependent. I'd skip it in a first cut and return a validation error if sessionAffinity is set on a GRPCRoute-backed canary, same as @1991santhu suggested for timeouts.

One additional thing to decide: how does the canary spec signal that it wants GRPCRoute instead of HTTPRoute? Options:

  1. A new field like spec.routeRef.kind: GRPCRoute (explicit, no ambiguity)
  2. Infer from the existing spec.service.gatewayRefs if the referenced route is a GRPCRoute (implicit, but requires a cluster lookup during reconciliation)

Option 1 seems cleaner since it's declarative and doesn't require an extra API call during init.

okarin1511 avatar Oct 19 '26 14:10 okarin1511

@aryan9600 would you take a PR for this? I'd like to work on it.

Based on the discussion above, this is how I would do it: a separate GatewayAPIGRPCRouter that shares the weight and backendRef helpers with the HTTPRoute router, GRPCRoute types added next to the existing HTTPRoute copy in pkg/apis/gatewayapi/v1, and the route kind set explicitly on the Canary, not inferred from portName or appProtocol, so nothing changes for existing HTTPRoute users.

First cut: weights, header and method matches, header modifier and mirror filters. timeout and sessionAffinity have no GRPCRoute equivalent and would be rejected. Plus an e2e test, podinfo already exposes a grpc port.

For the field, would you prefer something like service.gatewayRouteKind: GRPCRoute, or a new provider string?

weatherhog avatar Oct 29 '26 12:10 weatherhog