X Tutup
Skip to content

Commit 82d0208

Browse files
committed
Implement options for runtime specific settings
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent eedcbc6 commit 82d0208

File tree

18 files changed

+1701
-467
lines changed

18 files changed

+1701
-467
lines changed

api/services/tasks/v1/tasks.pb.go

Lines changed: 159 additions & 217 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/services/tasks/v1/tasks.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ message CreateTaskRequest {
7272
bool terminal = 7;
7373

7474
containerd.types.Descriptor checkpoint = 8;
75+
76+
google.protobuf.Any options = 9;
7577
}
7678

7779
message CreateTaskResponse {
@@ -184,7 +186,7 @@ message ListPidsResponse{
184186
message CheckpointTaskRequest {
185187
string container_id = 1;
186188
string parent_checkpoint = 2 [(gogoproto.customtype) = "github.com/opencontainers/go-digest.Digest", (gogoproto.nullable) = false];
187-
map<string, string> options = 3;
189+
google.protobuf.Any options = 3;
188190
}
189191

190192
message CheckpointTaskResponse {

checkpoint_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package containerd
22

33
import (
4+
"path/filepath"
45
"syscall"
56
"testing"
7+
8+
tasks "github.com/containerd/containerd/api/services/tasks/v1"
9+
"github.com/containerd/containerd/linux/runcopts"
10+
"github.com/gogo/protobuf/proto"
11+
protobuf "github.com/gogo/protobuf/types"
612
)
713

814
func TestCheckpointRestore(t *testing.T) {
@@ -200,3 +206,105 @@ func TestCheckpointRestoreNewContainer(t *testing.T) {
200206
}
201207
<-statusC
202208
}
209+
210+
func TestCheckpointLeaveRunning(t *testing.T) {
211+
if testing.Short() {
212+
t.Skip()
213+
}
214+
if !supportsCriu {
215+
t.Skip("system does not have criu installed")
216+
}
217+
client, err := New(address)
218+
if err != nil {
219+
t.Fatal(err)
220+
}
221+
defer client.Close()
222+
223+
var (
224+
ctx, cancel = testContext()
225+
id = t.Name()
226+
)
227+
defer cancel()
228+
229+
image, err := client.GetImage(ctx, testImage)
230+
if err != nil {
231+
t.Error(err)
232+
return
233+
}
234+
spec, err := GenerateSpec(WithImageConfig(ctx, image), WithProcessArgs("sleep", "100"))
235+
if err != nil {
236+
t.Error(err)
237+
return
238+
}
239+
container, err := client.NewContainer(ctx, id, WithSpec(spec), WithNewRootFS(id, image))
240+
if err != nil {
241+
t.Error(err)
242+
return
243+
}
244+
defer container.Delete(ctx, WithRootFSDeletion)
245+
246+
task, err := container.NewTask(ctx, empty())
247+
if err != nil {
248+
t.Error(err)
249+
return
250+
}
251+
defer task.Delete(ctx)
252+
253+
statusC := make(chan uint32, 1)
254+
go func() {
255+
status, err := task.Wait(ctx)
256+
if err != nil {
257+
t.Error(err)
258+
}
259+
statusC <- status
260+
}()
261+
262+
if err := task.Start(ctx); err != nil {
263+
t.Error(err)
264+
return
265+
}
266+
267+
if _, err := task.Checkpoint(ctx); err != nil {
268+
t.Error(err)
269+
return
270+
}
271+
272+
status, err := task.Status(ctx)
273+
if err != nil {
274+
t.Error(err)
275+
return
276+
}
277+
if status != Running {
278+
t.Errorf("expected status %q but received %q", Running, status)
279+
return
280+
}
281+
282+
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
283+
t.Error(err)
284+
return
285+
}
286+
287+
<-statusC
288+
}
289+
290+
func WithExit(r *tasks.CheckpointTaskRequest) error {
291+
a, err := marshal(&runcopts.CheckpointOptions{
292+
Exit: true,
293+
}, "CheckpointOptions")
294+
if err != nil {
295+
return err
296+
}
297+
r.Options = a
298+
return nil
299+
}
300+
301+
func marshal(m proto.Message, name string) (*protobuf.Any, error) {
302+
data, err := proto.Marshal(m)
303+
if err != nil {
304+
return nil, err
305+
}
306+
return &protobuf.Any{
307+
TypeUrl: filepath.Join(runcopts.URIBase, name),
308+
Value: data,
309+
}, nil
310+
}

client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,13 @@ func WithNewReadonlyRootFS(id string, i Image) NewContainerOpts {
195195
}
196196
}
197197

198-
func WithRuntime(name string) NewContainerOpts {
198+
// WithRuntime allows a user to specify the runtime name and additional options that should
199+
// be used to create tasks for the container
200+
func WithRuntime(name string, options map[string]string) NewContainerOpts {
199201
return func(ctx context.Context, client *Client, c *containers.Container) error {
200202
c.Runtime = &containers.Container_Runtime{
201-
Name: name,
203+
Name: name,
204+
Options: options,
202205
}
203206
return nil
204207
}

cmd/ctr/checkpoint.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package main
22

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/containerd/containerd"
8+
tasks "github.com/containerd/containerd/api/services/tasks/v1"
9+
"github.com/containerd/containerd/linux/runcopts"
10+
"github.com/gogo/protobuf/proto"
11+
protobuf "github.com/gogo/protobuf/types"
712
"github.com/pkg/errors"
813
"github.com/urfave/cli"
914
)
@@ -42,7 +47,7 @@ var checkpointCommand = cli.Command{
4247
}
4348
var opts []containerd.CheckpointOpts
4449
if context.Bool("exit") {
45-
opts = append(opts, containerd.WithExit)
50+
opts = append(opts, WithExit)
4651
}
4752
checkpoint, err := task.Checkpoint(ctx, opts...)
4853
if err != nil {
@@ -52,3 +57,25 @@ var checkpointCommand = cli.Command{
5257
return nil
5358
},
5459
}
60+
61+
func WithExit(r *tasks.CheckpointTaskRequest) error {
62+
a, err := marshal(&runcopts.CheckpointOptions{
63+
Exit: true,
64+
}, "CheckpointOptions")
65+
if err != nil {
66+
return err
67+
}
68+
r.Options = a
69+
return nil
70+
}
71+
72+
func marshal(m proto.Message, name string) (*protobuf.Any, error) {
73+
data, err := proto.Marshal(m)
74+
if err != nil {
75+
return nil, err
76+
}
77+
return &protobuf.Any{
78+
TypeUrl: filepath.Join(runcopts.URIBase, name),
79+
Value: data,
80+
}, nil
81+
}

linux/runcopts/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package runcopts
2+
3+
const URIBase = "containerd.io/linux/runc"

0 commit comments

Comments
 (0)
X Tutup