Skip to content

Commit f7cf40f

Browse files
authored
Merge pull request #5820 from tonistiigi/modernize-fixes
apply x/tools/modernize fixes
2 parents c318805 + b5286f8 commit f7cf40f

File tree

109 files changed

+439
-497
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+439
-497
lines changed

cache/blobs.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"maps"
77
"os"
8+
"slices"
89
"strconv"
910

1011
"github.com/containerd/containerd/v2/core/diff"
@@ -418,10 +419,8 @@ func isTypeWindows(sr *immutableRef) bool {
418419
}
419420
switch sr.kind() {
420421
case Merge:
421-
for _, p := range sr.mergeParents {
422-
if isTypeWindows(p) {
423-
return true
424-
}
422+
if slices.ContainsFunc(sr.mergeParents, isTypeWindows) {
423+
return true
425424
}
426425
case Layer:
427426
return isTypeWindows(sr.layerParent)

cache/contenthash/checksum.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path"
1010
"path/filepath"
11+
"slices"
1112
"strings"
1213
"sync"
1314
"sync/atomic"
@@ -542,7 +543,7 @@ func (cc *cacheContext) includedPaths(ctx context.Context, m *mount, p string, o
542543

543544
if origPrefix != "" {
544545
if keyOk {
545-
iter.SeekLowerBound(append(append([]byte{}, k...), 0))
546+
iter.SeekLowerBound(append(slices.Clone(k), 0))
546547
}
547548

548549
resolvedPrefix = convertKeyToPath(k)
@@ -883,7 +884,7 @@ func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node[*CacheRe
883884
h := sha256.New()
884885
next := append(k, 0)
885886
iter := root.Iterator()
886-
iter.SeekLowerBound(append(append([]byte{}, next...), 0))
887+
iter.SeekLowerBound(append(slices.Clone(next), 0))
887888
subk := next
888889
ok := true
889890
for {
@@ -1246,7 +1247,7 @@ func ensureOriginMetadata(md cache.RefMetadata) cache.RefMetadata {
12461247
}
12471248

12481249
var pool32K = sync.Pool{
1249-
New: func() interface{} {
1250+
New: func() any {
12501251
buf := make([]byte, 32*1024) // 32K
12511252
return &buf
12521253
},

cache/contenthash/filehash.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ func (s *statInfo) IsDir() bool {
112112
return s.Mode().IsDir()
113113
}
114114

115-
func (s *statInfo) Sys() interface{} {
115+
func (s *statInfo) Sys() any {
116116
return s.Stat
117117
}

cache/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ func IsNotFound(err error) bool {
15051505
return errors.Is(err, errNotFound)
15061506
}
15071507

1508-
type RefOption interface{}
1508+
type RefOption any
15091509

15101510
type cachePolicy int
15111511

cache/manager_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ func TestMergeBlobchainID(t *testing.T) {
391391
var mergeInputs []ImmutableRef
392392
var descs []ocispecs.Descriptor
393393
descHandlers := DescHandlers(map[digest.Digest]*DescHandler{})
394-
for i := 0; i < 3; i++ {
394+
for i := range 3 {
395395
contentBuffer := contentutil.NewBuffer()
396396
var curBlob ImmutableRef
397-
for j := 0; j < 3; j++ {
397+
for j := range 3 {
398398
blobBytes, desc, err := mapToBlob(map[string]string{strconv.Itoa(i): strconv.Itoa(j)}, true)
399399
require.NoError(t, err)
400400
cw, err := contentBuffer.Writer(ctx)
@@ -1194,7 +1194,7 @@ func TestLoopLeaseContent(t *testing.T) {
11941194
gotChain := []digest.Digest{orgDesc.Digest}
11951195
cur := orgDesc
11961196
previous := chain[len(chain)-1].Digest
1197-
for i := 0; i < 1000; i++ {
1197+
for range 1000 {
11981198
dgst := cur.Digest
11991199
visited[dgst] = struct{}{}
12001200
info, err := co.cs.Info(ctx, dgst)
@@ -1632,7 +1632,7 @@ func TestGetRemotes(t *testing.T) {
16321632
// make some lazy refs from blobs
16331633
expectedContent := map[digest.Digest]struct{}{}
16341634
var descs []ocispecs.Descriptor
1635-
for i := 0; i < 2; i++ {
1635+
for i := range 2 {
16361636
blobmap := map[string]string{"foo": strconv.Itoa(i)}
16371637
blobBytes, desc, err := mapToBlob(blobmap, true)
16381638
require.NoError(t, err)
@@ -1670,10 +1670,10 @@ func TestGetRemotes(t *testing.T) {
16701670
require.NoError(t, err)
16711671

16721672
refs := []ImmutableRef{lazyRef}
1673-
for i := 0; i < 3; i++ {
1673+
for i := range 3 {
16741674
var newRefs []ImmutableRef
16751675
for j, ir := range refs {
1676-
for k := 0; k < 2; k++ {
1676+
for k := range 2 {
16771677
mutRef, err := cm.New(ctx, ir, nil, descHandlers)
16781678
require.NoError(t, err)
16791679

@@ -1834,7 +1834,7 @@ func TestGetRemotes(t *testing.T) {
18341834
require.Equal(t, 1, len(mainOnly))
18351835
mainRemote := mainOnly[0]
18361836
require.Equal(t, len(mainRemote.Descriptors), len(gotMain.Descriptors))
1837-
for i := 0; i < len(mainRemote.Descriptors); i++ {
1837+
for i := range mainRemote.Descriptors {
18381838
require.Equal(t, mainRemote.Descriptors[i].Digest, gotMain.Descriptors[i].Digest)
18391839
}
18401840

@@ -2053,7 +2053,7 @@ func TestMergeOp(t *testing.T) {
20532053
require.Nil(t, emptyMerge)
20542054

20552055
var baseRefs []ImmutableRef
2056-
for i := 0; i < 6; i++ {
2056+
for i := range 6 {
20572057
active, err := cm.New(ctx, nil, nil)
20582058
require.NoError(t, err)
20592059
m, err := active.Mount(ctx, false, nil)
@@ -2352,7 +2352,7 @@ func TestMountReadOnly(t *testing.T) {
23522352
mutRef, err := cm.New(ctx, nil, nil)
23532353
require.NoError(t, err)
23542354

2355-
for i := 0; i < 2; i++ {
2355+
for range 2 {
23562356
rwMntable, err := mutRef.Mount(ctx, false, nil)
23572357
require.NoError(t, err)
23582358
rwMnts, release, err := rwMntable.Mount()

cache/metadata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func (md *cacheMetadata) updateLastUsed() error {
434434
})
435435
}
436436

437-
func (md *cacheMetadata) queueValue(key string, value interface{}, index string) error {
437+
func (md *cacheMetadata) queueValue(key string, value any, index string) error {
438438
v, err := metadata.NewValue(value)
439439
if err != nil {
440440
return errors.Wrap(err, "failed to create value")
@@ -450,7 +450,7 @@ func (md *cacheMetadata) SetString(key, value string, index string) error {
450450
return md.setValue(key, value, index)
451451
}
452452

453-
func (md *cacheMetadata) setValue(key string, value interface{}, index string) error {
453+
func (md *cacheMetadata) setValue(key string, value any, index string) error {
454454
v, err := metadata.NewValue(value)
455455
if err != nil {
456456
return errors.Wrap(err, "failed to create value")

cache/metadata/metadata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,15 @@ type Value struct {
432432
Index string `json:"index,omitempty"`
433433
}
434434

435-
func NewValue(v interface{}) (*Value, error) {
435+
func NewValue(v any) (*Value, error) {
436436
dt, err := json.Marshal(v)
437437
if err != nil {
438438
return nil, errors.WithStack(err)
439439
}
440440
return &Value{Value: json.RawMessage(dt)}, nil
441441
}
442442

443-
func (v *Value) Unmarshal(target interface{}) error {
443+
func (v *Value) Unmarshal(target any) error {
444444
return errors.WithStack(json.Unmarshal(v.Value, target))
445445
}
446446

cache/refs.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"maps"
77
"os"
88
"path/filepath"
9+
"slices"
910
"strings"
1011
"sync"
1112
"time"
@@ -1714,14 +1715,8 @@ func (sm *sharableMountable) Mount() (_ []mount.Mount, _ func() error, retErr er
17141715
release()
17151716
}
17161717
}()
1717-
var isOverlay bool
1718-
for _, m := range mounts {
1719-
if overlay.IsOverlayMountType(m) {
1720-
isOverlay = true
1721-
break
1722-
}
1723-
}
1724-
if !isOverlay {
1718+
1719+
if !slices.ContainsFunc(mounts, overlay.IsOverlayMountType) {
17251720
// Don't need temporary mount wrapper for non-overlayfs mounts
17261721
return mounts, release, nil
17271722
}

cache/remote.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"maps"
77
"net/url"
8+
"slices"
89
"strings"
910

1011
"github.com/containerd/containerd/v2/core/content"
@@ -199,14 +200,7 @@ func (sr *immutableRef) getRemote(ctx context.Context, createIfNeeded bool, refC
199200
if existings, ok := desc.Annotations[dslKey]; ok {
200201
existingRepos = strings.Split(existings, ",")
201202
}
202-
addNewRepo := true
203-
for _, existing := range existingRepos {
204-
if existing == repo {
205-
addNewRepo = false
206-
break
207-
}
208-
}
209-
if addNewRepo {
203+
if !slices.Contains(existingRepos, repo) {
210204
existingRepos = append(existingRepos, repo)
211205
}
212206
desc.Annotations[dslKey] = strings.Join(existingRepos, ",")

cache/remotecache/gha/gha_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
155155
}
156156

157157
func ensurePruneAll(t *testing.T, c *client.Client, sb integration.Sandbox) {
158-
for i := 0; i < 2; i++ {
158+
for i := range 2 {
159159
require.NoError(t, c.Prune(sb.Context(), nil, client.PruneAll))
160-
for j := 0; j < 20; j++ {
160+
for range 20 {
161161
du, err := c.DiskUsage(sb.Context())
162162
require.NoError(t, err)
163163
if len(du) == 0 {

cache/remotecache/inline/inline.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,7 @@ func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest)
106106
if len(resultBlobs) <= len(layers) {
107107
match = true
108108
for k, resultBlob := range resultBlobs {
109-
matchesBlob := false
110-
for _, layerBlob := range layerBlobDigests[k] {
111-
if layerBlob == resultBlob {
112-
matchesBlob = true
113-
break
114-
}
115-
}
109+
matchesBlob := slices.Contains(layerBlobDigests[k], resultBlob)
116110
if !matchesBlob {
117111
match = false
118112
break

cache/remotecache/s3/s3.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
214214
close(tasks)
215215
}()
216216

217-
for k := 0; k < e.config.UploadParallelism; k++ {
217+
for range e.config.UploadParallelism {
218218
eg.Go(func() error {
219219
for index := range tasks {
220220
blob := cacheConfig.Layers[index].Blob

cache/remotecache/v1/cachestorage.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cacheimport
22

33
import (
44
"context"
5+
"slices"
56
"time"
67

78
"github.com/moby/buildkit/identity"
@@ -194,10 +195,8 @@ func (cs *cacheKeyStorage) HasLink(id string, link solver.CacheInfoLink, target
194195
selector: link.Selector.String(),
195196
}
196197
if it, ok := cs.byID[id]; ok {
197-
for _, id := range it.links[l] {
198-
if id == target {
199-
return true
200-
}
198+
if slices.Contains(it.links[l], target) {
199+
return true
201200
}
202201
}
203202
return false

cache/remotecache/v1/chains.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
)
1616

1717
func NewCacheChains() *CacheChains {
18-
return &CacheChains{visited: map[interface{}]struct{}{}}
18+
return &CacheChains{visited: map[any]struct{}{}}
1919
}
2020

2121
type CacheChains struct {
2222
items []*item
23-
visited map[interface{}]struct{}
23+
visited map[any]struct{}
2424
}
2525

2626
var _ solver.CacheExporterTarget = &CacheChains{}

cache/remotecache/v1/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func sortConfig(cc *CacheConfig) {
113113
}
114114

115115
func outputKey(dgst digest.Digest, idx int) digest.Digest {
116-
return digest.FromBytes([]byte(fmt.Sprintf("%s@%d", dgst, idx)))
116+
return digest.FromBytes(fmt.Appendf(nil, "%s@%d", dgst, idx))
117117
}
118118

119119
type nlink struct {
@@ -232,7 +232,7 @@ func normalizeItem(it *item, state *normalizeState) (*item, error) {
232232
} else {
233233
// keep tmp IDs deterministic
234234
state.next++
235-
id = digest.FromBytes([]byte(fmt.Sprintf("%d", state.next)))
235+
id = digest.FromBytes(fmt.Appendf(nil, "%d", state.next))
236236
state.byKey[id] = it
237237
it.links = make([]map[link]struct{}, len(it.links))
238238
for i := range it.links {

client/build_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestClientGatewayIntegration(t *testing.T) {
6868
testClientGatewayContainerSecurityModeCaps,
6969
testClientGatewayContainerSecurityModeValidation,
7070
), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
71-
integration.WithMatrix("secmode", map[string]interface{}{
71+
integration.WithMatrix("secmode", map[string]any{
7272
"sandbox": securitySandbox,
7373
"insecure": securityInsecure,
7474
}),
@@ -79,7 +79,7 @@ func TestClientGatewayIntegration(t *testing.T) {
7979
testClientGatewayContainerHostNetworkingValidation,
8080
),
8181
integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
82-
integration.WithMatrix("netmode", map[string]interface{}{
82+
integration.WithMatrix("netmode", map[string]any{
8383
"default": defaultNetwork,
8484
"host": hostNetwork,
8585
}),
@@ -1061,7 +1061,7 @@ func newTestPrompt(ctx context.Context, t *testing.T, input io.Writer, output *b
10611061
func (p *testPrompt) String() string { return p.prompt }
10621062

10631063
func (p *testPrompt) SendExit(status int) {
1064-
p.input.Write([]byte(fmt.Sprintf("exit %d\n", status)))
1064+
p.input.Write(fmt.Appendf(nil, "exit %d\n", status))
10651065
}
10661066

10671067
func (p *testPrompt) Send(cmd string) {

0 commit comments

Comments
 (0)