Skip to content

fix(gatewayapi): don't skip MergeBackends precedence-index claims for policies that set nothing - #9590

Merged
zhaohuabing merged 11 commits into
envoyproxy:mainfrom
muwaqar:fix-routingtype-mergetype-9541
Aug 5, 2026
Merged

zhaohuabing merged 11 commits into
envoyproxy:mainfrom
muwaqar:fix-routingtype-mergetype-9541

Conversation

@muwaqar-cflt

@muwaqar-cflt muwaqar-cflt commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

MergeBackends' precedence-resolution indexes for BackendTrafficPolicy (RoutingType, cluster-scoped settings eligibility, LoadBalancer ConsistentHash) and ClientTrafficPolicy (cluster-scoped settings eligibility) all shared the same bug: population logic skipped any accepted policy that set nothing at its target, regardless of mergeType. For a route-rule/route-targeted BTP with mergeType also unset (meaning it doesn't merge with any parent at all), this let the lookup silently fall through to a broader listener/gateway policy's settings instead of resolving to the rule's own (empty) default - or let a skipped policy's target slot be silently claimed by a younger, conflicting policy instead.

This migrates all of BTPRoutingTypeIndex, BTPClusterSettingsIndex, BTPLoadBalancerIndex, and the newly-introduced CTPClusterSettingsIndex onto a shared, minimal policyIndex[T] primitive built on the existing policyScope type (already used by SecurityPolicy's override/merge tracking), rather than patching the old ad hoc raw-map-based indexes directly. Every accepted policy now claims its precedence slot unconditionally (even when it sets nothing), preserving the existing "oldest accepted policy wins over a younger conflicting one" guarantee - dedicated tests cover this at route-rule/route/listener/gateway/ListenerSet level for each index.

policyScope gains a RouteKind tag (a 1-byte enum, to keep the struct under gocritic's hugeParam threshold) so route-rule/route scopes targeting different route kinds under the same name never collide. The BTP indexes also gained ListenerSet-attach support (Gateway/Listener/ListenerSet/ListenerSet-listener levels), extending #9419/#9425's ListenerSet-for-BTP work to this precedence layer.

Out of scope: #9619 (ClusterSettings/LoadBalancer indexes don't yet track ListenerSet-level BTP attachment the way RoutingType now does) is a related but separate gap, left for a follow-up.

Which issue(s) this PR fixes:

Fixes #9541
Fixes #9588


PR Checklist

  • Authorship & ownership: Coding agents / AI assistants are welcome, but I have reviewed every change, understand how and why it works, can explain and maintain it, and take full responsibility for this PR. I have not submitted generated output I do not understand.
  • DCO: All commits are signed off (git commit -s).
  • API agreed first: N/A - no changes under /api.
  • Required checks pass: go build ./..., go vet ./..., gofmt -l, make lint.golint, and go test ./... all pass. make gen-check shows no drift beyond this PR's own changes.
  • Tests added/updated: Unit tests added/updated in backendtrafficpolicy_test.go, clienttrafficpolicy_test.go, and a new policyindex_test.go covering the shared primitive directly (including oldest-accepted-vs-younger-conflicting-policy cases at every level), plus three new golden translator fixtures.
  • Docs: N/A - no user-facing doc change; this restores already-documented precedence semantics.
  • Release notes: Added release-notes/current/bug_fixes/9541-9588-mergebackends-precedence-index-early-exit.md.
  • Generated files committed: N/A - no API/helm/proto changes.
  • Scope & compatibility: Scoped to the four MergeBackends precedence indexes (BTPRoutingTypeIndex, BTPClusterSettingsIndex, BTPLoadBalancerIndex, CTPClusterSettingsIndex); no breaking changes.
  • Codex review: Not yet requested.
  • Copilot review: Not yet requested.

@muwaqar-cflt
muwaqar-cflt requested a review from a team as a code owner July 28, 2026 15:31
@netlify

netlify Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for cerulean-figolla-1f9435 ready!

Name Link
🔨 Latest commit 826b1c8
🔍 Latest deploy log https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/6a7226474fb3fd0008948e29
😎 Deploy Preview https://deploy-preview-9590--cerulean-figolla-1f9435.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@muwaqar-cflt

Copy link
Copy Markdown
Contributor Author

@kkk777-7 can you review this? its ported from muwaqar#4

@codecov

codecov Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.05%. Comparing base (0e77573) to head (826b1c8).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
internal/gatewayapi/policyindex.go 92.85% 2 Missing and 2 partials ⚠️
internal/gatewayapi/helpers.go 88.23% 2 Missing ⚠️
internal/gatewayapi/backendtrafficpolicy.go 97.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9590      +/-   ##
==========================================
+ Coverage   76.02%   76.05%   +0.02%     
==========================================
  Files         259      261       +2     
  Lines       43275    43420     +145     
==========================================
+ Hits        32900    33022     +122     
- Misses       8183     8194      +11     
- Partials     2192     2204      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

kkk777-7
kkk777-7 previously approved these changes Jul 29, 2026
@kkk777-7

Copy link
Copy Markdown
Member

LGTM, thanks!

@kkk777-7

Copy link
Copy Markdown
Member

/retest

Comment thread internal/gatewayapi/policyindex.go Resolved
zhaohuabing
zhaohuabing previously approved these changes Jul 30, 2026

@zhaohuabing zhaohuabing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

…ergeType is unset

Fixes envoyproxy#9541: a route-rule/route-targeted
BackendTrafficPolicy with both RoutingType and MergeType unset
incorrectly inherited RoutingType from its resolved gateway/listener
parent. MergeType nil means the policy doesn't merge with a parent at
all, so it must resolve to its own (nil) value instead.

Migrates BTPRoutingTypeIndex onto a new, generic policyIndex[T]
primitive that reuses the existing policyScope type (previously used
only by SecurityPolicy's override/merge tracking), rather than
patching the old ad hoc btpRoutingKey-based maps directly. Each entry
carries a single effective bool decided at write time - via
isRouteEffective's MergeType-aware rule for route-rule/route scopes,
or the caller-supplied hasValue for listener/gateway scopes - so
Lookup resolves any scope uniformly.

policyScope gains a RouteKind tag (a 1-byte enum, to keep the struct
under gocritic's hugeParam threshold) so route-rule/route scopes
targeting different route kinds under the same name never collide.

Signed-off-by: Muhammad Waqar <[email protected]>
…ngs, LoadBalancer, and ClientTrafficPolicy

Applies the same "every accepted policy must claim its precedence slot,
even one that sets nothing" fix from BTPRoutingTypeIndex to
BTPClusterSettingsIndex, BTPLoadBalancerIndex, and the newly-migrated
CTPClusterSettingsIndex, closing the same class of bug for envoyproxy#9588. All
three indexes are now built on the shared policyIndex[T] primitive
instead of ad-hoc raw maps, and the BTP indexes gained ListenerSet
support.

Signed-off-by: Muhammad Waqar <[email protected]>
@muwaqar-cflt muwaqar-cflt changed the title fix(gatewayapi): don't inherit RoutingType when MergeType is unset fix(gatewayapi): don't skip MergeBackends precedence-index claims for policies that set nothing Jul 30, 2026
@jukie

jukie commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

@codex review

….go and its test

The test file had grown its own copy of route.go's listener-scope-checking
loop just to keep compiling after CTPClusterSettingsIndex became a plain
policyIndex alias, so the test wasn't actually exercising production
code and could silently drift from it. Extract the loop into a package
function both call.

Signed-off-by: Muhammad Waqar <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e4ccd881f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…onally

The LoadBalancer switch only ran under `if hasLoadBalancer`, so an
accepted Gateway-wide BTP that left LoadBalancer unset skipped claiming
its slot entirely - letting a younger conflicting BTP that does set
ConsistentHash win it. Same "must claim slot unconditionally" fix
already applied to RoutingType and ClusterSettings.

Found via automated PR review on envoyproxy#9590.

Signed-off-by: Muhammad Waqar <[email protected]>
@muwaqar-cflt

Copy link
Copy Markdown
Contributor Author

A note on scope: this PR originally targeted BTPRoutingTypeIndex only, so it could be cherry-picked cleanly to older release branches (e.g. 1.8.x). Unfortunately, MergeBackends (#9477) merged to main in the meantime, and it isn't possible to keep this fix scoped to RoutingType alone anymore - a bunch of interrelated code (BTPClusterSettingsIndex, BTPLoadBalancerIndex, CTPClusterSettingsIndex) landed alongside it that shares the exact same bug class, and fixing forward on top of main means addressing all of them together rather than re-introducing the old ad hoc raw-map indexes just to keep the diff narrow.

So the scope here is intentionally wider than the original description, now covering #9541 and #9588. For 1.8.4 (which doesn't have MergeBackends' ClusterSettings/LoadBalancer/CTP indexes at all), a separate, isolated fix scoped to RoutingType only will be needed - this PR won't cherry-pick cleanly there.

…aliasing

Go won't allow new methods on a type alias to an instantiated generic
(the reason these indexes were reduced to plain policyIndex[T]
aliases), but a struct that anonymously embeds *policyIndex[T] is a
distinct named type, so it can have its own methods. This restores
BTPRoutingTypeIndex.LookupBTPRoutingType/LookupGatewayBTRoutingType,
BTPClusterSettingsIndex.HasRouteLevelClusterSettings,
BTPLoadBalancerIndex.IsConsistentHash, and
CTPClusterSettingsIndex.HasListenerLevelClusterSettings - matching
main's PR #4 design - while keeping call sites free of any .index
field indirection: because the embed is anonymous, methods forward via
promotion (idx.Lookup(...), not idx.index.Lookup(...)). Each dedicated
method checks for a nil receiver before touching the embedded field,
so nil-safety doesn't regress.

Signed-off-by: Muhammad Waqar <[email protected]>
…omments

- Port TestCTPClusterSettingsIndex's 2 missing oldest-accepted-blocks-
  younger regression cases (Gateway-listener and ListenerSet-wide
  level), matching the same coverage already ported for BTP indexes.
- Port the missing mergebackends-http-demerge-route-replaces-gateway-
  cluster-settings golden fixture pair, verified byte-identical to
  PR #4's own validated output (aside from the mergeBackends schema
  drift already fixed in the other 3 ported fixtures).
- Strip every test-case comment added by this PR (per review feedback);
  comments already present before this PR are left untouched.
- Merge TestPolicyIndexLookupListenerSet into TestPolicyIndexLookup for
  consistency with TestPolicyIndexLookupExact's single-table style, and
  settle on a consistent useRuleName/useListener/useListenerSet naming
  convention for the three optional Lookup arguments.

Signed-off-by: Muhammad Waqar <[email protected]>
@zhaohuabing
zhaohuabing requested review from a team, kkk777-7 and zhaohuabing July 30, 2026 22:14
@zhaohuabing

Copy link
Copy Markdown
Member

/retest

… via hasRouteLevelClusterSettings

The route/rule-level BackendTrafficPolicy conflict this TODO described
is already excluded from cluster merging by hasRouteLevelClusterSettings's
pinned-slot tracking, so it never reaches x.BackendClusters here.

Signed-off-by: Muhammad Waqar <[email protected]>
@zhaohuabing
zhaohuabing requested a review from a team July 31, 2026 15:00
…dence fix

MergeBackends itself hasn't shipped in a release yet, so a separate
bug-fix note in the same release cycle as its own new-feature note is
redundant and confusing.

Signed-off-by: Muhammad Waqar <[email protected]>
Comment thread internal/gatewayapi/policyindex.go Resolved
Comment thread internal/gatewayapi/policyindex.go Resolved
Comment thread internal/gatewayapi/route.go Resolved
Comment thread internal/gatewayapi/route.go Resolved
…thods for clarity

Fold BTPClusterSettingsIndex.HasRouteLevelClusterSettings and
CTPClusterSettingsIndex.HasListenerLevelClusterSettings's callers into a
single hasClusterSettingsBelowGateway, and rename all three consistently
to describe what they answer (whether cluster-scoped settings are
defined below Gateway scope) rather than the mechanism. Harmonize
policyIndex's internal value-naming (pinned -> replacesParent) and give
resolveEntry's counterpart a matching resolveParentLevels name. Move the
TODO(envoyproxy#9619) ListenerSet-attachment gap note from the read-side doc
comment to the write-side switch-cases in BuildBTPIndexes that actually
lack ListenerSet support.

Signed-off-by: Muhammad Waqar <[email protected]>
…cope

setListenerSetLevel hardcoded effective:true unconditionally, unlike its
Gateway-listener counterpart setGatewayListenerLevel. This blocked a
bare-ListenerSet-targeted policy that leaves a field unset from falling
through to the Gateway's setting, even though resolveParentLevels's
final step (a bare gatewayScope lookup) is not actually skipped for
ListenerSet-attached routes - only the Gateway-listener scope is, since
Gateway listeners and ListenerSet listeners are true siblings. Thread
BTPRoutingTypeIndex's existing hasRoutingType signal through to fix the
one caller (routingTypeIdx) where this was user-visible; CTP's caller is
unaffected since it always intends hasValue true.

Also correct the Lookup/setListenerSetLevel doc comments, which
overstated the skip as covering "gateway" scope generally instead of
just the Gateway-listener level specifically.

Signed-off-by: Muhammad Waqar <[email protected]>
@zhaohuabing

Copy link
Copy Markdown
Member

/retest

@zhaohuabing zhaohuabing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@zhaohuabing
zhaohuabing requested review from a team and kkk777-7 August 5, 2026 01:03
@zhaohuabing

Copy link
Copy Markdown
Member

/retest

@zhaohuabing
zhaohuabing merged commit a0ebf49 into envoyproxy:main Aug 5, 2026
85 of 98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep replacing route policies out of shared clusters BackendTrafficPolicy RoutingType ignores MergeType, always inherits from broader-scoped policies

6 participants