Skip to content

Commit da2ffa5

Browse files
committed
contentutil: use Pull (with Unpack), not Fetch
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent f0cff12 commit da2ffa5

2 files changed

Lines changed: 25 additions & 64 deletions

File tree

pkg/contentutil/contentutil.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,26 @@ import (
3232
"github.com/containerd/containerd/images"
3333
"github.com/containerd/containerd/log"
3434
"github.com/containerd/containerd/pkg/progress"
35-
"github.com/containerd/containerd/platforms"
3635
"github.com/containerd/containerd/remotes"
3736
"github.com/opencontainers/go-digest"
3837
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3938
)
4039

41-
// FetchConfig for content fetch
42-
type FetchConfig struct {
40+
// PullConfig for content fetch
41+
type PullConfig struct {
4342
// Resolver
4443
Resolver remotes.Resolver
4544
// ProgressOutput to display progress
4645
ProgressOutput io.Writer
47-
// Labels to set on the content
48-
Labels []string
49-
// PlatformMatcher matches platforms, supersedes Platforms
50-
PlatformMatcher platforms.MatchComparer
51-
// Platforms to fetch
52-
Platforms []string
53-
// Whether or not download all metadata
54-
AllMetadata bool
55-
// RemoteOpts is not used by ctr, but can be used by other CLI tools
46+
// RemoteOpts, e.g. containerd.WithPullUnpack.
47+
//
48+
// Regardless to RemoteOpts, the following opts are always set:
49+
// WithResolver, WithImageHandler, WithSchema1Conversion
5650
RemoteOpts []containerd.RemoteOpt
5751
}
5852

59-
// Fetch loads all resources into the content store and returns the image
60-
func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) {
53+
// Pull loads all resources into the content store and returns the image
54+
func Pull(ctx context.Context, client *containerd.Client, ref string, config *PullConfig) (containerd.Image, error) {
6155
ongoing := newJobs(ref)
6256

6357
pctx, stopProgress := context.WithCancel(ctx)
@@ -86,22 +80,10 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, config *F
8680
}
8781
opts = append(opts, config.RemoteOpts...)
8882

89-
if config.AllMetadata {
90-
opts = append(opts, containerd.WithAllMetadata())
91-
}
92-
93-
if config.PlatformMatcher != nil {
94-
opts = append(opts, containerd.WithPlatformMatcher(config.PlatformMatcher))
95-
} else {
96-
for _, platform := range config.Platforms {
97-
opts = append(opts, containerd.WithPlatform(platform))
98-
}
99-
}
100-
101-
img, err := client.Fetch(pctx, ref, opts...)
83+
img, err := client.Pull(pctx, ref, opts...)
10284
stopProgress()
10385
if err != nil {
104-
return images.Image{}, err
86+
return nil, err
10587
}
10688

10789
<-progress

pkg/imgutil/imgutil.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@ package imgutil
1919

2020
import (
2121
"context"
22-
"fmt"
2322
"io"
2423
"strings"
2524

2625
"github.com/AkihiroSuda/nerdctl/pkg/contentutil"
2726
"github.com/containerd/containerd"
28-
"github.com/containerd/containerd/images"
29-
"github.com/containerd/containerd/platforms"
3027
refdocker "github.com/containerd/containerd/reference/docker"
3128
"github.com/containerd/containerd/remotes/docker"
3229
"github.com/containerd/stargz-snapshotter/fs/source"
33-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3430
"github.com/pkg/errors"
3531
"github.com/sirupsen/logrus"
3632
)
@@ -84,42 +80,25 @@ func EnsureImage(ctx context.Context, client *containerd.Client, stdout io.Write
8480
resolver := docker.NewResolver(resovlerOpts)
8581

8682
var containerdImage containerd.Image
83+
config := &contentutil.PullConfig{
84+
Resolver: resolver,
85+
ProgressOutput: stdout,
86+
RemoteOpts: []containerd.RemoteOpt{
87+
containerd.WithPullUnpack,
88+
containerd.WithPullSnapshotter(snapshotter),
89+
},
90+
}
8791
sgz := isStargz(snapshotter)
8892
if sgz {
89-
h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
90-
if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
91-
fmt.Fprintf(stdout, "fetching %v... %v\n", desc.Digest.String()[:15], desc.MediaType)
92-
}
93-
return nil, nil
94-
})
9593
// TODO: support "skip-content-verify"
96-
opts := []containerd.RemoteOpt{
97-
containerd.WithResolver(resolver),
98-
containerd.WithImageHandler(h),
99-
containerd.WithSchema1Conversion,
100-
containerd.WithPullUnpack,
101-
containerd.WithPullSnapshotter(snapshotter),
94+
config.RemoteOpts = append(
95+
config.RemoteOpts,
10296
containerd.WithImageHandlerWrapper(source.AppendDefaultLabelsHandlerWrapper(ref, 10*1024*1024)),
103-
}
104-
containerdImage, err = client.Pull(ctx, ref, opts...)
105-
if err != nil {
106-
return nil, err
107-
}
108-
} else {
109-
config := &contentutil.FetchConfig{
110-
Resolver: resolver,
111-
ProgressOutput: stdout,
112-
PlatformMatcher: platforms.Default(),
113-
}
114-
115-
img, err := contentutil.Fetch(ctx, client, ref, config)
116-
if err != nil {
117-
return nil, err
118-
}
119-
containerdImage = containerd.NewImageWithPlatform(client, img, config.PlatformMatcher)
120-
if err := containerdImage.Unpack(ctx, snapshotter); err != nil {
121-
return nil, err
122-
}
97+
)
98+
}
99+
containerdImage, err = contentutil.Pull(ctx, client, ref, config)
100+
if err != nil {
101+
return nil, err
123102
}
124103
res := &EnsuredImage{
125104
Ref: ref,

0 commit comments

Comments
 (0)