diff --git a/client.go b/client.go index 0317c8afc..82505889e 100644 --- a/client.go +++ b/client.go @@ -332,7 +332,6 @@ func defaultRemoteContext() *RemoteContext { Resolver: docker.NewResolver(docker.ResolverOptions{ Client: http.DefaultClient, }), - Snapshotter: DefaultSnapshotter, } } @@ -672,7 +671,13 @@ func (c *Client) Version(ctx context.Context) (Version, error) { }, 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) if s == nil { return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name) diff --git a/cmd/ctr/commands/commands.go b/cmd/ctr/commands/commands.go index 6d9fb5488..234971d35 100644 --- a/cmd/ctr/commands/commands.go +++ b/cmd/ctr/commands/commands.go @@ -24,7 +24,6 @@ import ( "runtime" "strings" - "github.com/containerd/containerd" "github.com/urfave/cli" ) @@ -34,7 +33,6 @@ var ( cli.StringFlag{ Name: "snapshotter", Usage: "snapshotter name. Empty value stands for the default value.", - Value: containerd.DefaultSnapshotter, EnvVar: "CONTAINERD_SNAPSHOTTER", }, } diff --git a/container.go b/container.go index e060303e8..46d51ecd9 100644 --- a/container.go +++ b/container.go @@ -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 - s, err := c.client.getSnapshotter(r.Snapshotter) + s, err := c.client.getSnapshotter(ctx, r.Snapshotter) if err != nil { return nil, err } diff --git a/container_opts.go b/container_opts.go index deb078db6..c8a0933b7 100644 --- a/container_opts.go +++ b/container_opts.go @@ -20,9 +20,7 @@ import ( "context" "github.com/containerd/containerd/containers" - "github.com/containerd/containerd/defaults" "github.com/containerd/containerd/errdefs" - "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/oci" "github.com/containerd/containerd/platforms" "github.com/containerd/containerd/snapshots" @@ -118,9 +116,8 @@ func WithSnapshotter(name string) NewContainerOpts { // WithSnapshot uses an existing root filesystem for the container func WithSnapshot(id string) NewContainerOpts { 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 - s, err := client.getSnapshotter(c.Snapshotter) + s, err := client.getSnapshotter(ctx, c.Snapshotter) if err != nil { return err } @@ -140,9 +137,9 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts if err != nil { return err } - setSnapshotterIfEmpty(ctx, client, c) + parent := identity.ChainID(diffIDs).String() - s, err := client.getSnapshotter(c.Snapshotter) + s, err := client.getSnapshotter(ctx, c.Snapshotter) if err != nil { return err } @@ -161,7 +158,7 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta if c.Snapshotter == "" { 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 { return err } @@ -178,9 +175,9 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer if err != nil { return err } - setSnapshotterIfEmpty(ctx, client, c) + parent := identity.ChainID(diffIDs).String() - s, err := client.getSnapshotter(c.Snapshotter) + s, err := client.getSnapshotter(ctx, c.Snapshotter) if err != nil { 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. // Use this to decorate the container object with additional data for the client // integration. diff --git a/container_opts_unix.go b/container_opts_unix.go index 39f09bb55..c8b6247b5 100644 --- a/container_opts_unix.go +++ b/container_opts_unix.go @@ -50,13 +50,11 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool return err } - setSnapshotterIfEmpty(ctx, client, c) - var ( parent = identity.ChainID(diffIDs).String() 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 { return err } diff --git a/image.go b/image.go index c13d3869a..3c820840c 100644 --- a/image.go +++ b/image.go @@ -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) { - sn, err := i.client.getSnapshotter(snapshotterName) + sn, err := i.client.getSnapshotter(ctx, snapshotterName) if err != nil { return false, err } @@ -149,7 +149,7 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string) error { chain []digest.Digest unpacked bool ) - sn, err := i.client.getSnapshotter(snapshotterName) + sn, err := i.client.getSnapshotter(ctx, snapshotterName) if err != nil { return err }