Skip to content

fix: By default, httproutes are sorted alphabetically based on names#6433

Closed
fayizk1 wants to merge 5 commits intoenvoyproxy:mainfrom
fayizk1:httproutes_order
Closed

fix: By default, httproutes are sorted alphabetically based on names#6433
fayizk1 wants to merge 5 commits intoenvoyproxy:mainfrom
fayizk1:httproutes_order

Conversation

@fayizk1
Copy link
Copy Markdown
Contributor

@fayizk1 fayizk1 commented Jun 29, 2025

What type of PR is this?

fix: By default, httproutes are sorted alphabetically based on names

What this PR does / why we need it:

This PR ensures consistent route ordering by including route names in the default sorting logic, preventing the inconsistent reordering that may be triggered by unrelated updates (i.e. pod churn, etc.). After all sorting conditions, ties exist across multiple routes, causing this.

Requirements for the GW-API under these circumstances:

  • The Route appearing first in alphabetical order by "{namespace}/{name}"

So sort routes by route names, which is a combination of namespace, route name, route index and domain.

Which issue(s) this PR fixes:

Fixes #6140

Release Notes: No

@fayizk1 fayizk1 marked this pull request as ready for review June 29, 2025 21:37
@fayizk1 fayizk1 requested a review from a team as a code owner June 29, 2025 21:37
@arkodg arkodg requested a review from guydc July 1, 2025 01:41
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 1, 2025

Codecov Report

Attention: Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.

Project coverage is 70.91%. Comparing base (ec94c98) to head (4b10a53).
Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
internal/gatewayapi/sort.go 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6433      +/-   ##
==========================================
- Coverage   70.93%   70.91%   -0.02%     
==========================================
  Files         220      220              
  Lines       37259    37265       +6     
==========================================
- Hits        26429    26426       -3     
- Misses       9287     9292       +5     
- Partials     1543     1547       +4     

☔ View full report in Codecov by Sentry.
📢 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.

fayizk1 and others added 2 commits July 1, 2025 12:20
@fayizk1 fayizk1 requested a review from arkodg July 1, 2025 13:49
@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Jul 3, 2025

by doing this we are losing creationTimestamp order, because that is how the HTTPRoutes are originally processed
and now rely on naming order

the pro is this adds consistent ordering even when timestamp is same

the con is

wdyt @envoyproxy/gateway-maintainers

@fayizk1
Copy link
Copy Markdown
Contributor Author

fayizk1 commented Jul 3, 2025

by doing this we are losing creationTimestamp order, because that is how the HTTPRoutes are originally processed and now rely on naming order

the pro is this adds consistent ordering even when timestamp is same

the con is

* doesnt align with how ties should be broken according to https://gateway-api.sigs.k8s.io/reference/spec/#httprouterule

* users who rely on timestamp order will need to rely on naming order

wdyt @envoyproxy/gateway-maintainers

@arkodg
Another option is, include a sort logic here based on namespace and name of the HTTPRoute if their creation dates are equal. This exactly match specs, but I have not checked if this order break later based on route rules. I can test it today and let you know.

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Jul 7, 2025

by doing this we are losing creationTimestamp order, because that is how the HTTPRoutes are originally processed and now rely on naming order
the pro is this adds consistent ordering even when timestamp is same
the con is

* doesnt align with how ties should be broken according to https://gateway-api.sigs.k8s.io/reference/spec/#httprouterule

* users who rely on timestamp order will need to rely on naming order

wdyt @envoyproxy/gateway-maintainers

@arkodg Another option is, include a sort logic here based on namespace and name of the HTTPRoute if their creation dates are equal. This exactly match specs, but I have not checked if this order break later based on route rules. I can test it today and let you know.

@fayizk1 to do this, we'd also have to save creationTimestamp in the IR, which would increase mem, today the sort happens before translation

// always sort initially by creation time stamp. Later on, additional sorting based on matcher type and

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Jul 9, 2025

hey @fayizk1 rethinking this one, this can be solved in

sort.Slice(httpRoutes, func(i, j int) bool {
by converting it into a Sort Func and creating a two level match within it (timestamp and alphabetical name), it should have the same desired testdata result

@arkodg arkodg added this to the v1.5.0-rc.1 Release milestone Jul 9, 2025
@fayizk1
Copy link
Copy Markdown
Contributor Author

fayizk1 commented Jul 14, 2025

hey @fayizk1 rethinking this one, this can be solved in

sort.Slice(httpRoutes, func(i, j int) bool {
by converting it into a Sort Func and creating a two level match within it (timestamp and alphabetical name), it should have the same desired testdata result

Exactly, this is what I wanted to test, and I had a working version for sometime but did not get time to add a broader test case. We may also need to move to a stable sort in other sort logic to avoid order change when equal case. I will open another PR as soon as possible.

(Just realized that I put wrong long in previous comment :) )

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Jul 17, 2025

closing in favor of #6550

@arkodg arkodg closed this Jul 17, 2025
@fayizk1 fayizk1 deleted the httproutes_order branch July 26, 2025 14:54
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.

CPU spikes result from inconsistent route order after each pod restarts

3 participants