Merge pull request #4080 from mxpv/opts

Pass snapshotter opts from client Pull
This commit is contained in:
Michael Crosby 2020-03-06 10:26:28 -05:00 committed by GitHub
commit 35a8de8996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 12 deletions

View File

@ -319,6 +319,9 @@ type RemoteContext struct {
// Snapshotter used for unpacking // Snapshotter used for unpacking
Snapshotter string Snapshotter string
// SnapshotterOpts are additional options to be passed to a snapshotter during pull
SnapshotterOpts []snapshots.Opt
// Labels to be applied to the created image // Labels to be applied to the created image
Labels map[string]string Labels map[string]string

View File

@ -22,6 +22,8 @@ import (
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/snapshots"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -138,10 +140,11 @@ func WithUnpackOpts(opts []UnpackOpt) RemoteOpt {
} }
} }
// WithPullSnapshotter specifies snapshotter name used for unpacking // WithPullSnapshotter specifies snapshotter name used for unpacking.
func WithPullSnapshotter(snapshotterName string) RemoteOpt { func WithPullSnapshotter(snapshotterName string, opts ...snapshots.Opt) RemoteOpt {
return func(_ *Client, c *RemoteContext) error { return func(_ *Client, c *RemoteContext) error {
c.Snapshotter = snapshotterName c.Snapshotter = snapshotterName
c.SnapshotterOpts = opts
return nil return nil
} }
} }

View File

@ -72,7 +72,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
if err != nil { if err != nil {
return nil, errors.Wrap(err, "create unpacker") return nil, errors.Wrap(err, "create unpacker")
} }
unpackWrapper, unpackEg = u.handlerWrapper(ctx, &unpacks) unpackWrapper, unpackEg = u.handlerWrapper(ctx, pullCtx, &unpacks)
defer func() { defer func() {
if err := unpackEg.Wait(); err != nil { if err := unpackEg.Wait(); err != nil {
if retErr == nil { if retErr == nil {

View File

@ -129,7 +129,7 @@ func applyLayers(ctx context.Context, layers []Layer, chain []digest.Digest, sn
mounts, err = sn.Prepare(ctx, key, parent.String(), opts...) mounts, err = sn.Prepare(ctx, key, parent.String(), opts...)
if err != nil { if err != nil {
if errdefs.IsNotFound(err) && len(layers) > 1 { if errdefs.IsNotFound(err) && len(layers) > 1 {
if err := applyLayers(ctx, layers[:len(layers)-1], chain[:len(chain)-1], sn, a, nil, applyOpts); err != nil { if err := applyLayers(ctx, layers[:len(layers)-1], chain[:len(chain)-1], sn, a, opts, applyOpts); err != nil {
if !errdefs.IsAlreadyExists(err) { if !errdefs.IsAlreadyExists(err) {
return err return err
} }

View File

@ -355,10 +355,17 @@ type Cleaner interface {
// Opt allows setting mutable snapshot properties on creation // Opt allows setting mutable snapshot properties on creation
type Opt func(info *Info) error type Opt func(info *Info) error
// WithLabels adds labels to a created snapshot // WithLabels appends labels to a created snapshot
func WithLabels(labels map[string]string) Opt { func WithLabels(labels map[string]string) Opt {
return func(info *Info) error { return func(info *Info) error {
info.Labels = labels if info.Labels == nil {
info.Labels = make(map[string]string)
}
for k, v := range labels {
info.Labels[k] = v
}
return nil return nil
} }
} }

View File

@ -72,7 +72,13 @@ func (c *Client) newUnpacker(ctx context.Context, rCtx *RemoteContext) (*unpacke
}, nil }, nil
} }
func (u *unpacker) unpack(ctx context.Context, h images.Handler, config ocispec.Descriptor, layers []ocispec.Descriptor) error { func (u *unpacker) unpack(
ctx context.Context,
rCtx *RemoteContext,
h images.Handler,
config ocispec.Descriptor,
layers []ocispec.Descriptor,
) error {
p, err := content.ReadBlob(ctx, u.c.ContentStore(), config) p, err := content.ReadBlob(ctx, u.c.ContentStore(), config)
if err != nil { if err != nil {
return err return err
@ -123,17 +129,17 @@ EachLayer:
labels = make(map[string]string) labels = make(map[string]string)
} }
labels[labelSnapshotRef] = chainID labels[labelSnapshotRef] = chainID
labelOpt := snapshots.WithLabels(labels)
var ( var (
key string key string
mounts []mount.Mount mounts []mount.Mount
opts = append(rCtx.SnapshotterOpts, snapshots.WithLabels(labels))
) )
for try := 1; try <= 3; try++ { for try := 1; try <= 3; try++ {
// Prepare snapshot with from parent, label as root // Prepare snapshot with from parent, label as root
key = fmt.Sprintf("extract-%s %s", uniquePart(), chainID) key = fmt.Sprintf("extract-%s %s", uniquePart(), chainID)
mounts, err = sn.Prepare(ctx, key, parent.String(), labelOpt) mounts, err = sn.Prepare(ctx, key, parent.String(), opts...)
if err != nil { if err != nil {
if errdefs.IsAlreadyExists(err) { if errdefs.IsAlreadyExists(err) {
if _, err := sn.Stat(ctx, chainID); err != nil { if _, err := sn.Stat(ctx, chainID); err != nil {
@ -201,7 +207,7 @@ EachLayer:
return errors.Errorf("wrong diff id calculated on extraction %q", diffIDs[i]) return errors.Errorf("wrong diff id calculated on extraction %q", diffIDs[i])
} }
if err = sn.Commit(ctx, chainID, key, labelOpt); err != nil { if err = sn.Commit(ctx, chainID, key, opts...); err != nil {
abort() abort()
if errdefs.IsAlreadyExists(err) { if errdefs.IsAlreadyExists(err) {
continue continue
@ -271,7 +277,11 @@ func (u *unpacker) fetch(ctx context.Context, h images.Handler, layers []ocispec
return eg.Wait() return eg.Wait()
} }
func (u *unpacker) handlerWrapper(uctx context.Context, unpacks *int32) (func(images.Handler) images.Handler, *errgroup.Group) { func (u *unpacker) handlerWrapper(
uctx context.Context,
rCtx *RemoteContext,
unpacks *int32,
) (func(images.Handler) images.Handler, *errgroup.Group) {
eg, uctx := errgroup.WithContext(uctx) eg, uctx := errgroup.WithContext(uctx)
return func(f images.Handler) images.Handler { return func(f images.Handler) images.Handler {
var ( var (
@ -313,7 +323,7 @@ func (u *unpacker) handlerWrapper(uctx context.Context, unpacks *int32) (func(im
if len(l) > 0 { if len(l) > 0 {
atomic.AddInt32(unpacks, 1) atomic.AddInt32(unpacks, 1)
eg.Go(func() error { eg.Go(func() error {
return u.unpack(uctx, f, desc, l) return u.unpack(uctx, rCtx, f, desc, l)
}) })
} }
} }