Skip to content

Commit 0ddbc52

Browse files
change sort to slices package (#3370)
Signed-off-by: dongjiang <[email protected]>
1 parent 6e24092 commit 0ddbc52

File tree

13 files changed

+60
-48
lines changed

13 files changed

+60
-48
lines changed

pkg/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"maps"
2323
"net/http"
2424
"slices"
25-
"sort"
2625
"time"
2726

2827
corev1 "k8s.io/api/core/v1"
@@ -583,7 +582,8 @@ func defaultConfig(toDefault, defaultFrom Config) Config {
583582

584583
func namespaceAllSelector(namespaces []string) []fields.Selector {
585584
selectors := make([]fields.Selector, 0, len(namespaces)-1)
586-
sort.Strings(namespaces)
585+
slices.Sort(namespaces)
586+
587587
for _, namespace := range namespaces {
588588
if namespace != metav1.NamespaceAll {
589589
selectors = append(selectors, fields.OneTermNotEqualSelector("metadata.namespace", namespace))

pkg/cache/cache_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"reflect"
24-
"sort"
24+
"slices"
2525
"strconv"
2626
"strings"
2727
"time"
@@ -808,8 +808,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
808808

809809
By("verifying the pointer fields in pod have the same addresses")
810810
Expect(outList1.Items).To(HaveLen(len(outList2.Items)))
811-
sort.SliceStable(outList1.Items, func(i, j int) bool { return outList1.Items[i].Name <= outList1.Items[j].Name })
812-
sort.SliceStable(outList2.Items, func(i, j int) bool { return outList2.Items[i].Name <= outList2.Items[j].Name })
811+
slices.SortStableFunc(outList1.Items, func(i, j corev1.Pod) int { return strings.Compare(i.Name, j.Name) })
812+
slices.SortStableFunc(outList2.Items, func(i, j corev1.Pod) int { return strings.Compare(i.Name, j.Name) })
813813
for i := range outList1.Items {
814814
a := &outList1.Items[i]
815815
b := &outList2.Items[i]
@@ -1134,8 +1134,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
11341134

11351135
By("verifying the pointer fields in pod have the same addresses")
11361136
Expect(outList1.Items).To(HaveLen(len(outList2.Items)))
1137-
sort.SliceStable(outList1.Items, func(i, j int) bool { return outList1.Items[i].GetName() <= outList1.Items[j].GetName() })
1138-
sort.SliceStable(outList2.Items, func(i, j int) bool { return outList2.Items[i].GetName() <= outList2.Items[j].GetName() })
1137+
slices.SortStableFunc(outList1.Items, func(i, j unstructured.Unstructured) int { return strings.Compare(i.GetName(), j.GetName()) })
1138+
slices.SortStableFunc(outList2.Items, func(i, j unstructured.Unstructured) int { return strings.Compare(i.GetName(), j.GetName()) })
11391139
for i := range outList1.Items {
11401140
a := &outList1.Items[i]
11411141
b := &outList2.Items[i]
@@ -1519,8 +1519,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
15191519

15201520
By("verifying the pointer fields in pod have the same addresses")
15211521
Expect(outList1.Items).To(HaveLen(len(outList2.Items)))
1522-
sort.SliceStable(outList1.Items, func(i, j int) bool { return outList1.Items[i].Name <= outList1.Items[j].Name })
1523-
sort.SliceStable(outList2.Items, func(i, j int) bool { return outList2.Items[i].Name <= outList2.Items[j].Name })
1522+
slices.SortStableFunc(outList1.Items, func(i, j metav1.PartialObjectMetadata) int { return strings.Compare(i.Name, j.Name) })
1523+
slices.SortStableFunc(outList2.Items, func(i, j metav1.PartialObjectMetadata) int { return strings.Compare(i.Name, j.Name) })
15241524
for i := range outList1.Items {
15251525
a := &outList1.Items[i]
15261526
b := &outList2.Items[i]

pkg/client/apiutil/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package apiutil
1818

1919
import (
2020
"fmt"
21-
"sort"
21+
"slices"
2222
"strings"
2323

2424
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -38,7 +38,7 @@ func (e *ErrResourceDiscoveryFailed) Error() string {
3838
for k, v := range *e {
3939
subErrors = append(subErrors, fmt.Sprintf("%s: %v", k, v))
4040
}
41-
sort.Strings(subErrors)
41+
slices.Sort(subErrors)
4242
return fmt.Sprintf("unable to retrieve the complete list of server APIs: %s", strings.Join(subErrors, ", "))
4343
}
4444

pkg/healthz/healthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"net/http"
2222
"path"
23-
"sort"
23+
"slices"
2424
"strings"
2525

2626
"k8s.io/apimachinery/pkg/util/sets"
@@ -75,7 +75,7 @@ func (h *Handler) serveAggregated(resp http.ResponseWriter, req *http.Request) {
7575
}
7676

7777
// ...sort to be consistent...
78-
sort.Slice(parts, func(i, j int) bool { return parts[i].name < parts[j].name })
78+
slices.SortStableFunc(parts, func(i, j checkStatus) int { return strings.Compare(i.name, j.name) })
7979

8080
// ...and write out the result
8181
// TODO(directxman12): this should also accept a request for JSON content (via a accept header)

pkg/internal/testing/certs/tinyca_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"encoding/pem"
2222
"math/big"
2323
"net"
24-
"sort"
24+
"slices"
2525
"time"
2626

2727
. "github.com/onsi/ginkgo/v2"
@@ -67,10 +67,11 @@ var _ = Describe("TinyCA", func() {
6767
secondCerts.Cert.SerialNumber,
6868
thirdCerts.Cert.SerialNumber,
6969
}
70-
// quick uniqueness check of numbers: sort, then you only have to compare sequential entries
71-
sort.Slice(serials, func(i, j int) bool {
72-
return serials[i].Cmp(serials[j]) == -1
70+
// quick uniqueness check of numbers: slices sort, then you only have to compare sequential entries
71+
slices.SortStableFunc(serials, func(i, j *big.Int) int {
72+
return i.Cmp(j)
7373
})
74+
7475
Expect(serials[1].Cmp(serials[0])).NotTo(Equal(0), "serials shouldn't be equal")
7576
Expect(serials[2].Cmp(serials[1])).NotTo(Equal(0), "serials shouldn't be equal")
7677
})

pkg/internal/testing/process/arguments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package process
1919
import (
2020
"bytes"
2121
"html/template"
22-
"sort"
22+
"slices"
2323
"strings"
2424
)
2525

@@ -230,7 +230,7 @@ func (a *Arguments) AsStrings(defaults map[string][]string) []string {
230230
for key := range a.values {
231231
keysInOrder = append(keysInOrder, key)
232232
}
233-
sort.Strings(keysInOrder)
233+
slices.Sort(keysInOrder)
234234

235235
var res []string
236236
for _, key := range keysInOrder {

tools/setup-envtest/env/env.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"io"
1111
"io/fs"
1212
"path/filepath"
13-
"sort"
13+
"slices"
1414
"strings"
1515
"text/tabwriter"
1616

@@ -121,9 +121,10 @@ func (e *Env) ListVersions(ctx context.Context) {
121121
if !e.Version.Matches(set.Version) {
122122
continue
123123
}
124-
sort.Slice(set.Platforms, func(i, j int) bool {
125-
return orderPlatforms(set.Platforms[i].Platform, set.Platforms[j].Platform)
124+
slices.SortStableFunc(set.Platforms, func(i, j versions.PlatformItem) int {
125+
return orderPlatforms(i.Platform, j.Platform)
126126
})
127+
127128
for _, plat := range set.Platforms {
128129
if e.Platform.Matches(plat.Platform) {
129130
fmt.Fprintf(out, "(available)\tv%s\t%s\n", set.Version, plat)
@@ -246,7 +247,7 @@ func (e *Env) EnsureVersionIsSet(ctx context.Context) {
246247
serverVer, platform := e.LatestVersion(ctx)
247248

248249
// if we're not forcing a download, and we have a newer local version, just use that
249-
if !e.ForceDownload && localVer != nil && localVer.NewerThan(serverVer) {
250+
if !e.ForceDownload && localVer != nil && localVer.Compare(serverVer) > 0 {
250251
e.Platform.Platform = localPlat // update our data with hash
251252
e.Version.MakeConcrete(*localVer)
252253
return

tools/setup-envtest/env/helpers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ package env
55

66
import (
77
"fmt"
8+
"strings"
89

910
"sigs.k8s.io/controller-runtime/tools/setup-envtest/versions"
1011
)
1112

1213
// orderPlatforms orders platforms by OS then arch.
13-
func orderPlatforms(first, second versions.Platform) bool {
14+
func orderPlatforms(first, second versions.Platform) int {
1415
// sort by OS, then arch
1516
if first.OS != second.OS {
16-
return first.OS < second.OS
17+
return strings.Compare(first.OS, second.OS)
1718
}
18-
return first.Arch < second.Arch
19+
return strings.Compare(first.Arch, second.Arch)
1920
}
2021

2122
// PrintFormat indicates how to print out fetch and switch results.

tools/setup-envtest/remote/http_client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
"sort"
12+
"slices"
1313

1414
"github.com/go-logr/logr"
1515
"sigs.k8s.io/controller-runtime/tools/setup-envtest/versions"
@@ -87,9 +87,8 @@ func (c *HTTPClient) ListVersions(ctx context.Context) ([]versions.Set, error) {
8787
res = append(res, versions.Set{Version: ver, Platforms: details})
8888
}
8989
// sort in inverse order so that the newest one is first
90-
sort.Slice(res, func(i, j int) bool {
91-
first, second := res[i].Version, res[j].Version
92-
return first.NewerThan(second)
90+
slices.SortStableFunc(res, func(i, j versions.Set) int {
91+
return i.Version.Compare(j.Version)
9392
})
9493

9594
return res, nil

tools/setup-envtest/store/store.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
"io"
1313
"os"
1414
"path/filepath"
15-
"sort"
15+
"slices"
16+
"strings"
1617

1718
"github.com/go-logr/logr"
1819
"github.com/spf13/afero"
@@ -102,11 +103,11 @@ func (s *Store) List(ctx context.Context, matching Filter) ([]Item, error) {
102103
return nil, fmt.Errorf("unable to list version-platform pairs in store: %w", err)
103104
}
104105

105-
sort.Slice(res, func(i, j int) bool {
106-
if !res[i].Version.Matches(res[j].Version) {
107-
return res[i].Version.NewerThan(res[j].Version)
106+
slices.SortStableFunc(res, func(i, j Item) int {
107+
if !i.Version.Matches(j.Version) {
108+
return i.Version.Compare(j.Version)
108109
}
109-
return orderPlatforms(res[i].Platform, res[j].Platform)
110+
return orderPlatforms(i.Platform, j.Platform)
110111
})
111112

112113
return res, nil
@@ -296,10 +297,10 @@ func (s *Store) removeItem(itemDir afero.Fs) error {
296297
}
297298

298299
// orderPlatforms orders platforms by OS then arch.
299-
func orderPlatforms(first, second versions.Platform) bool {
300+
func orderPlatforms(first, second versions.Platform) int {
300301
// sort by OS, then arch
301302
if first.OS != second.OS {
302-
return first.OS < second.OS
303+
return strings.Compare(first.OS, second.OS)
303304
}
304-
return first.Arch < second.Arch
305+
return strings.Compare(first.Arch, second.Arch)
305306
}

0 commit comments

Comments
 (0)