Skip to content

Fix non-transitive rule precedence for routes without hostnames#4809

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-sigs:mainfrom
sergeylanzman:fix/catchall-empty-hostname-precedence
Jul 6, 2026
Merged

Fix non-transitive rule precedence for routes without hostnames#4809
kubernetes-prow[bot] merged 1 commit into
kubernetes-sigs:mainfrom
sergeylanzman:fix/catchall-empty-hostname-precedence

Conversation

@sergeylanzman

Copy link
Copy Markdown
Contributor

Issue

Related to #4807 and #4792.

Description

getHostnameListPrecedenceOrder compares two hostname lists positionally, but only up to min(len(a), len(b)):

length := min(len(hostnameListOne), len(hostnameListTwo))
for i := range length {
    precedence := getHostnamePrecedenceOrder(hostnameListOne[i], hostnameListTwo[i])
    if precedence != 0 {
        return precedence
    }
}
return 0

When a route has no hostnames (a catch-all /* HTTPRoute), its list is empty, so min(0, N) = 0, the loop never executes, and the function returns 0 ("equal") instead of treating the empty list as the least specific.

Why this is a problem: a catch-all becomes hostname-equal to every route, so its precedence falls through to path length / creation timestamp. Meanwhile host-specific routes are ordered against each other by hostname specificity (path is never consulted). Mixing these two criteria makes compareHttpRulePrecedence non-transitive — it contains comparison cycles, e.g.:

catch-all  >  console  (equal path -> older creation timestamp wins)
console    >  login    (more specific hostname wins, path ignored)
login      >  catch-all (longer path wins)

sort.Slice requires the less function to be a strict weak ordering; given a non-transitive comparator its output is undefined and depends on the input order (which varies between reconciles). In practice this let the catch-all rule intermittently sort above host-specific rules, so requests that should match host-scoped rules were served by the catch-all.

Fix: after all compared hostnames tie, break by hostname-list length — a shorter (in the extreme, empty) list constrains fewer hostnames and is therefore less specific, so it gets lower precedence. This makes an empty hostname list always lose to any route that has hostnames and restores a strict weak ordering, so the sort result no longer depends on input order.

The same getHostnameListPrecedenceOrder is used by both compareHttpRulePrecedence and compareGrpcRulePrecedence, so the fix covers HTTP and GRPC routes.

Checklist

  • Added tests that cover your change (if possible)
  • Added/modified documentation as required (such as the README.md, or the docs directory)
  • Manually tested
  • Made sure the title of the PR is a good description that can go into the release notes

BONUS POINTS checklist: complete for good vibes and maybe prizes?! 🤯

  • Backfilled missing tests for code in same general area 🎉
  • Refactored something and made the world a better place 🌟

getHostnameListPrecedenceOrder compared hostname lists positionally only up to
min(len(a), len(b)). When a route has no hostnames (a catch-all), min(0, N) = 0,
the loop never runs, and it returned 0 ("equal") instead of treating the empty
list as least specific.

As a result the catch-all was hostname-equal to every route and its precedence
fell through to path length / creation timestamp, while host-specific routes were
ordered against each other by hostname specificity. Mixing these criteria makes
compareHttpRulePrecedence non-transitive; since sort.Slice requires a strict weak
ordering, the resulting ALB rule order was undefined and depended on input order,
letting the catch-all intermittently sort above host-specific rules.

Break the remaining tie by hostname-list length: a shorter (or empty) list is
less specific and gets lower precedence. This restores a strict weak ordering.

Adds catch-all vs host-specific cases to Test_compareHttpRulePrecedence.
@kubernetes-prow kubernetes-prow Bot requested review from wweiwei-li and zac-nixon July 5, 2026 14:51
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @sergeylanzman!

It looks like this is your first PR to kubernetes-sigs/aws-load-balancer-controller 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/aws-load-balancer-controller has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 5, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @sergeylanzman. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 5, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.36%. Comparing base (82c3870) to head (0e2f60b).
⚠️ Report is 30 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4809      +/-   ##
==========================================
+ Coverage   57.08%   57.36%   +0.27%     
==========================================
  Files         396      396              
  Lines       31572    31553      -19     
==========================================
+ Hits        18022    18099      +77     
+ Misses      12492    12393      -99     
- Partials     1058     1061       +3     

☔ 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.

@zac-nixon

Copy link
Copy Markdown
Collaborator

/lgtm
/approved

thanks for the fix!

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 6, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sergeylanzman, shraddhabang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 6, 2026
@kubernetes-prow kubernetes-prow Bot merged commit 35ca23f into kubernetes-sigs:main Jul 6, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants