Skip to content

refactor: replace sort package with slices package#14356

Merged
davidjumani merged 3 commits into
kgateway-dev:mainfrom
dongjiang1989:change-sort-to-slices
Jul 13, 2026
Merged

refactor: replace sort package with slices package#14356
davidjumani merged 3 commits into
kgateway-dev:mainfrom
dongjiang1989:change-sort-to-slices

Conversation

@dongjiang1989

@dongjiang1989 dongjiang1989 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Description

Replace all usage of Go's sort package with the modern slices and cmp packages (Go 1.21+) for more concise and readable sorting code.

Changes

  • sort.Stringsslices.Sort
  • sort.Intsslices.Sort
  • sort.Sliceslices.SortFunc with cmp.Compare
  • sort.SliceStableslices.SortStableFunc
  • sort.Sort/sort.Stable on sort.Interface types → new Sort()/CompareTo() methods using slices.SortStableFunc internally
  • Added CompareStagedFilters generic helper for StagedFilterList
  • Added 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.

Change Type

/kind cleanup

Changelog

Refactor: replace sort package with slices package for cleaner sorting code.

Additional Notes

  • All unit tests pass
  • Build and go vet clean

Copilot AI review requested due to automatic review settings July 5, 2026 16:55
@gateway-bot gateway-bot added do-not-merge/description-invalid kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note labels Jul 5, 2026
@dongjiang1989
dongjiang1989 force-pushed the change-sort-to-slices branch from d73ecb7 to f17b18a Compare July 5, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.Sort to slices.Sort* and cmp.Compare.
  • Introduce comparator-style helpers (SortableRoute.CompareTo, CompareStagedFilters) to support slices.Sort*Func.
  • Add depguard configuration to forbid future imports of the sort package.

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.

Comment thread pkg/pluginsdk/filters/stages.go Outdated
Comment thread pkg/pluginsdk/filters/stages.go Outdated
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
dongjiang1989 force-pushed the change-sort-to-slices branch from d8455ef to eaa1d1d Compare July 13, 2026 08:42
// 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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: This can call s.SortStable()

@davidjumani
davidjumani added this pull request to the merge queue Jul 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 13, 2026
@davidjumani
davidjumani added this pull request to the merge queue Jul 13, 2026
Merged via the queue into kgateway-dev:main with commit 239ba8d Jul 13, 2026
32 checks passed
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]>
@dongjiang1989
dongjiang1989 deleted the change-sort-to-slices branch July 14, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants