Skip to content

Commit

Permalink
Merge pull request #3072 from crosbymichael/v2opts
Browse files Browse the repository at this point in the history
Fix runtime v2 option handling
  • Loading branch information
estesp committed Mar 6, 2019
2 parents bfbd1d0 + aaae811 commit 04b2e5b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 31 deletions.
4 changes: 3 additions & 1 deletion container.go
Expand Up @@ -229,7 +229,9 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
})
}
}
var info TaskInfo
info := TaskInfo{
runtime: r.Runtime.Name,
}
for _, o := range opts {
if err := o(ctx, c.client, &info); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions container_checkpoint_test.go
Expand Up @@ -461,7 +461,7 @@ func TestCRWithImagePath(t *testing.T) {
defer os.RemoveAll(crDir)
imagePath := filepath.Join(crDir, "cr")
// checkpoint task
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(client.runtime, imagePath)); err != nil {
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(imagePath)); err != nil {
t.Fatal(err)
}

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

ntask, err := ncontainer.NewTask(ctx, empty(), WithRestoreImagePath(client.runtime, imagePath))
ntask, err := ncontainer.NewTask(ctx, empty(), WithRestoreImagePath(imagePath))
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 21 additions & 6 deletions task.go
Expand Up @@ -43,7 +43,7 @@ import (
google_protobuf "github.com/gogo/protobuf/types"
digest "github.com/opencontainers/go-digest"
is "github.com/opencontainers/image-spec/specs-go"
"github.com/opencontainers/image-spec/specs-go/v1"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -117,6 +117,13 @@ type CheckpointTaskInfo struct {
ParentCheckpoint digest.Digest
// Options hold runtime specific settings for checkpointing a task
Options interface{}

runtime string
}

// Runtime name for the container
func (i *CheckpointTaskInfo) Runtime() string {
return i.runtime
}

// CheckpointTaskOpts allows the caller to set checkpoint options
Expand All @@ -131,6 +138,12 @@ type TaskInfo struct {
RootFS []mount.Mount
// Options hold runtime specific settings for task creation
Options interface{}
runtime string
}

// Runtime name for the container
func (i *TaskInfo) Runtime() string {
return i.runtime
}

// Task is the executable object within containerd
Expand Down Expand Up @@ -401,11 +414,17 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
return nil, err
}
defer done(ctx)
cr, err := t.client.ContainerService().Get(ctx, t.id)
if err != nil {
return nil, err
}

request := &tasks.CheckpointTaskRequest{
ContainerID: t.id,
}
var i CheckpointTaskInfo
i := CheckpointTaskInfo{
runtime: cr.Runtime.Name,
}
for _, o := range opts {
if err := o(&i); err != nil {
return nil, err
Expand All @@ -428,10 +447,6 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
return nil, err
}
defer t.Resume(ctx)
cr, err := t.client.ContainerService().Get(ctx, t.id)
if err != nil {
return nil, err
}
index := v1.Index{
Versioned: is.Versioned{
SchemaVersion: 2,
Expand Down
8 changes: 4 additions & 4 deletions task_opts.go
Expand Up @@ -92,9 +92,9 @@ func WithCheckpointName(name string) CheckpointTaskOpts {
}

// WithCheckpointImagePath sets image path for checkpoint option
func WithCheckpointImagePath(rt, path string) CheckpointTaskOpts {
func WithCheckpointImagePath(path string) CheckpointTaskOpts {
return func(r *CheckpointTaskInfo) error {
if CheckRuntime(rt, "io.containerd.runc") {
if CheckRuntime(r.Runtime(), "io.containerd.runc") {
if r.Options == nil {
r.Options = &options.CheckpointOptions{}
}
Expand All @@ -118,9 +118,9 @@ func WithCheckpointImagePath(rt, path string) CheckpointTaskOpts {
}

// WithRestoreImagePath sets image path for create option
func WithRestoreImagePath(rt, path string) NewTaskOpts {
func WithRestoreImagePath(path string) NewTaskOpts {
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
if CheckRuntime(rt, "io.containerd.runc") {
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
if ti.Options == nil {
ti.Options = &options.Options{}
}
Expand Down
58 changes: 40 additions & 18 deletions task_opts_unix.go
Expand Up @@ -22,36 +22,58 @@ import (
"context"

"github.com/containerd/containerd/runtime/linux/runctypes"
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/pkg/errors"
)

// WithNoNewKeyring causes tasks not to be created with a new keyring for secret storage.
// There is an upper limit on the number of keyrings in a linux system
func WithNoNewKeyring(ctx context.Context, c *Client, ti *TaskInfo) error {
if ti.Options == nil {
ti.Options = &runctypes.CreateOptions{}
}
opts, ok := ti.Options.(*runctypes.CreateOptions)
if !ok {
return errors.New("could not cast TaskInfo Options to CreateOptions")
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
if ti.Options == nil {
ti.Options = &options.Options{}
}
opts, ok := ti.Options.(*options.Options)
if !ok {
return errors.New("invalid v2 shim create options format")
}
opts.NoNewKeyring = true
} else {
if ti.Options == nil {
ti.Options = &runctypes.CreateOptions{}
}
opts, ok := ti.Options.(*runctypes.CreateOptions)
if !ok {
return errors.New("could not cast TaskInfo Options to CreateOptions")
}
opts.NoNewKeyring = true
}

opts.NoNewKeyring = true
return nil
}

// WithNoPivotRoot instructs the runtime not to you pivot_root
func WithNoPivotRoot(_ context.Context, _ *Client, info *TaskInfo) error {
if info.Options == nil {
info.Options = &runctypes.CreateOptions{
NoPivotRoot: true,
func WithNoPivotRoot(_ context.Context, _ *Client, ti *TaskInfo) error {
if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
if ti.Options == nil {
ti.Options = &options.Options{}
}
return nil
}
opts, ok := info.Options.(*runctypes.CreateOptions)
if !ok {
return errors.New("invalid options type, expected runctypes.CreateOptions")
opts, ok := ti.Options.(*options.Options)
if !ok {
return errors.New("invalid v2 shim create options format")
}
opts.NoPivotRoot = true
} else {
if ti.Options == nil {
ti.Options = &runctypes.CreateOptions{
NoPivotRoot: true,
}
return nil
}
opts, ok := ti.Options.(*runctypes.CreateOptions)
if !ok {
return errors.New("invalid options type, expected runctypes.CreateOptions")
}
opts.NoPivotRoot = true
}
opts.NoPivotRoot = true
return nil
}

0 comments on commit 04b2e5b

Please sign in to comment.