refactor: replace sort package with slices package#14356
Merged
davidjumani merged 3 commits intoJul 13, 2026
Conversation
dongjiang1989
force-pushed
the
change-sort-to-slices
branch
from
July 5, 2026 16:57
d73ecb7 to
f17b18a
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR modernizes sorting across kgateway by replacing uses of Go’s sort package with the slices + cmp APIs and adds a depguard rule to prevent reintroducing sort. It also adjusts a few internal sortable abstractions (routes and staged filters) to work naturally with slices.Sort* comparators.
Changes:
- Refactor call sites from
sort.Strings/sort.Ints/sort.Slice*/sort.Sorttoslices.Sort*andcmp.Compare. - Introduce comparator-style helpers (
SortableRoute.CompareTo,CompareStagedFilters) to supportslices.Sort*Func. - Add
depguardconfiguration to forbid future imports of thesortpackage.
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| test/translator/test.go | Switch deterministic sorting in translator tests to slices.SortFunc + stdlib cmp (aliased). |
| test/translator/status.go | Replace sort.Strings with slices.Sort for stable status key ordering in tests. |
| test/e2e/tests/shards_test.go | Replace string sorting with slices.Sort for shard coverage checks. |
| test/e2e/features/loadtesting/startup_suite.go | Replace snapshot/event sorting with slices.Sort / slices.SortFunc. |
| test/e2e/features/loadtesting/attachedroutes_suite.go | Replace caller key sorting with slices.Sort. |
| pkg/pluginsdk/filters/stages.go | Add comparator-based staged filter sorting helpers and new Sort/SortStable methods. |
| pkg/metrics/cmd/findmetrics/main.go | Replace metrics list sorting with slices.SortFunc + cmp.Compare. |
| pkg/kgateway/translator/routeutils/sortable_route.go | Add CompareTo comparator for SortableRoute to support slices.SortStableFunc. |
| pkg/kgateway/translator/listener/gateway_listener_translator.go | Replace route/vhost sorting with slices.Sort*Func using the new comparator APIs. |
| pkg/kgateway/translator/irtranslator/gateway.go | Replace filter-chain sorting with slices.SortFunc; alias Istio slices to avoid stdlib name collision. |
| pkg/kgateway/translator/irtranslator/fc.go | Replace sort.Sort usage on staged filter lists with the new .Sort() method. |
| pkg/kgateway/translator/irtranslator/endpoint_plugins.go | Replace stable slice sorting with slices.SortStableFunc + cmp.Compare. |
| pkg/kgateway/setup/setup_test.go | Replace various test sort helpers with slices.Sort* + stdlib cmp (aliased) for determinism. |
| pkg/kgateway/proxy_syncer/status.go | Replace sort.Strings with slices.Sort for deterministic error message aggregation. |
| pkg/kgateway/proxy_syncer/perclient.go | Replace string slice sorting with slices.Sort for deterministic reporting. |
| pkg/kgateway/proxy_syncer/perclient_clusters_test.go | Replace sort.Strings with slices.Sort in tests. |
| pkg/kgateway/proxy_syncer/local_cluster.go | Replace endpoint ordering sort with slices.SortFunc + cmp.Compare. |
| pkg/kgateway/proxy_syncer/effective_endpoints.go | Replace sort.Slice with slices.SortFunc for deterministic policy hashing inputs. |
| pkg/kgateway/extensions2/pluginutils/headers.go | Replace header pair sorting with slices.SortFunc + cmp.Compare. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/transformation_plugin.go | Replace key sorting with slices.Sort for deterministic metadata generation. |
| pkg/kgateway/extensions2/plugins/trafficpolicy/jwt.go | Replace requirement sorting with slices.SortFunc + cmp.Compare for idempotency. |
| pkg/kgateway/endpoints/prioritize.go | Replace priority sorting with slices.Sort; alias Istio slices helpers to istioslices. |
| pkg/kgateway/admin/server.go | Replace profile list sorting with slices.SortFunc + cmp.Compare. |
| pkg/kgateway/admin/logging.go | Replace component sorting with slices.Sort. |
| pkg/deployer/values_helpers.go | Replace string sorting with slices.Sort; alias Istio slices helpers to istioslices. |
| .golangci.yaml | Enable/configure depguard to forbid importing sort. |
dongjiang1989
force-pushed
the
change-sort-to-slices
branch
from
July 6, 2026 03:18
fbc173b to
d8455ef
Compare
Use Go 1.21+ slices and cmp packages instead of the sort package for more concise and readable sorting code. This matches the pattern used upstream in agentgateway (kgateway-dev#2367). Changes: - sort.Strings -> slices.Sort - sort.Ints -> slices.Sort - sort.Slice -> slices.SortFunc with cmp.Compare - sort.SliceStable -> slices.SortStableFunc - sort.Sort/sort.Stable on sort.Interface types -> Sort()/CompareTo methods using slices.SortFunc internally - Add CompareStagedFilters generic helper for StagedFilterList - Add depguard linter rule to forbid future use of the sort package For files importing istio.io/istio/pkg/slices, the istio import is aliased to istioslices to avoid conflict with stdlib slices. Signed-off-by: dongjiang <[email protected]>
Address Copilot review feedback: - Sort() now uses slices.SortStableFunc to preserve relative order of equal elements (avoids non-deterministic filter ordering) - Remove references to index-based tiebreaker in comments since CompareStagedFilters no longer uses index for comparison Signed-off-by: dongjiang <[email protected]>
dongjiang1989
force-pushed
the
change-sort-to-slices
branch
from
July 13, 2026 08:42
d8455ef to
eaa1d1d
Compare
davidjumani
approved these changes
Jul 13, 2026
| // Sort sorts the StagedFilterList in place using slices.SortStableFunc to preserve | ||
| // the relative order of equal elements. | ||
| func (s StagedFilterList[WellKnown, FilterType]) Sort() { | ||
| slices.SortStableFunc(s, CompareStagedFilters[WellKnown, FilterType]) |
Contributor
There was a problem hiding this comment.
nit: This can call s.SortStable()
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jul 13, 2026
puertomontt
added a commit
to puertomontt/kgateway
that referenced
this pull request
Jul 13, 2026
…omparator Follow-up to kgateway-dev#14356. After migrating off the sort package, the old sort.Interface trio (Len/Less/Swap) on StagedFilterList and SortableRoutes was left behind, along with an unused SortStable() helper identical to Sort(). - Remove dead SortStable() from StagedFilterList (Sort() already uses slices.SortStableFunc). - Remove the now-unused Len/Less/Swap methods from StagedFilterList and SortableRoutes. Production sorts exclusively via CompareStagedFilters and SortableRoute.CompareTo, so these are vestigial. - Point the routeutils test at CompareTo, the comparator production actually uses, instead of the removed Less. This closes the gap where Less and CompareTo could diverge for equal routes (Less returned true, CompareTo 0) while being tested against different contracts. Signed-off-by: omar <[email protected]>
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.
Description
Replace all usage of Go's
sortpackage with the modernslicesandcmppackages (Go 1.21+) for more concise and readable sorting code.Changes
sort.Strings→slices.Sortsort.Ints→slices.Sortsort.Slice→slices.SortFuncwithcmp.Comparesort.SliceStable→slices.SortStableFuncsort.Sort/sort.Stableonsort.Interfacetypes → newSort()/CompareTo()methods usingslices.SortStableFuncinternallyCompareStagedFiltersgeneric helper forStagedFilterListdepguardlinter rule to forbid future use of thesortpackageFor files importing
istio.io/istio/pkg/slices, the istio import is aliased toistioslicesto avoid conflict with stdlibslices.Change Type
/kind cleanup
Changelog
Additional Notes