Skip to content

Commit

Permalink
apply x/tools/modernize fixes
Browse files Browse the repository at this point in the history
Autogenerated with couple of manual patches.

Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Mar 7, 2025
1 parent e62f1ed commit 8a8d4a2
Show file tree
Hide file tree
Showing 109 changed files with 438 additions and 492 deletions.
7 changes: 3 additions & 4 deletions cache/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"maps"
"os"
"slices"
"strconv"

"github.com/containerd/containerd/v2/core/diff"
Expand Down Expand Up @@ -418,10 +419,8 @@ func isTypeWindows(sr *immutableRef) bool {
}
switch sr.kind() {
case Merge:
for _, p := range sr.mergeParents {
if isTypeWindows(p) {
return true
}
if slices.ContainsFunc(sr.mergeParents, isTypeWindows) {
return true
}
case Layer:
return isTypeWindows(sr.layerParent)
Expand Down
7 changes: 4 additions & 3 deletions cache/contenthash/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -542,7 +543,7 @@ func (cc *cacheContext) includedPaths(ctx context.Context, m *mount, p string, o

if origPrefix != "" {
if keyOk {
iter.SeekLowerBound(append(append([]byte{}, k...), 0))
iter.SeekLowerBound(append(slices.Clone(k), 0))
}

resolvedPrefix = convertKeyToPath(k)
Expand Down Expand Up @@ -883,7 +884,7 @@ func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node[*CacheRe
h := sha256.New()
next := append(k, 0)
iter := root.Iterator()
iter.SeekLowerBound(append(append([]byte{}, next...), 0))
iter.SeekLowerBound(append(slices.Clone(next), 0))
subk := next
ok := true
for {
Expand Down Expand Up @@ -1246,7 +1247,7 @@ func ensureOriginMetadata(md cache.RefMetadata) cache.RefMetadata {
}

var pool32K = sync.Pool{
New: func() interface{} {
New: func() any {
buf := make([]byte, 32*1024) // 32K
return &buf
},
Expand Down
2 changes: 1 addition & 1 deletion cache/contenthash/filehash.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ func (s *statInfo) IsDir() bool {
return s.Mode().IsDir()
}

func (s *statInfo) Sys() interface{} {
func (s *statInfo) Sys() any {
return s.Stat
}
2 changes: 1 addition & 1 deletion cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ func IsNotFound(err error) bool {
return errors.Is(err, errNotFound)
}

type RefOption interface{}
type RefOption any

type cachePolicy int

Expand Down
18 changes: 9 additions & 9 deletions cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ func TestMergeBlobchainID(t *testing.T) {
var mergeInputs []ImmutableRef
var descs []ocispecs.Descriptor
descHandlers := DescHandlers(map[digest.Digest]*DescHandler{})
for i := 0; i < 3; i++ {
for i := range 3 {
contentBuffer := contentutil.NewBuffer()
var curBlob ImmutableRef
for j := 0; j < 3; j++ {
for j := range 3 {
blobBytes, desc, err := mapToBlob(map[string]string{strconv.Itoa(i): strconv.Itoa(j)}, true)
require.NoError(t, err)
cw, err := contentBuffer.Writer(ctx)
Expand Down Expand Up @@ -1194,7 +1194,7 @@ func TestLoopLeaseContent(t *testing.T) {
gotChain := []digest.Digest{orgDesc.Digest}
cur := orgDesc
previous := chain[len(chain)-1].Digest
for i := 0; i < 1000; i++ {
for range 1000 {
dgst := cur.Digest
visited[dgst] = struct{}{}
info, err := co.cs.Info(ctx, dgst)
Expand Down Expand Up @@ -1632,7 +1632,7 @@ func TestGetRemotes(t *testing.T) {
// make some lazy refs from blobs
expectedContent := map[digest.Digest]struct{}{}
var descs []ocispecs.Descriptor
for i := 0; i < 2; i++ {
for i := range 2 {
blobmap := map[string]string{"foo": strconv.Itoa(i)}
blobBytes, desc, err := mapToBlob(blobmap, true)
require.NoError(t, err)
Expand Down Expand Up @@ -1670,10 +1670,10 @@ func TestGetRemotes(t *testing.T) {
require.NoError(t, err)

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

Expand Down Expand Up @@ -1834,7 +1834,7 @@ func TestGetRemotes(t *testing.T) {
require.Equal(t, 1, len(mainOnly))
mainRemote := mainOnly[0]
require.Equal(t, len(mainRemote.Descriptors), len(gotMain.Descriptors))
for i := 0; i < len(mainRemote.Descriptors); i++ {
for i := range mainRemote.Descriptors {
require.Equal(t, mainRemote.Descriptors[i].Digest, gotMain.Descriptors[i].Digest)
}

Expand Down Expand Up @@ -2053,7 +2053,7 @@ func TestMergeOp(t *testing.T) {
require.Nil(t, emptyMerge)

var baseRefs []ImmutableRef
for i := 0; i < 6; i++ {
for i := range 6 {
active, err := cm.New(ctx, nil, nil)
require.NoError(t, err)
m, err := active.Mount(ctx, false, nil)
Expand Down Expand Up @@ -2352,7 +2352,7 @@ func TestMountReadOnly(t *testing.T) {
mutRef, err := cm.New(ctx, nil, nil)
require.NoError(t, err)

for i := 0; i < 2; i++ {
for range 2 {
rwMntable, err := mutRef.Mount(ctx, false, nil)
require.NoError(t, err)
rwMnts, release, err := rwMntable.Mount()
Expand Down
4 changes: 2 additions & 2 deletions cache/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (md *cacheMetadata) updateLastUsed() error {
})
}

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

func (md *cacheMetadata) setValue(key string, value interface{}, index string) error {
func (md *cacheMetadata) setValue(key string, value any, index string) error {
v, err := metadata.NewValue(value)
if err != nil {
return errors.Wrap(err, "failed to create value")
Expand Down
4 changes: 2 additions & 2 deletions cache/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,15 @@ type Value struct {
Index string `json:"index,omitempty"`
}

func NewValue(v interface{}) (*Value, error) {
func NewValue(v any) (*Value, error) {
dt, err := json.Marshal(v)
if err != nil {
return nil, errors.WithStack(err)
}
return &Value{Value: json.RawMessage(dt)}, nil
}

func (v *Value) Unmarshal(target interface{}) error {
func (v *Value) Unmarshal(target any) error {
return errors.WithStack(json.Unmarshal(v.Value, target))
}

Expand Down
8 changes: 3 additions & 5 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"maps"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -1715,11 +1716,8 @@ func (sm *sharableMountable) Mount() (_ []mount.Mount, _ func() error, retErr er
}
}()
var isOverlay bool
for _, m := range mounts {
if overlay.IsOverlayMountType(m) {
isOverlay = true
break
}
if slices.ContainsFunc(mounts, overlay.IsOverlayMountType) {
isOverlay = true
}
if !isOverlay {
// Don't need temporary mount wrapper for non-overlayfs mounts
Expand Down
8 changes: 3 additions & 5 deletions cache/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"maps"
"net/url"
"slices"
"strings"

"github.com/containerd/containerd/v2/core/content"
Expand Down Expand Up @@ -200,11 +201,8 @@ func (sr *immutableRef) getRemote(ctx context.Context, createIfNeeded bool, refC
existingRepos = strings.Split(existings, ",")
}
addNewRepo := true
for _, existing := range existingRepos {
if existing == repo {
addNewRepo = false
break
}
if slices.Contains(existingRepos, repo) {
addNewRepo = false
}
if addNewRepo {
existingRepos = append(existingRepos, repo)
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/gha/gha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
}

func ensurePruneAll(t *testing.T, c *client.Client, sb integration.Sandbox) {
for i := 0; i < 2; i++ {
for i := range 2 {
require.NoError(t, c.Prune(sb.Context(), nil, client.PruneAll))
for j := 0; j < 20; j++ {
for range 20 {
du, err := c.DiskUsage(sb.Context())
require.NoError(t, err)
if len(du) == 0 {
Expand Down
8 changes: 1 addition & 7 deletions cache/remotecache/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,7 @@ func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest)
if len(resultBlobs) <= len(layers) {
match = true
for k, resultBlob := range resultBlobs {
matchesBlob := false
for _, layerBlob := range layerBlobDigests[k] {
if layerBlob == resultBlob {
matchesBlob = true
break
}
}
matchesBlob := slices.Contains(layerBlobDigests[k], resultBlob)
if !matchesBlob {
match = false
break
Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
close(tasks)
}()

for k := 0; k < e.config.UploadParallelism; k++ {
for range e.config.UploadParallelism {
eg.Go(func() error {
for index := range tasks {
blob := cacheConfig.Layers[index].Blob
Expand Down
7 changes: 3 additions & 4 deletions cache/remotecache/v1/cachestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cacheimport

import (
"context"
"slices"
"time"

"github.com/moby/buildkit/identity"
Expand Down Expand Up @@ -194,10 +195,8 @@ func (cs *cacheKeyStorage) HasLink(id string, link solver.CacheInfoLink, target
selector: link.Selector.String(),
}
if it, ok := cs.byID[id]; ok {
for _, id := range it.links[l] {
if id == target {
return true
}
if slices.Contains(it.links[l], target) {
return true
}
}
return false
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/v1/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
)

func NewCacheChains() *CacheChains {
return &CacheChains{visited: map[interface{}]struct{}{}}
return &CacheChains{visited: map[any]struct{}{}}
}

type CacheChains struct {
items []*item
visited map[interface{}]struct{}
visited map[any]struct{}
}

var _ solver.CacheExporterTarget = &CacheChains{}
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func sortConfig(cc *CacheConfig) {
}

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

type nlink struct {
Expand Down Expand Up @@ -232,7 +232,7 @@ func normalizeItem(it *item, state *normalizeState) (*item, error) {
} else {
// keep tmp IDs deterministic
state.next++
id = digest.FromBytes([]byte(fmt.Sprintf("%d", state.next)))
id = digest.FromBytes(fmt.Appendf(nil, "%d", state.next))
state.byKey[id] = it
it.links = make([]map[link]struct{}, len(it.links))
for i := range it.links {
Expand Down
6 changes: 3 additions & 3 deletions client/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestClientGatewayIntegration(t *testing.T) {
testClientGatewayContainerSecurityModeCaps,
testClientGatewayContainerSecurityModeValidation,
), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
integration.WithMatrix("secmode", map[string]interface{}{
integration.WithMatrix("secmode", map[string]any{
"sandbox": securitySandbox,
"insecure": securityInsecure,
}),
Expand All @@ -79,7 +79,7 @@ func TestClientGatewayIntegration(t *testing.T) {
testClientGatewayContainerHostNetworkingValidation,
),
integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
integration.WithMatrix("netmode", map[string]interface{}{
integration.WithMatrix("netmode", map[string]any{
"default": defaultNetwork,
"host": hostNetwork,
}),
Expand Down Expand Up @@ -1061,7 +1061,7 @@ func newTestPrompt(ctx context.Context, t *testing.T, input io.Writer, output *b
func (p *testPrompt) String() string { return p.prompt }

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

func (p *testPrompt) Send(cmd string) {
Expand Down
Loading

0 comments on commit 8a8d4a2

Please sign in to comment.