Skip to content

Commit 3e7c6f6

Browse files
authored
Merge pull request #3352 from crosbymichael/sn-panic
Fix snapshotter getter in client code
2 parents cbb108e + 41e1bb8 commit 3e7c6f6

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/containerd/containerd/content"
4444
contentproxy "github.com/containerd/containerd/content/proxy"
4545
"github.com/containerd/containerd/defaults"
46+
"github.com/containerd/containerd/errdefs"
4647
"github.com/containerd/containerd/events"
4748
"github.com/containerd/containerd/images"
4849
"github.com/containerd/containerd/leases"
@@ -655,6 +656,14 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
655656
}, nil
656657
}
657658

659+
func (c *Client) getSnapshotter(name string) (snapshots.Snapshotter, error) {
660+
s := c.SnapshotService(name)
661+
if s == nil {
662+
return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name)
663+
}
664+
return s, nil
665+
}
666+
658667
// CheckRuntime returns true if the current runtime matches the expected
659668
// runtime. Providing various parts of the runtime schema will match those
660669
// parts of the expected runtime

container.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
233233
}
234234

235235
// get the rootfs from the snapshotter and add it to the request
236-
mounts, err := c.client.SnapshotService(r.Snapshotter).Mounts(ctx, r.SnapshotKey)
236+
s, err := c.client.getSnapshotter(r.Snapshotter)
237+
if err != nil {
238+
return nil, err
239+
}
240+
mounts, err := s.Mounts(ctx, r.SnapshotKey)
237241
if err != nil {
238242
return nil, err
239243
}

container_opts.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ func WithSnapshot(id string) NewContainerOpts {
120120
return func(ctx context.Context, client *Client, c *containers.Container) error {
121121
setSnapshotterIfEmpty(ctx, client, c)
122122
// check that the snapshot exists, if not, fail on creation
123-
if _, err := client.SnapshotService(c.Snapshotter).Mounts(ctx, id); err != nil {
123+
s, err := client.getSnapshotter(c.Snapshotter)
124+
if err != nil {
125+
return err
126+
}
127+
if _, err := s.Mounts(ctx, id); err != nil {
124128
return err
125129
}
126130
c.SnapshotKey = id
@@ -138,7 +142,11 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts
138142
}
139143
setSnapshotterIfEmpty(ctx, client, c)
140144
parent := identity.ChainID(diffIDs).String()
141-
if _, err := client.SnapshotService(c.Snapshotter).Prepare(ctx, id, parent, opts...); err != nil {
145+
s, err := client.getSnapshotter(c.Snapshotter)
146+
if err != nil {
147+
return err
148+
}
149+
if _, err := s.Prepare(ctx, id, parent, opts...); err != nil {
142150
return err
143151
}
144152
c.SnapshotKey = id
@@ -153,7 +161,11 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta
153161
if c.Snapshotter == "" {
154162
return errors.Wrapf(errdefs.ErrInvalidArgument, "container.Snapshotter must be set to cleanup rootfs snapshot")
155163
}
156-
return client.SnapshotService(c.Snapshotter).Remove(ctx, c.SnapshotKey)
164+
s, err := client.getSnapshotter(c.Snapshotter)
165+
if err != nil {
166+
return err
167+
}
168+
return s.Remove(ctx, c.SnapshotKey)
157169
}
158170
return nil
159171
}
@@ -168,7 +180,11 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
168180
}
169181
setSnapshotterIfEmpty(ctx, client, c)
170182
parent := identity.ChainID(diffIDs).String()
171-
if _, err := client.SnapshotService(c.Snapshotter).View(ctx, id, parent, opts...); err != nil {
183+
s, err := client.getSnapshotter(c.Snapshotter)
184+
if err != nil {
185+
return err
186+
}
187+
if _, err := s.View(ctx, id, parent, opts...); err != nil {
172188
return err
173189
}
174190
c.SnapshotKey = id

container_opts_unix.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
5353
setSnapshotterIfEmpty(ctx, client, c)
5454

5555
var (
56-
snapshotter = client.SnapshotService(c.Snapshotter)
57-
parent = identity.ChainID(diffIDs).String()
58-
usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid)
56+
parent = identity.ChainID(diffIDs).String()
57+
usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid)
5958
)
59+
snapshotter, err := client.getSnapshotter(c.Snapshotter)
60+
if err != nil {
61+
return err
62+
}
6063
if _, err := snapshotter.Stat(ctx, usernsID); err == nil {
6164
if _, err := snapshotter.Prepare(ctx, id, usernsID); err == nil {
6265
c.SnapshotKey = id

image.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ func (i *image) Config(ctx context.Context) (ocispec.Descriptor, error) {
108108
}
109109

110110
func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, error) {
111-
sn := i.client.SnapshotService(snapshotterName)
111+
sn, err := i.client.getSnapshotter(snapshotterName)
112+
if err != nil {
113+
return false, err
114+
}
112115
cs := i.client.ContentStore()
113116

114117
diffs, err := i.i.RootFS(ctx, cs, i.platform)
@@ -140,13 +143,16 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string) error {
140143
}
141144

142145
var (
143-
sn = i.client.SnapshotService(snapshotterName)
144146
a = i.client.DiffService()
145147
cs = i.client.ContentStore()
146148

147149
chain []digest.Digest
148150
unpacked bool
149151
)
152+
sn, err := i.client.getSnapshotter(snapshotterName)
153+
if err != nil {
154+
return err
155+
}
150156
for _, layer := range layers {
151157
unpacked, err = rootfs.ApplyLayer(ctx, layer, chain, sn, a)
152158
if err != nil {

0 commit comments

Comments
 (0)