Fix non-transitive rule precedence for routes without hostnames#4809
Conversation
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.
|
Welcome @sergeylanzman! |
|
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 Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
/lgtm thanks for the fix! |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Issue
Related to #4807 and #4792.
Description
getHostnameListPrecedenceOrdercompares two hostname lists positionally, but only up tomin(len(a), len(b)):When a route has no hostnames (a catch-all
/*HTTPRoute), its list is empty, somin(0, N) = 0, the loop never executes, and the function returns0("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
compareHttpRulePrecedencenon-transitive — it contains comparison cycles, e.g.:sort.Slicerequires thelessfunction 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
getHostnameListPrecedenceOrderis used by bothcompareHttpRulePrecedenceandcompareGrpcRulePrecedence, so the fix covers HTTP and GRPC routes.Checklist
README.md, or thedocsdirectory)BONUS POINTS checklist: complete for good vibes and maybe prizes?! 🤯