Skip to content

Commit 04b2e5b

Browse files
authored
Merge pull request #3072 from crosbymichael/v2opts
Fix runtime v2 option handling
2 parents bfbd1d0 + aaae811 commit 04b2e5b

5 files changed

Lines changed: 70 additions & 31 deletions

File tree

container.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
229229
})
230230
}
231231
}
232-
var info TaskInfo
232+
info := TaskInfo{
233+
runtime: r.Runtime.Name,
234+
}
233235
for _, o := range opts {
234236
if err := o(ctx, c.client, &info); err != nil {
235237
return nil, err

container_checkpoint_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func TestCRWithImagePath(t *testing.T) {
461461
defer os.RemoveAll(crDir)
462462
imagePath := filepath.Join(crDir, "cr")
463463
// checkpoint task
464-
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(client.runtime, imagePath)); err != nil {
464+
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(imagePath)); err != nil {
465465
t.Fatal(err)
466466
}
467467

@@ -485,7 +485,7 @@ func TestCRWithImagePath(t *testing.T) {
485485
}
486486
defer ncontainer.Delete(ctx, WithSnapshotCleanup)
487487

488-
ntask, err := ncontainer.NewTask(ctx, empty(), WithRestoreImagePath(client.runtime, imagePath))
488+
ntask, err := ncontainer.NewTask(ctx, empty(), WithRestoreImagePath(imagePath))
489489
if err != nil {
490490
t.Fatal(err)
491491
}

task.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
google_protobuf "github.com/gogo/protobuf/types"
4444
digest "github.com/opencontainers/go-digest"
4545
is "github.com/opencontainers/image-spec/specs-go"
46-
"github.com/opencontainers/image-spec/specs-go/v1"
46+
v1 "github.com/opencontainers/image-spec/specs-go/v1"
4747
specs "github.com/opencontainers/runtime-spec/specs-go"
4848
"github.com/pkg/errors"
4949
)
@@ -117,6 +117,13 @@ type CheckpointTaskInfo struct {
117117
ParentCheckpoint digest.Digest
118118
// Options hold runtime specific settings for checkpointing a task
119119
Options interface{}
120+
121+
runtime string
122+
}
123+
124+
// Runtime name for the container
125+
func (i *CheckpointTaskInfo) Runtime() string {
126+
return i.runtime
120127
}
121128

122129
// CheckpointTaskOpts allows the caller to set checkpoint options
@@ -131,6 +138,12 @@ type TaskInfo struct {
131138
RootFS []mount.Mount
132139
// Options hold runtime specific settings for task creation
133140
Options interface{}
141+
runtime string
142+
}
143+
144+
// Runtime name for the container
145+
func (i *TaskInfo) Runtime() string {
146+
return i.runtime
134147
}
135148

136149
// Task is the executable object within containerd
@@ -401,11 +414,17 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
401414
return nil, err
402415
}
403416
defer done(ctx)
417+
cr, err := t.client.ContainerService().Get(ctx, t.id)
418+
if err != nil {
419+
return nil, err
420+
}
404421

405422
request := &tasks.CheckpointTaskRequest{
406423
ContainerID: t.id,
407424
}
408-
var i CheckpointTaskInfo
425+
i := CheckpointTaskInfo{
426+
runtime: cr.Runtime.Name,
427+
}
409428
for _, o := range opts {
410429
if err := o(&i); err != nil {
411430
return nil, err
@@ -428,10 +447,6 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
428447
return nil, err
429448
}
430449
defer t.Resume(ctx)
431-
cr, err := t.client.ContainerService().Get(ctx, t.id)
432-
if err != nil {
433-
return nil, err
434-
}
435450
index := v1.Index{
436451
Versioned: is.Versioned{
437452
SchemaVersion: 2,

task_opts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func WithCheckpointName(name string) CheckpointTaskOpts {
9292
}
9393

9494
// WithCheckpointImagePath sets image path for checkpoint option
95-
func WithCheckpointImagePath(rt, path string) CheckpointTaskOpts {
95+
func WithCheckpointImagePath(path string) CheckpointTaskOpts {
9696
return func(r *CheckpointTaskInfo) error {
97-
if CheckRuntime(rt, "io.containerd.runc") {
97+
if CheckRuntime(r.Runtime(), "io.containerd.runc") {
9898
if r.Options == nil {
9999
r.Options = &options.CheckpointOptions{}
100100
}
@@ -118,9 +118,9 @@ func WithCheckpointImagePath(rt, path string) CheckpointTaskOpts {
118118
}
119119

120120
// WithRestoreImagePath sets image path for create option
121-
func WithRestoreImagePath(rt, path string) NewTaskOpts {
121+
func WithRestoreImagePath(path string) NewTaskOpts {
122122
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
123-
if CheckRuntime(rt, "io.containerd.runc") {
123+
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
124124
if ti.Options == nil {
125125
ti.Options = &options.Options{}
126126
}

task_opts_unix.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,58 @@ import (
2222
"context"
2323

2424
"github.com/containerd/containerd/runtime/linux/runctypes"
25+
"github.com/containerd/containerd/runtime/v2/runc/options"
2526
"github.com/pkg/errors"
2627
)
2728

2829
// WithNoNewKeyring causes tasks not to be created with a new keyring for secret storage.
2930
// There is an upper limit on the number of keyrings in a linux system
3031
func WithNoNewKeyring(ctx context.Context, c *Client, ti *TaskInfo) error {
31-
if ti.Options == nil {
32-
ti.Options = &runctypes.CreateOptions{}
33-
}
34-
opts, ok := ti.Options.(*runctypes.CreateOptions)
35-
if !ok {
36-
return errors.New("could not cast TaskInfo Options to CreateOptions")
32+
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
33+
if ti.Options == nil {
34+
ti.Options = &options.Options{}
35+
}
36+
opts, ok := ti.Options.(*options.Options)
37+
if !ok {
38+
return errors.New("invalid v2 shim create options format")
39+
}
40+
opts.NoNewKeyring = true
41+
} else {
42+
if ti.Options == nil {
43+
ti.Options = &runctypes.CreateOptions{}
44+
}
45+
opts, ok := ti.Options.(*runctypes.CreateOptions)
46+
if !ok {
47+
return errors.New("could not cast TaskInfo Options to CreateOptions")
48+
}
49+
opts.NoNewKeyring = true
3750
}
38-
39-
opts.NoNewKeyring = true
4051
return nil
4152
}
4253

4354
// WithNoPivotRoot instructs the runtime not to you pivot_root
44-
func WithNoPivotRoot(_ context.Context, _ *Client, info *TaskInfo) error {
45-
if info.Options == nil {
46-
info.Options = &runctypes.CreateOptions{
47-
NoPivotRoot: true,
55+
func WithNoPivotRoot(_ context.Context, _ *Client, ti *TaskInfo) error {
56+
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
57+
if ti.Options == nil {
58+
ti.Options = &options.Options{}
4859
}
49-
return nil
50-
}
51-
opts, ok := info.Options.(*runctypes.CreateOptions)
52-
if !ok {
53-
return errors.New("invalid options type, expected runctypes.CreateOptions")
60+
opts, ok := ti.Options.(*options.Options)
61+
if !ok {
62+
return errors.New("invalid v2 shim create options format")
63+
}
64+
opts.NoPivotRoot = true
65+
} else {
66+
if ti.Options == nil {
67+
ti.Options = &runctypes.CreateOptions{
68+
NoPivotRoot: true,
69+
}
70+
return nil
71+
}
72+
opts, ok := ti.Options.(*runctypes.CreateOptions)
73+
if !ok {
74+
return errors.New("invalid options type, expected runctypes.CreateOptions")
75+
}
76+
opts.NoPivotRoot = true
5477
}
55-
opts.NoPivotRoot = true
5678
return nil
5779
}

0 commit comments

Comments
 (0)