fix: honor BackendTrafficPolicy targetRefs.sectionName for Service ports#2796
Merged
AlinsRan merged 4 commits intoJun 23, 2026
Merged
Conversation
AttachBackendTrafficPolicyToUpstream selected the policy to apply by matching only targetRef.Name, ignoring sectionName (and group/kind). As a result a BackendTrafficPolicy intended for a single named Service port was applied to the whole Service, and a policy targeting a different resource kind with a colliding name could attach to a Service backend. For a Service target the Gateway API interprets sectionName as the port name. Attachment now: - matches targetRef group/kind against the backend ref (Gateway API defaults: empty group, Service kind) before matching by name; - when sectionName is set, attaches only to the backend whose resolved Service port name matches it, and a port-specific targetRef takes precedence over a whole-Service one; - per Gateway API semantics, a sectionName that cannot be resolved does not attach. Add unit tests for the matching logic and an e2e test asserting a sectionName-scoped policy attaches to exactly the matched per-port upstream in both directions.
Use a single HTTPRoute with two rules to the same Service on different ports (/get->80, /headers->8080), plus a sectionName-scoped policy (http-v2) and a whole-Service policy. Assert the http-v2 port uses the scoped policy (precedence over the whole-Service one) while port 80 falls back to the whole-Service policy.
The conflict key only included the target Service (namespace/name/kind), so two BackendTrafficPolicies targeting the same Service were always treated as conflicting even when scoped to different ports via sectionName. Include sectionName in the key so a port-scoped policy and a whole-Service policy (or two different-section policies) can both be accepted.
nic-6443
approved these changes
Jun 22, 2026
shreemaan-abhishek
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
AttachBackendTrafficPolicyToUpstreamselected theBackendTrafficPolicyto apply by matching onlytargetRef.Name, ignoringsectionName(andgroup/kind). As a result:ServiceImport) could attach to aServicebackend.For a
Servicetarget, the Gateway API interpretssectionNameas the port name. This PR makes both attachment and conflict detection honor it:targetRefwithoutsectionNamestill applies to the whole Service (unchanged);targetRefwithsectionNameattaches only to the backend whose resolved Service port name matches it; a port-specifictargetReftakes precedence over a whole-Service one;targetRefgroup/kindare matched against the backend ref (Gateway API defaults: empty group,Servicekind) before matching by name;sectionNamethat cannot be resolved does not attach;ProcessBackendTrafficPolicy) now scopes the conflict key bysectionName, so a port-scoped policy and a whole-Service policy (or two different-section policies) on the same Service can both be Accepted instead of one being markedConflicted.To resolve a backend's port name, the attachment function now also receives the translate context's
Servicesand maps the backend ref port number to the Service port name. All call sites (httproute / grpcroute / ingress / tcproute / tlsroute / udproute) are updated.Example / Reproduction
A Service that exposes two named ports backed by the same workload:
One HTTPRoute whose two rules send
/getto port 80 and/headersto port 8080 (same Service, different ports):Two policies — one scoped to the
http-v2port viasectionName, one targeting the whole Service:Expected result — both policies are accepted, and each port gets its own host:
Before this fix: the
per-portpolicy leaked to port 80 as well (name-only match ignoredsectionName), and thewholepolicy was rejected withstatus.conditions[Accepted]=False, reason=Conflicted("Unable to target Service ... it conflicts with another BackendTrafficPolicy"), so port 80 received the wrong upstream host.Tests
test/e2e/crds/v1alpha1/backendtrafficpolicy.go,Section Name): the example above — asserts thehttp-v2port uses the scoped policy while port 80 falls back to the whole-Service policy.go build,go vet, and the translator unit tests pass.