diff --git a/pkg/contentutil/contentutil.go b/pkg/contentutil/contentutil.go index c5f0898662f..2ea5519d3a0 100644 --- a/pkg/contentutil/contentutil.go +++ b/pkg/contentutil/contentutil.go @@ -32,32 +32,26 @@ import ( "github.com/containerd/containerd/images" "github.com/containerd/containerd/log" "github.com/containerd/containerd/pkg/progress" - "github.com/containerd/containerd/platforms" "github.com/containerd/containerd/remotes" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) -// FetchConfig for content fetch -type FetchConfig struct { +// PullConfig for content fetch +type PullConfig struct { // Resolver Resolver remotes.Resolver // ProgressOutput to display progress ProgressOutput io.Writer - // Labels to set on the content - Labels []string - // PlatformMatcher matches platforms, supersedes Platforms - PlatformMatcher platforms.MatchComparer - // Platforms to fetch - Platforms []string - // Whether or not download all metadata - AllMetadata bool - // RemoteOpts is not used by ctr, but can be used by other CLI tools + // RemoteOpts, e.g. containerd.WithPullUnpack. + // + // Regardless to RemoteOpts, the following opts are always set: + // WithResolver, WithImageHandler, WithSchema1Conversion RemoteOpts []containerd.RemoteOpt } -// Fetch loads all resources into the content store and returns the image -func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) { +// Pull loads all resources into the content store and returns the image +func Pull(ctx context.Context, client *containerd.Client, ref string, config *PullConfig) (containerd.Image, error) { ongoing := newJobs(ref) pctx, stopProgress := context.WithCancel(ctx) @@ -86,22 +80,10 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, config *F } opts = append(opts, config.RemoteOpts...) - if config.AllMetadata { - opts = append(opts, containerd.WithAllMetadata()) - } - - if config.PlatformMatcher != nil { - opts = append(opts, containerd.WithPlatformMatcher(config.PlatformMatcher)) - } else { - for _, platform := range config.Platforms { - opts = append(opts, containerd.WithPlatform(platform)) - } - } - - img, err := client.Fetch(pctx, ref, opts...) + img, err := client.Pull(pctx, ref, opts...) stopProgress() if err != nil { - return images.Image{}, err + return nil, err } <-progress diff --git a/pkg/imgutil/imgutil.go b/pkg/imgutil/imgutil.go index 609eb44d4fe..fc84660db25 100644 --- a/pkg/imgutil/imgutil.go +++ b/pkg/imgutil/imgutil.go @@ -19,18 +19,14 @@ package imgutil import ( "context" - "fmt" "io" "strings" "github.com/AkihiroSuda/nerdctl/pkg/contentutil" "github.com/containerd/containerd" - "github.com/containerd/containerd/images" - "github.com/containerd/containerd/platforms" refdocker "github.com/containerd/containerd/reference/docker" "github.com/containerd/containerd/remotes/docker" "github.com/containerd/stargz-snapshotter/fs/source" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -84,42 +80,25 @@ func EnsureImage(ctx context.Context, client *containerd.Client, stdout io.Write resolver := docker.NewResolver(resovlerOpts) var containerdImage containerd.Image + config := &contentutil.PullConfig{ + Resolver: resolver, + ProgressOutput: stdout, + RemoteOpts: []containerd.RemoteOpt{ + containerd.WithPullUnpack, + containerd.WithPullSnapshotter(snapshotter), + }, + } sgz := isStargz(snapshotter) if sgz { - h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { - if desc.MediaType != images.MediaTypeDockerSchema1Manifest { - fmt.Fprintf(stdout, "fetching %v... %v\n", desc.Digest.String()[:15], desc.MediaType) - } - return nil, nil - }) // TODO: support "skip-content-verify" - opts := []containerd.RemoteOpt{ - containerd.WithResolver(resolver), - containerd.WithImageHandler(h), - containerd.WithSchema1Conversion, - containerd.WithPullUnpack, - containerd.WithPullSnapshotter(snapshotter), + config.RemoteOpts = append( + config.RemoteOpts, containerd.WithImageHandlerWrapper(source.AppendDefaultLabelsHandlerWrapper(ref, 10*1024*1024)), - } - containerdImage, err = client.Pull(ctx, ref, opts...) - if err != nil { - return nil, err - } - } else { - config := &contentutil.FetchConfig{ - Resolver: resolver, - ProgressOutput: stdout, - PlatformMatcher: platforms.Default(), - } - - img, err := contentutil.Fetch(ctx, client, ref, config) - if err != nil { - return nil, err - } - containerdImage = containerd.NewImageWithPlatform(client, img, config.PlatformMatcher) - if err := containerdImage.Unpack(ctx, snapshotter); err != nil { - return nil, err - } + ) + } + containerdImage, err = contentutil.Pull(ctx, client, ref, config) + if err != nil { + return nil, err } res := &EnsuredImage{ Ref: ref,