Skip to content

Commit

Permalink
contentutil: use Pull (with Unpack), not Fetch
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 10, 2020
1 parent f0cff12 commit da2ffa5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 64 deletions.
38 changes: 10 additions & 28 deletions pkg/contentutil/contentutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
51 changes: 15 additions & 36 deletions pkg/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit da2ffa5

Please sign in to comment.