diff --git a/client.go b/client.go index 3c248eb3f..2d255c0e1 100644 --- a/client.go +++ b/client.go @@ -43,6 +43,7 @@ import ( "github.com/containerd/containerd/content" contentproxy "github.com/containerd/containerd/content/proxy" "github.com/containerd/containerd/defaults" + "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events" "github.com/containerd/containerd/images" "github.com/containerd/containerd/leases" @@ -655,6 +656,14 @@ func (c *Client) Version(ctx context.Context) (Version, error) { }, nil } +func (c *Client) getSnapshotter(name string) (snapshots.Snapshotter, error) { + s := c.SnapshotService(name) + if s == nil { + return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name) + } + return s, nil +} + // CheckRuntime returns true if the current runtime matches the expected // runtime. Providing various parts of the runtime schema will match those // parts of the expected runtime diff --git a/container.go b/container.go index 9bc43604e..e060303e8 100644 --- a/container.go +++ b/container.go @@ -233,7 +233,11 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N } // get the rootfs from the snapshotter and add it to the request - mounts, err := c.client.SnapshotService(r.Snapshotter).Mounts(ctx, r.SnapshotKey) + s, err := c.client.getSnapshotter(r.Snapshotter) + if err != nil { + return nil, err + } + mounts, err := s.Mounts(ctx, r.SnapshotKey) if err != nil { return nil, err } diff --git a/container_opts.go b/container_opts.go index a65f429ae..deb078db6 100644 --- a/container_opts.go +++ b/container_opts.go @@ -120,7 +120,11 @@ 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 - if _, err := client.SnapshotService(c.Snapshotter).Mounts(ctx, id); err != nil { + s, err := client.getSnapshotter(c.Snapshotter) + if err != nil { + return err + } + if _, err := s.Mounts(ctx, id); err != nil { return err } c.SnapshotKey = id @@ -138,7 +142,11 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts } setSnapshotterIfEmpty(ctx, client, c) parent := identity.ChainID(diffIDs).String() - if _, err := client.SnapshotService(c.Snapshotter).Prepare(ctx, id, parent, opts...); err != nil { + s, err := client.getSnapshotter(c.Snapshotter) + if err != nil { + return err + } + if _, err := s.Prepare(ctx, id, parent, opts...); err != nil { return err } c.SnapshotKey = id @@ -153,7 +161,11 @@ 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") } - return client.SnapshotService(c.Snapshotter).Remove(ctx, c.SnapshotKey) + s, err := client.getSnapshotter(c.Snapshotter) + if err != nil { + return err + } + return s.Remove(ctx, c.SnapshotKey) } return nil } @@ -168,7 +180,11 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer } setSnapshotterIfEmpty(ctx, client, c) parent := identity.ChainID(diffIDs).String() - if _, err := client.SnapshotService(c.Snapshotter).View(ctx, id, parent, opts...); err != nil { + s, err := client.getSnapshotter(c.Snapshotter) + if err != nil { + return err + } + if _, err := s.View(ctx, id, parent, opts...); err != nil { return err } c.SnapshotKey = id diff --git a/container_opts_unix.go b/container_opts_unix.go index 340a91857..39f09bb55 100644 --- a/container_opts_unix.go +++ b/container_opts_unix.go @@ -53,10 +53,13 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool setSnapshotterIfEmpty(ctx, client, c) var ( - snapshotter = client.SnapshotService(c.Snapshotter) - parent = identity.ChainID(diffIDs).String() - usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid) + parent = identity.ChainID(diffIDs).String() + usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid) ) + snapshotter, err := client.getSnapshotter(c.Snapshotter) + if err != nil { + return err + } if _, err := snapshotter.Stat(ctx, usernsID); err == nil { if _, err := snapshotter.Prepare(ctx, id, usernsID); err == nil { c.SnapshotKey = id diff --git a/image.go b/image.go index 14bfea91b..c13d3869a 100644 --- a/image.go +++ b/image.go @@ -108,7 +108,10 @@ func (i *image) Config(ctx context.Context) (ocispec.Descriptor, error) { } func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, error) { - sn := i.client.SnapshotService(snapshotterName) + sn, err := i.client.getSnapshotter(snapshotterName) + if err != nil { + return false, err + } cs := i.client.ContentStore() diffs, err := i.i.RootFS(ctx, cs, i.platform) @@ -140,13 +143,16 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string) error { } var ( - sn = i.client.SnapshotService(snapshotterName) a = i.client.DiffService() cs = i.client.ContentStore() chain []digest.Digest unpacked bool ) + sn, err := i.client.getSnapshotter(snapshotterName) + if err != nil { + return err + } for _, layer := range layers { unpacked, err = rootfs.ApplyLayer(ctx, layer, chain, sn, a) if err != nil {