feat: add TrafficPolicy requestMirror policy #14441
The e2e assertion can produce false positives because it checks for the expected path and expected authority as two independent substrings; those could be present in different access-log entries. It also only reads logs from pods[0], which can miss the mirrored request if multiple proxy pods exist. Consider aggregating logs from all matching pods and asserting on a single log record substring that contains both the expected authority and the expected path.
Pull request overview
This PR extends the TrafficPolicy API to expose Envoy’s disable_shadow_host_suffix_append behavior for request mirroring, allowing users to preserve the original Host/:authority header (no -shadow suffix) when mirroring traffic. It fits into kgateway’s policy->IR->xDS pipeline by adding a small TrafficPolicy sub-IR that mutates existing Envoy requestMirrorPolicies generated from HTTPRoute/GRPCRoute RequestMirror filters.
Changes:
spec.requestMirror.disableShadowHostSuffixAppend to the TrafficPolicy CRD/API types (with schema validation) and generated deepcopy.:authority with/without the -shadow suffix.Reviewed changes
Copilot reviewed 17 out of 18 changed files in this pull request and generated 1 comment.
<details>
<summary>Show a summary per file</summary>
| File | Description |
| ---- | ----------- |
| test/e2e/tests/kgateway/suite_runner.go | Registers the new RequestMirror e2e suite. |
| test/e2e/tests/apivalidation/apivalidation.go | Adds CRD validation coverage for rejecting empty requestMirror: {}. |
| test/e2e/features/requestmirror/types.go | Defines suite setup manifest wiring for the new request mirror feature test. |
| test/e2e/features/requestmirror/testdata/setup.yaml | Adds gateway/routes/policies to exercise mirrored :authority behavior. |
| test/e2e/features/requestmirror/suite.go | Implements the RequestMirror e2e assertions for -shadow suffix behavior. |
| pkg/kgateway/translator/gateway/testutils/outputs/traffic-policy/request-mirror.yaml | Adds/updates golden output verifying Envoy disableShadowHostSuffixAppend rendering. |
| pkg/kgateway/translator/gateway/testutils/inputs/traffic-policy/request-mirror.yaml | Adds translator input coverage for gateway/route TrafficPolicy request mirror behavior. |
| pkg/kgateway/translator/gateway/gateway_translator_test.go | Runs the new translator golden test case. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/traffic_policy_plugin.go | Applies request-mirror settings per-route and tracks precedence across attachment levels. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/request_mirror.go | Introduces request-mirror sub-IR, construction, equality, and Envoy mutation logic. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/request_mirror_test.go | Unit tests for IR construction, equality, and application behavior. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/merge.go | Adds merge support for request-mirror sub-IR. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/merge_test.go | Adds merge tests for request-mirror precedence/behavior. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/constructor.go | Wires request-mirror IR construction into TrafficPolicy IR building. |
| install/helm/kgateway-crds/templates/gateway.kgateway.dev_trafficpolicies.yaml | Updates CRD schema docs/validation for the new requestMirror block. |
| api/v1alpha1/kgateway/zz_generated.deepcopy.go | Adds deepcopy support for the new API types/fields. |
| api/v1alpha1/kgateway/traffic_policy_types.go | Adds RequestMirror to TrafficPolicySpec and defines RequestMirrorPolicy. |
| .github/workflows/e2e.yaml | Includes the RequestMirror suite in CI execution regex. |
</details>
<details>
<summary>Files not reviewed (1)</summary>
</details>
Addressed in third commit
nit: mention that it's also when unset (default)
nit: A bit unique pattern to have pointers set here. A one-line comment noting it's scoped to a single translation pass (pointers unique per pass, so no cross-route/cross-gateway collision) would help future readers.
LG overall but this PR is about GRPCRoute support but every translator/golden case is HTTPRoute. Worth adding one GRPCRoute rule to request-mirror.yaml (i think) to actually exercise this.
Yeah, that's worth calling out - will add a comment to that effect. Also working on adding the GRPCRoute golden case that you asked for
This struct is needed if this section of the policy requires an empty filter in the chain to overwrite at the route level. If this does not rely on the filter chain, can it be added only to the trafficPolicySpecIr like AutoHostRewrite ?
Should this apply only to roues or gateways as well ?
What is the purpose of this test?
Good callout. The idea when I wrote it was to validate that MinProperties=1 is enforced, but looking around our codebase more, it looks like we don't really test the basic functionality of kubebuilder like that, and it makes less sense to do so on principle the more I think about it. I will just remove this one.
I will take a bit to look further into this, because on an initial look, I'm not sure if I'd even have to change anything at code-level to make this toggle applicable to Gateways as well. I think you're right that it should be applicable to Gateways too and that, whether I'm changing the implementation or not, there needs to be verbiage in some combo of code, comments, and tests (plus the PR description) clarifying that use case is supported. Will get back to this after looking more deeply into it
I was looking at the implementations for Timeout and Retry too when I started this, because they also use the pattern you cited from AutoHostRewrite. This is going to take a long look, because this goes pretty deep in the weeds of Envoy.
The field we're using for this toggle is just an unwrapped bool, see here, unlike the other fields we're using from this file, like AutoHostRewrite (see here), which is a more idiomatic wrapped bool using google.protobuf.BoolValue. And my understanding is that the difference in kgateway between those is that, with an unwrapped bool, there is no nil value where someone didn't declare a value for the field, it's just false, whereas with a wrapped bool the fact it was nil is preserved.
Where I need to see if this becomes a problem is if, as addressed in your earlier comment and my reply, we're supporting this being configurable at Gateway level. I want to see how it would behave if we have an explicit, non-nil false at Route level with a true at the parent Gateway level. I think there's a chance that the treatment of that could be wonky, because we'd want it to preserve an explicit false but not to preserve a nil false. Let me validate rather than guessing, though.
Working on pushing up a next revision. I have a golden test case (actually already did, but am making it clearer) that will assert that this works at Gateway level, and am making the verbiage clearer around the use case. Will also update the PR description after I push that revision up
So I did some manual validation both with the current code, and with it implemented in a way that mimicked the approach used in AutoHostRewrite. That approach worked in every case except one - the case with a Route-level false and a Gateway-level true, where the false at Route level indeed got overwritten. I could add details of this validation if you like, I put all of the commands and output into my Notes app, but wasn't sure if that were necessary. So I believe we have to keep the current approach in the next revision.
I'll leave this comment thread unresolved just in case reviewers want evidence of that validation.
Thanks, updated this portion
Is this the same case with retries and timeouts as well? How are they handled if defined at the gateway level ?
Because those fields Retry and Timeout are nilable (this is determined at Envoy level, we can't control it), a nil value is treated as distinct from an explicit false, and only a nil value at Route level will be overwritten, not an explicit false.
We have an existing golden test case for Timeouts that should assert this: https://github.com/kgateway-dev/kgateway/blob/main/pkg/kgateway/translator/gateway/testutils/inputs/traffic-policy/timeout-gateway.yaml#L67.
A quote from formal docs I consulted while digging into how to answer your prior comment that may help - like I said this is pretty deep in the weeds:
Note that for implicit-presence scalar fields, once a message is parsed there’s no way of telling whether that field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types. For example, don’t have a boolean that switches on some behavior when set to false if you don’t want that behavior to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire. If a float or double value is set to +0 it will not be serialized, but -0 is considered distinct and will be serialized.
This would apply to the type of field used for DisableShadowHostSuffixAppend in Envoy. Source link.
I didn't find quite a good single quote from this doc for the handling of wrapped bools, but the salient point from this doc page is that field absence is handled in a distinct way from just a false bool value.
LGTM.
All review comments addressed, thanks.
So an update on this - I tried a style that I hope is a bit cleaner, where it is now keying by out.GetName() and map[string]struct{}. David and I had discussed whether we could key by filter chain name - we cannot, as mirror precedence is per-route, and a chain can contain many routes. Because we are only passing a pointer to the route, by my understanding of how Go handles this, there should not be a memory issue involved in passing it, as we aren't parsing the whole route. But hopefully things look a bit cleaner now, in the updated code.
so, I assume listener policies get run before gateway? I don't see a test applying the new policy at listener level to override gateway level setting or route level overriding listener level setting. Can this be applied at vhost level as well? If yes, https listener should also be tested.
The complication is similar to the retry policy https://github.com/kgateway-dev/kgateway/pull/14360, maybe compare that PR to this and see if we need anything similar to that PR.
Sorry I am a bit vague. I remember it's complicated but not all the details.
Requested changes.
Thanks - let me read over that PR and think about this before giving a full answer.
Yes, listener policies do get run before gateway policies. But you're completely correct that the listener level wasn't covered before, and I'm fixing that with a commit I am pushing up shortly.
I did compare against #14360 and concluded that this PR, as it stood, was already using the generalized per-route fanout for retries introduced in that PR. Gateway policies flow through ApplyRouteConfigPlugin, listener policies flow through ApplyVhostPlugin, and both of them call handlePerRoutePolicies for each route covered. So I don't think any bigger structural change is needed for this PR on that account.
I am adding a listener-scoped test case and a new HTTPS golden test case, because that HTTPS listener coverage was also a good callout. Thanks again
The changes have now been pushed up, resolving for now
Marking as resolved for now, as there's been no further discussion on this with the subsequent reviewers and they seem fine with the new direction, but feel free to unresolve if this is still up for review when you next look and it remains a sticking point.
Approved these changes.
Approved these changes.
/merge
/merge
ymesika
JCigan
Description
Implements options to control the
Host/:authorityheader of mirrored requests to upstream shadow hosts. Gateway API, which this gateway relies on, can't properly express Envoy'sdisable_shadow_host_suffix_appendorhost_rewrite_literalmirror options. This PR adds arequestMirrorfield toTrafficPolicyexposing those Envoy fields.disableShadowHostSuffixAppenddisables the automatically appended-shadowsuffix on that header value, andhostRewriteLiteralreplaces the mirrored header value with a literal value.Added unit and translator tests. In the future, it would be good to add a data plane e2e test case asserting that a mirrored request's
Hostis correctly configured without-shadowappended when the options added in this PR are enabled. Attempted to add such a test for this PR, but encountered difficulties around asserting whatHost/:authorityheader the mirror received.Change Type
/kind feature
Changelog
``
release-notespec.requestMirrorAdded
toTrafficPolicy. This field allows users to control theHost/:authorityheader of mirrored requests.disableShadowHostSuffixAppenddisables appending-shadowto the end of theHost/:authorityheader value, andhostRewriteLiteralreplaces the mirroredHost/:authorityheader value with a literal value.
`Additional Notes
The implementation augments existing mirrors rather than defining new ones, so the filter still owns the destination, percentage, and existence of the mirror. Here, TrafficPolicy
only tweaks some Envoy-specific behavior for mirrors already on the route, which avoids needing a second way to declare mirrors for this feature.When attached at multiple scopes, the whole requestMirror
block from the most-specific policy wins. SoRoute>Listener>Gatewayis the order of precedence.Applies to mirrors from both HTTPRoute
andGRPCRoute` filters.