Skip to content

Commit 8cddb5f

Browse files
committed
Pass snapshotter opts during Pull
For remote snapshotter cases it's quite often there is need to pass extra info from client (for instance - registry URL to query remote layer from, credentials, etc). This commit slightly extends WithPullSnapshotter to pass extra labels to a snapshotter. Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 269015e commit 8cddb5f

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

client.go

+3
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ type RemoteContext struct {
319319
// Snapshotter used for unpacking
320320
Snapshotter string
321321

322+
// SnapshotterOpts are additional options to be passed to a snapshotter during pull
323+
SnapshotterOpts []snapshots.Opt
324+
322325
// Labels to be applied to the created image
323326
Labels map[string]string
324327

client_opts.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/containerd/containerd/images"
2323
"github.com/containerd/containerd/platforms"
2424
"github.com/containerd/containerd/remotes"
25+
"github.com/containerd/containerd/snapshots"
26+
2527
"google.golang.org/grpc"
2628
)
2729

@@ -138,10 +140,11 @@ func WithUnpackOpts(opts []UnpackOpt) RemoteOpt {
138140
}
139141
}
140142

141-
// WithPullSnapshotter specifies snapshotter name used for unpacking
142-
func WithPullSnapshotter(snapshotterName string) RemoteOpt {
143+
// WithPullSnapshotter specifies snapshotter name used for unpacking.
144+
func WithPullSnapshotter(snapshotterName string, opts ...snapshots.Opt) RemoteOpt {
143145
return func(_ *Client, c *RemoteContext) error {
144146
c.Snapshotter = snapshotterName
147+
c.SnapshotterOpts = opts
145148
return nil
146149
}
147150
}

pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
7272
if err != nil {
7373
return nil, errors.Wrap(err, "create unpacker")
7474
}
75-
unpackWrapper, unpackEg = u.handlerWrapper(ctx, &unpacks)
75+
unpackWrapper, unpackEg = u.handlerWrapper(ctx, pullCtx, &unpacks)
7676
defer func() {
7777
if err := unpackEg.Wait(); err != nil {
7878
if retErr == nil {

snapshots/snapshotter.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,17 @@ type Cleaner interface {
355355
// Opt allows setting mutable snapshot properties on creation
356356
type Opt func(info *Info) error
357357

358-
// WithLabels adds labels to a created snapshot
358+
// WithLabels appends labels to a created snapshot
359359
func WithLabels(labels map[string]string) Opt {
360360
return func(info *Info) error {
361-
info.Labels = labels
361+
if info.Labels == nil {
362+
info.Labels = make(map[string]string)
363+
}
364+
365+
for k, v := range labels {
366+
info.Labels[k] = v
367+
}
368+
362369
return nil
363370
}
364371
}

unpacker.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ func (c *Client) newUnpacker(ctx context.Context, rCtx *RemoteContext) (*unpacke
7272
}, nil
7373
}
7474

75-
func (u *unpacker) unpack(ctx context.Context, h images.Handler, config ocispec.Descriptor, layers []ocispec.Descriptor) error {
75+
func (u *unpacker) unpack(
76+
ctx context.Context,
77+
rCtx *RemoteContext,
78+
h images.Handler,
79+
config ocispec.Descriptor,
80+
layers []ocispec.Descriptor,
81+
) error {
7682
p, err := content.ReadBlob(ctx, u.c.ContentStore(), config)
7783
if err != nil {
7884
return err
@@ -123,17 +129,17 @@ EachLayer:
123129
labels = make(map[string]string)
124130
}
125131
labels[labelSnapshotRef] = chainID
126-
labelOpt := snapshots.WithLabels(labels)
127132

128133
var (
129134
key string
130135
mounts []mount.Mount
136+
opts = append(rCtx.SnapshotterOpts, snapshots.WithLabels(labels))
131137
)
132138

133139
for try := 1; try <= 3; try++ {
134140
// Prepare snapshot with from parent, label as root
135141
key = fmt.Sprintf("extract-%s %s", uniquePart(), chainID)
136-
mounts, err = sn.Prepare(ctx, key, parent.String(), labelOpt)
142+
mounts, err = sn.Prepare(ctx, key, parent.String(), opts...)
137143
if err != nil {
138144
if errdefs.IsAlreadyExists(err) {
139145
if _, err := sn.Stat(ctx, chainID); err != nil {
@@ -201,7 +207,7 @@ EachLayer:
201207
return errors.Errorf("wrong diff id calculated on extraction %q", diffIDs[i])
202208
}
203209

204-
if err = sn.Commit(ctx, chainID, key, labelOpt); err != nil {
210+
if err = sn.Commit(ctx, chainID, key, opts...); err != nil {
205211
abort()
206212
if errdefs.IsAlreadyExists(err) {
207213
continue
@@ -271,7 +277,11 @@ func (u *unpacker) fetch(ctx context.Context, h images.Handler, layers []ocispec
271277
return eg.Wait()
272278
}
273279

274-
func (u *unpacker) handlerWrapper(uctx context.Context, unpacks *int32) (func(images.Handler) images.Handler, *errgroup.Group) {
280+
func (u *unpacker) handlerWrapper(
281+
uctx context.Context,
282+
rCtx *RemoteContext,
283+
unpacks *int32,
284+
) (func(images.Handler) images.Handler, *errgroup.Group) {
275285
eg, uctx := errgroup.WithContext(uctx)
276286
return func(f images.Handler) images.Handler {
277287
var (
@@ -313,7 +323,7 @@ func (u *unpacker) handlerWrapper(uctx context.Context, unpacks *int32) (func(im
313323
if len(l) > 0 {
314324
atomic.AddInt32(unpacks, 1)
315325
eg.Go(func() error {
316-
return u.unpack(uctx, f, desc, l)
326+
return u.unpack(uctx, rCtx, f, desc, l)
317327
})
318328
}
319329
}

0 commit comments

Comments
 (0)