Respect default snapshotter label

Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
Maksym Pavlenko 2019-07-10 12:16:43 -07:00
parent 47d2ac0902
commit 1918ee4d11
6 changed files with 17 additions and 34 deletions

View File

@ -332,7 +332,6 @@ func defaultRemoteContext() *RemoteContext {
Resolver: docker.NewResolver(docker.ResolverOptions{ Resolver: docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient, Client: http.DefaultClient,
}), }),
Snapshotter: DefaultSnapshotter,
} }
} }
@ -672,7 +671,13 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
}, nil }, nil
} }
func (c *Client) getSnapshotter(name string) (snapshots.Snapshotter, error) { func (c *Client) getSnapshotter(ctx context.Context, name string) (snapshots.Snapshotter, error) {
if name == "" {
if err := c.GetLabel(ctx, defaults.DefaultSnapshotterNSLabel, &name, DefaultSnapshotter); err != nil {
return nil, err
}
}
s := c.SnapshotService(name) s := c.SnapshotService(name)
if s == nil { if s == nil {
return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name) return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name)

View File

@ -24,7 +24,6 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/containerd/containerd"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -34,7 +33,6 @@ var (
cli.StringFlag{ cli.StringFlag{
Name: "snapshotter", Name: "snapshotter",
Usage: "snapshotter name. Empty value stands for the default value.", Usage: "snapshotter name. Empty value stands for the default value.",
Value: containerd.DefaultSnapshotter,
EnvVar: "CONTAINERD_SNAPSHOTTER", EnvVar: "CONTAINERD_SNAPSHOTTER",
}, },
} }

View File

@ -233,7 +233,7 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
} }
// get the rootfs from the snapshotter and add it to the request // get the rootfs from the snapshotter and add it to the request
s, err := c.client.getSnapshotter(r.Snapshotter) s, err := c.client.getSnapshotter(ctx, r.Snapshotter)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,9 +20,7 @@ import (
"context" "context"
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/snapshots" "github.com/containerd/containerd/snapshots"
@ -118,9 +116,8 @@ func WithSnapshotter(name string) NewContainerOpts {
// WithSnapshot uses an existing root filesystem for the container // WithSnapshot uses an existing root filesystem for the container
func WithSnapshot(id string) NewContainerOpts { func WithSnapshot(id string) NewContainerOpts {
return func(ctx context.Context, client *Client, c *containers.Container) error { return func(ctx context.Context, client *Client, c *containers.Container) error {
setSnapshotterIfEmpty(ctx, client, c)
// check that the snapshot exists, if not, fail on creation // check that the snapshot exists, if not, fail on creation
s, err := client.getSnapshotter(c.Snapshotter) s, err := client.getSnapshotter(ctx, c.Snapshotter)
if err != nil { if err != nil {
return err return err
} }
@ -140,9 +137,9 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts
if err != nil { if err != nil {
return err return err
} }
setSnapshotterIfEmpty(ctx, client, c)
parent := identity.ChainID(diffIDs).String() parent := identity.ChainID(diffIDs).String()
s, err := client.getSnapshotter(c.Snapshotter) s, err := client.getSnapshotter(ctx, c.Snapshotter)
if err != nil { if err != nil {
return err return err
} }
@ -161,7 +158,7 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta
if c.Snapshotter == "" { if c.Snapshotter == "" {
return errors.Wrapf(errdefs.ErrInvalidArgument, "container.Snapshotter must be set to cleanup rootfs snapshot") return errors.Wrapf(errdefs.ErrInvalidArgument, "container.Snapshotter must be set to cleanup rootfs snapshot")
} }
s, err := client.getSnapshotter(c.Snapshotter) s, err := client.getSnapshotter(ctx, c.Snapshotter)
if err != nil { if err != nil {
return err return err
} }
@ -178,9 +175,9 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
if err != nil { if err != nil {
return err return err
} }
setSnapshotterIfEmpty(ctx, client, c)
parent := identity.ChainID(diffIDs).String() parent := identity.ChainID(diffIDs).String()
s, err := client.getSnapshotter(c.Snapshotter) s, err := client.getSnapshotter(ctx, c.Snapshotter)
if err != nil { if err != nil {
return err return err
} }
@ -193,21 +190,6 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
} }
} }
func setSnapshotterIfEmpty(ctx context.Context, client *Client, c *containers.Container) {
if c.Snapshotter == "" {
defaultSnapshotter := DefaultSnapshotter
namespaceService := client.NamespaceService()
if ns, err := namespaces.NamespaceRequired(ctx); err == nil {
if labels, err := namespaceService.Labels(ctx, ns); err == nil {
if snapshotLabel, ok := labels[defaults.DefaultSnapshotterNSLabel]; ok {
defaultSnapshotter = snapshotLabel
}
}
}
c.Snapshotter = defaultSnapshotter
}
}
// WithContainerExtension appends extension data to the container object. // WithContainerExtension appends extension data to the container object.
// Use this to decorate the container object with additional data for the client // Use this to decorate the container object with additional data for the client
// integration. // integration.

View File

@ -50,13 +50,11 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
return err return err
} }
setSnapshotterIfEmpty(ctx, client, c)
var ( var (
parent = identity.ChainID(diffIDs).String() parent = identity.ChainID(diffIDs).String()
usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid) usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid)
) )
snapshotter, err := client.getSnapshotter(c.Snapshotter) snapshotter, err := client.getSnapshotter(ctx, c.Snapshotter)
if err != nil { if err != nil {
return err return err
} }

View File

@ -108,7 +108,7 @@ func (i *image) Config(ctx context.Context) (ocispec.Descriptor, error) {
} }
func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, error) { func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, error) {
sn, err := i.client.getSnapshotter(snapshotterName) sn, err := i.client.getSnapshotter(ctx, snapshotterName)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -149,7 +149,7 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string) error {
chain []digest.Digest chain []digest.Digest
unpacked bool unpacked bool
) )
sn, err := i.client.getSnapshotter(snapshotterName) sn, err := i.client.getSnapshotter(ctx, snapshotterName)
if err != nil { if err != nil {
return err return err
} }