Skip to content

Commit

Permalink
WIP: vendor: github.com/containerd/containerd v1.7.18
Browse files Browse the repository at this point in the history
Update to containerd 1.7.18, which now migrated to the errdefs module. The
existing errdefs package is now an alias for the module, and should no longer
be used directly.

This patch:

- updates the containerd dependency
- replaces uses of the old package in favor of the new module
- adds a linter check to prevent accidental re-introduction of the old package
- adds a linter check to enforce using an alias, to prevent accidental use
  of the errdefs package in BuildKit or Moby.
- adds a linter check to prevent using the "log" package, which was also
  migrated to a separate module.

There are still some uses of the old package in (indirect) dependencies,
which should go away over time.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed May 28, 2024
1 parent 4cf5e34 commit 3ab93cc
Show file tree
Hide file tree
Showing 126 changed files with 563 additions and 376 deletions.
12 changes: 10 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ linters-settings:
rules:
main:
deny:
# The io/ioutil package has been deprecated.
# https://go.dev/doc/go1.16#ioutil
- pkg: "github.com/containerd/containerd/errdefs"
desc: The containerd errdefs package was migrated to a separate module. Use github.com/containerd/errdefs instead.
- pkg: "github.com/containerd/containerd/log"
desc: The containerd log package was migrated to a separate module. Use github.com/containerd/log instead.
- pkg: "io/ioutil"
desc: The io/ioutil package has been deprecated.
forbidigo:
Expand All @@ -61,10 +63,16 @@ linters-settings:
- '^ctx\.Err(# use context\.Cause instead)?$'
importas:
alias:
# Enforce alias to prevent it accidentally being used instead of our
# own errdefs package (or vice-versa).
- pkg: "github.com/containerd/errdefs"
alias: "cerrdefs"
- pkg: "github.com/opencontainers/image-spec/specs-go/v1"
alias: "ocispecs"
- pkg: "github.com/opencontainers/go-digest"
alias: "digest"

# Do not allow unaliased imports of aliased packages.
no-unaliased: true
gosec:
excludes:
Expand Down
4 changes: 2 additions & 2 deletions cache/blobs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"io"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
labelspkg "github.com/containerd/containerd/labels"
"github.com/containerd/containerd/mount"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/overlay"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
}
dgst := cw.Digest()
if err := cw.Commit(ctx, 0, dgst, commitopts...); err != nil {
if !errdefs.IsAlreadyExists(err) {
if !cerrdefs.IsAlreadyExists(err) {
return emptyDesc, false, errors.Wrap(err, "failed to commit")
}
}
Expand Down
4 changes: 2 additions & 2 deletions cache/compression_nydus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"io"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/labels"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/compression"
digest "github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -95,7 +95,7 @@ func MergeNydus(ctx context.Context, ref ImmutableRef, comp compression.Config,
if err := cw.Commit(ctx, 0, compressedDgst, content.WithLabels(map[string]string{
labels.LabelUncompressed: uncompressedDgst.Digest().String(),
})); err != nil {
if !errdefs.IsAlreadyExists(err) {
if !cerrdefs.IsAlreadyExists(err) {
return nil, errors.Wrap(err, "commit to content store")
}
}
Expand Down
10 changes: 5 additions & 5 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/filters"
"github.com/containerd/containerd/gc"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/leases"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/cache/metadata"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -137,7 +137,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,

descHandlers := descHandlersOf(opts...)
if desc.Digest != "" && (descHandlers == nil || descHandlers[desc.Digest] == nil) {
if _, err := cm.ContentStore.Info(ctx, desc.Digest); errors.Is(err, errdefs.ErrNotFound) {
if _, err := cm.ContentStore.Info(ctx, desc.Digest); errors.Is(err, cerrdefs.ErrNotFound) {
return nil, NeedsRemoteProviderError([]digest.Digest{desc.Digest})
} else if err != nil {
return nil, err
Expand Down Expand Up @@ -253,7 +253,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,
if err := cm.LeaseManager.AddResource(ctx, l, leases.Resource{
ID: snapshotID,
Type: "snapshots/" + cm.Snapshotter.Name(),
}); err != nil && !errdefs.IsAlreadyExists(err) {
}); err != nil && !cerrdefs.IsAlreadyExists(err) {
return nil, errors.Wrapf(err, "failed to add snapshot %s to lease", id)
}

Expand Down Expand Up @@ -484,7 +484,7 @@ func (cm *cacheManager) getRecord(ctx context.Context, id string, opts ...RefOpt
if rec.mutable {
// If the record is mutable, then the snapshot must exist
if _, err := cm.Snapshotter.Stat(ctx, rec.ID()); err != nil {
if !errdefs.IsNotFound(err) {
if !cerrdefs.IsNotFound(err) {
return nil, errors.Wrap(err, "failed to check mutable ref snapshot")
}
// the snapshot doesn't exist, clear this record
Expand Down Expand Up @@ -609,7 +609,7 @@ func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, sess session.Gr
if err := cm.LeaseManager.AddResource(ctx, l, leases.Resource{
ID: snapshotID,
Type: "snapshots/" + cm.Snapshotter.Name(),
}); err != nil && !errdefs.IsAlreadyExists(err) {
}); err != nil && !cerrdefs.IsAlreadyExists(err) {
return nil, errors.Wrapf(err, "failed to add snapshot %s to lease", snapshotID)
}

Expand Down
8 changes: 4 additions & 4 deletions cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/containerd/containerd/content/local"
"github.com/containerd/containerd/diff/apply"
"github.com/containerd/containerd/diff/walking"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/leases"
ctdmetadata "github.com/containerd/containerd/metadata"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/native"
"github.com/containerd/continuity/fs/fstest"
cerrdefs "github.com/containerd/errdefs"
"github.com/containerd/stargz-snapshotter/estargz"
"github.com/klauspost/compress/zstd"
"github.com/moby/buildkit/cache/config"
Expand Down Expand Up @@ -1196,7 +1196,7 @@ func TestLoopLeaseContent(t *testing.T) {
dgst := cur.Digest
visited[dgst] = struct{}{}
info, err := co.cs.Info(ctx, dgst)
if err != nil && !errors.Is(err, errdefs.ErrNotFound) {
if err != nil && !errors.Is(err, cerrdefs.ErrNotFound) {
require.NoError(t, err)
}
var children []ocispecs.Descriptor
Expand Down Expand Up @@ -1232,7 +1232,7 @@ func TestLoopLeaseContent(t *testing.T) {
// Check if contents are cleaned up
for _, d := range gotChain {
_, err := co.cs.Info(ctx, d)
require.ErrorIs(t, err, errdefs.ErrNotFound)
require.ErrorIs(t, err, cerrdefs.ErrNotFound)
}
}

Expand Down Expand Up @@ -2005,7 +2005,7 @@ func checkDescriptor(ctx context.Context, t *testing.T, cs content.Store, desc o
// Check annotation values are valid
c := new(iohelper.Counter)
ra, err := cs.ReaderAt(ctx, desc)
if err != nil && errdefs.IsNotFound(err) {
if err != nil && cerrdefs.IsNotFound(err) {
return // lazy layer
}
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions cache/migrate_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"time"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/snapshots"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/cache/metadata"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/bklog"
Expand Down Expand Up @@ -185,7 +185,7 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
})
if err != nil {
// if we are running the migration twice
if errors.Is(err, errdefs.ErrAlreadyExists) {
if errors.Is(err, cerrdefs.ErrAlreadyExists) {
continue
}
return errors.Wrap(err, "failed to create lease")
Expand Down Expand Up @@ -216,7 +216,7 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
if _, err := s.Update(ctx, snapshots.Info{
Name: md.getSnapshotID(),
}, "labels.containerd.io/gc.root"); err != nil {
if !errors.Is(err, errdefs.ErrNotFound) {
if !errors.Is(err, cerrdefs.ErrNotFound) {
return err
}
}
Expand All @@ -237,7 +237,7 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
if _, err := s.Update(ctx, snapshots.Info{
Name: info.Name,
}, "labels.containerd.io/gc.root"); err != nil {
if !errors.Is(err, errdefs.ErrNotFound) {
if !errors.Is(err, cerrdefs.ErrNotFound) {
return err
}
}
Expand Down
34 changes: 17 additions & 17 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"time"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/snapshots"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/pkg/idtools"
"github.com/hashicorp/go-multierror"
"github.com/moby/buildkit/cache/config"
Expand Down Expand Up @@ -292,7 +292,7 @@ func (cr *cacheRecord) isLazy(ctx context.Context) (bool, error) {
return false, nil
}
_, err := cr.cm.ContentStore.Info(ctx, dgst)
if errors.Is(err, errdefs.ErrNotFound) {
if errors.Is(err, cerrdefs.ErrNotFound) {
return true, nil
} else if err != nil {
return false, err
Expand Down Expand Up @@ -349,7 +349,7 @@ func (cr *cacheRecord) size(ctx context.Context) (int64, error) {
if isDead {
return 0, nil
}
if !errors.Is(err, errdefs.ErrNotFound) {
if !errors.Is(err, cerrdefs.ErrNotFound) {
return s, errors.Wrapf(err, "failed to get usage for %s", cr.ID())
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func (cr *cacheRecord) mount(ctx context.Context) (_ snapshot.Mountable, rerr er
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
}
return nil
}, leaseutil.MakeTemporary); err != nil && !errdefs.IsAlreadyExists(err) {
}, leaseutil.MakeTemporary); err != nil && !cerrdefs.IsAlreadyExists(err) {
return nil, err
}
defer func() {
Expand All @@ -412,14 +412,14 @@ func (cr *cacheRecord) mount(ctx context.Context) (_ snapshot.Mountable, rerr er
if err := cr.cm.LeaseManager.AddResource(ctx, leases.Lease{ID: cr.viewLeaseID()}, leases.Resource{
ID: mountSnapshotID,
Type: "snapshots/" + cr.cm.Snapshotter.Name(),
}); err != nil && !errdefs.IsAlreadyExists(err) {
}); err != nil && !cerrdefs.IsAlreadyExists(err) {
return nil, err
}
// Return the mount direct from View rather than setting it using the Mounts call below.
// The two are equivalent for containerd snapshotters but the moby snapshotter requires
// the use of the mountable returned by View in this case.
mnts, err := cr.cm.Snapshotter.View(ctx, mountSnapshotID, cr.getSnapshotID())
if err != nil && !errdefs.IsAlreadyExists(err) {
if err != nil && !cerrdefs.IsAlreadyExists(err) {
return nil, err
}
cr.mountCache = mnts
Expand Down Expand Up @@ -455,12 +455,12 @@ func (cr *cacheRecord) remove(ctx context.Context, removeSnapshot bool) (rerr er
if removeSnapshot {
if err := cr.cm.LeaseManager.Delete(ctx, leases.Lease{
ID: cr.ID(),
}); err != nil && !errdefs.IsNotFound(err) {
}); err != nil && !cerrdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete lease for %s", cr.ID())
}
if err := cr.cm.LeaseManager.Delete(ctx, leases.Lease{
ID: cr.compressionVariantsLeaseID(),
}); err != nil && !errdefs.IsNotFound(err) {
}); err != nil && !cerrdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete compression variant lease for %s", cr.ID())
}
}
Expand Down Expand Up @@ -764,7 +764,7 @@ func (sr *immutableRef) linkBlob(ctx context.Context, desc ocispecs.Descriptor)
l.ID = sr.compressionVariantsLeaseID()
// do not make it flat lease to allow linking blobs using gc label
return nil
}); err != nil && !errdefs.IsAlreadyExists(err) {
}); err != nil && !cerrdefs.IsAlreadyExists(err) {
return err
}
if err := sr.cm.LeaseManager.AddResource(ctx, leases.Lease{ID: sr.compressionVariantsLeaseID()}, leases.Resource{
Expand Down Expand Up @@ -828,7 +828,7 @@ func getBlobWithCompression(ctx context.Context, cs content.Store, desc ocispecs
}
return true
}); err != nil || target == nil {
return ocispecs.Descriptor{}, errdefs.ErrNotFound
return ocispecs.Descriptor{}, cerrdefs.ErrNotFound
}
return *target, nil
}
Expand All @@ -849,7 +849,7 @@ func walkBlobVariantsOnly(ctx context.Context, cs content.Store, dgst digest.Dig
}
visited[dgst] = struct{}{}
info, err := cs.Info(ctx, dgst)
if errors.Is(err, errdefs.ErrNotFound) {
if errors.Is(err, cerrdefs.ErrNotFound) {
return true, nil
} else if err != nil {
return false, err
Expand Down Expand Up @@ -1025,9 +1025,9 @@ func (sr *immutableRef) withRemoteSnapshotLabelsStargzMode(ctx context.Context,
for _, r := range sr.layerChain() {
r := r
info, err := r.cm.Snapshotter.Stat(ctx, r.getSnapshotID())
if err != nil && !errdefs.IsNotFound(err) {
if err != nil && !cerrdefs.IsNotFound(err) {
return err
} else if errdefs.IsNotFound(err) {
} else if cerrdefs.IsNotFound(err) {
continue // This snpashot doesn't exist; skip
} else if _, ok := info.Labels["containerd.io/snapshot/remote"]; !ok {
continue // This isn't a remote snapshot; skip
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func (sr *immutableRef) prepareRemoteSnapshotsStargzMode(ctx context.Context, s
parentID = r.layerParent.getSnapshotID()
}
if err := r.cm.Snapshotter.Prepare(ctx, key, parentID, opts...); err != nil {
if errdefs.IsAlreadyExists(err) {
if cerrdefs.IsAlreadyExists(err) {
// Check if the targeting snapshot ID has been prepared as
// a remote snapshot in the snapshotter.
info, err := r.cm.Snapshotter.Stat(ctx, snapshotID)
Expand Down Expand Up @@ -1332,7 +1332,7 @@ func (sr *immutableRef) unlazyLayer(ctx context.Context, dhs DescHandlers, pg pr
return err
}
if err := sr.cm.Snapshotter.Commit(ctx, sr.getSnapshotID(), key); err != nil {
if !errors.Is(err, errdefs.ErrAlreadyExists) {
if !errors.Is(err, cerrdefs.ErrAlreadyExists) {
return err
}
}
Expand Down Expand Up @@ -1391,7 +1391,7 @@ func (sr *immutableRef) release(ctx context.Context) (rerr error) {
if sr.equalMutable != nil {
sr.equalMutable.release(ctx)
} else {
if err := sr.cm.LeaseManager.Delete(ctx, leases.Lease{ID: sr.viewLeaseID()}); err != nil && !errdefs.IsNotFound(err) {
if err := sr.cm.LeaseManager.Delete(ctx, leases.Lease{ID: sr.viewLeaseID()}); err != nil && !cerrdefs.IsNotFound(err) {
return err
}
sr.mountCache = nil
Expand Down Expand Up @@ -1422,7 +1422,7 @@ func (cr *cacheRecord) finalize(ctx context.Context) error {
return nil
})
if err != nil {
if !errors.Is(err, errdefs.ErrAlreadyExists) { // migrator adds leases for everything
if !errors.Is(err, cerrdefs.ErrAlreadyExists) { // migrator adds leases for everything
return errors.Wrap(err, "failed to create lease")
}
}
Expand Down
6 changes: 3 additions & 3 deletions cache/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/reference"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/cache/config"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
Expand Down Expand Up @@ -301,7 +301,7 @@ type lazyRefProvider struct {

func (p lazyRefProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
if desc.Digest != p.desc.Digest {
return nil, errdefs.ErrNotFound
return nil, cerrdefs.ErrNotFound
}
if err := p.Unlazy(ctx); err != nil {
return nil, err
Expand All @@ -311,7 +311,7 @@ func (p lazyRefProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor)

func (p lazyRefProvider) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
if dgst != p.desc.Digest {
return content.Info{}, errdefs.ErrNotFound
return content.Info{}, cerrdefs.ErrNotFound
}
info, err := p.ref.cm.ContentStore.Info(ctx, dgst)
if err == nil {
Expand Down
Loading

0 comments on commit 3ab93cc

Please sign in to comment.