Skip to content

Commit 18d9e43

Browse files
Merge pull request #2623 from yanxuean/move-task-opts
move WithXXXX to task_opts.go
2 parents c48cafe + 3c8692a commit 18d9e43

4 files changed

Lines changed: 67 additions & 61 deletions

File tree

container_opts_unix.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,21 @@ package containerd
2020

2121
import (
2222
"context"
23-
"encoding/json"
2423
"fmt"
2524
"os"
2625
"path/filepath"
2726
"syscall"
2827

29-
"github.com/containerd/containerd/api/types"
3028
"github.com/containerd/containerd/containers"
3129
"github.com/containerd/containerd/content"
3230
"github.com/containerd/containerd/errdefs"
3331
"github.com/containerd/containerd/images"
3432
"github.com/containerd/containerd/mount"
3533
"github.com/containerd/containerd/platforms"
36-
"github.com/containerd/containerd/runtime/linux/runctypes"
3734
"github.com/gogo/protobuf/proto"
3835
protobuf "github.com/gogo/protobuf/types"
3936
"github.com/opencontainers/image-spec/identity"
4037
"github.com/opencontainers/image-spec/specs-go/v1"
41-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
4238
"github.com/pkg/errors"
4339
)
4440

@@ -105,44 +101,6 @@ func WithCheckpoint(im Image, snapshotKey string) NewContainerOpts {
105101
}
106102
}
107103

108-
// WithTaskCheckpoint allows a task to be created with live runtime and memory data from a
109-
// previous checkpoint. Additional software such as CRIU may be required to
110-
// restore a task from a checkpoint
111-
func WithTaskCheckpoint(im Image) NewTaskOpts {
112-
return func(ctx context.Context, c *Client, info *TaskInfo) error {
113-
desc := im.Target()
114-
id := desc.Digest
115-
index, err := decodeIndex(ctx, c.ContentStore(), desc)
116-
if err != nil {
117-
return err
118-
}
119-
for _, m := range index.Manifests {
120-
if m.MediaType == images.MediaTypeContainerd1Checkpoint {
121-
info.Checkpoint = &types.Descriptor{
122-
MediaType: m.MediaType,
123-
Size_: m.Size,
124-
Digest: m.Digest,
125-
}
126-
return nil
127-
}
128-
}
129-
return fmt.Errorf("checkpoint not found in index %s", id)
130-
}
131-
}
132-
133-
func decodeIndex(ctx context.Context, store content.Provider, desc ocispec.Descriptor) (*v1.Index, error) {
134-
var index v1.Index
135-
p, err := content.ReadBlob(ctx, store, desc)
136-
if err != nil {
137-
return nil, err
138-
}
139-
if err := json.Unmarshal(p, &index); err != nil {
140-
return nil, err
141-
}
142-
143-
return &index, nil
144-
}
145-
146104
// WithRemappedSnapshot creates a new snapshot and remaps the uid/gid for the
147105
// filesystem to be used by a container with user namespaces
148106
func WithRemappedSnapshot(id string, i Image, uid, gid uint32) NewContainerOpts {
@@ -221,19 +179,3 @@ func incrementFS(root string, uidInc, gidInc uint32) filepath.WalkFunc {
221179
return os.Lchown(path, u, g)
222180
}
223181
}
224-
225-
// WithNoPivotRoot instructs the runtime not to you pivot_root
226-
func WithNoPivotRoot(_ context.Context, _ *Client, info *TaskInfo) error {
227-
if info.Options == nil {
228-
info.Options = &runctypes.CreateOptions{
229-
NoPivotRoot: true,
230-
}
231-
return nil
232-
}
233-
copts, ok := info.Options.(*runctypes.CreateOptions)
234-
if !ok {
235-
return errors.New("invalid options type, expected runctypes.CreateOptions")
236-
}
237-
copts.NoPivotRoot = true
238-
return nil
239-
}

task_opts.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ package containerd
1818

1919
import (
2020
"context"
21-
"errors"
21+
"encoding/json"
22+
"fmt"
2223
"syscall"
2324

25+
"github.com/containerd/containerd/api/types"
26+
"github.com/containerd/containerd/content"
2427
"github.com/containerd/containerd/errdefs"
28+
"github.com/containerd/containerd/images"
2529
"github.com/containerd/containerd/mount"
26-
specs "github.com/opencontainers/runtime-spec/specs-go"
30+
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
31+
"github.com/opencontainers/runtime-spec/specs-go"
32+
"github.com/pkg/errors"
2733
)
2834

2935
// NewTaskOpts allows the caller to set options on a new task
@@ -37,6 +43,44 @@ func WithRootFS(mounts []mount.Mount) NewTaskOpts {
3743
}
3844
}
3945

46+
// WithTaskCheckpoint allows a task to be created with live runtime and memory data from a
47+
// previous checkpoint. Additional software such as CRIU may be required to
48+
// restore a task from a checkpoint
49+
func WithTaskCheckpoint(im Image) NewTaskOpts {
50+
return func(ctx context.Context, c *Client, info *TaskInfo) error {
51+
desc := im.Target()
52+
id := desc.Digest
53+
index, err := decodeIndex(ctx, c.ContentStore(), desc)
54+
if err != nil {
55+
return err
56+
}
57+
for _, m := range index.Manifests {
58+
if m.MediaType == images.MediaTypeContainerd1Checkpoint {
59+
info.Checkpoint = &types.Descriptor{
60+
MediaType: m.MediaType,
61+
Size_: m.Size,
62+
Digest: m.Digest,
63+
}
64+
return nil
65+
}
66+
}
67+
return fmt.Errorf("checkpoint not found in index %s", id)
68+
}
69+
}
70+
71+
func decodeIndex(ctx context.Context, store content.Provider, desc imagespec.Descriptor) (*imagespec.Index, error) {
72+
var index imagespec.Index
73+
p, err := content.ReadBlob(ctx, store, desc)
74+
if err != nil {
75+
return nil, err
76+
}
77+
if err := json.Unmarshal(p, &index); err != nil {
78+
return nil, err
79+
}
80+
81+
return &index, nil
82+
}
83+
4084
// WithCheckpointName sets the image name for the checkpoint
4185
func WithCheckpointName(name string) CheckpointTaskOpts {
4286
return func(r *CheckpointTaskInfo) error {
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !windows
2+
13
/*
24
Copyright The containerd Authors.
35
@@ -18,9 +20,9 @@ package containerd
1820

1921
import (
2022
"context"
21-
"errors"
2223

2324
"github.com/containerd/containerd/runtime/linux/runctypes"
25+
"github.com/pkg/errors"
2426
)
2527

2628
// WithNoNewKeyring causes tasks not to be created with a new keyring for secret storage.
@@ -37,3 +39,19 @@ func WithNoNewKeyring(ctx context.Context, c *Client, ti *TaskInfo) error {
3739
opts.NoNewKeyring = true
3840
return nil
3941
}
42+
43+
// 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,
48+
}
49+
return nil
50+
}
51+
opts, ok := info.Options.(*runctypes.CreateOptions)
52+
if !ok {
53+
return errors.New("invalid options type, expected runctypes.CreateOptions")
54+
}
55+
opts.NoPivotRoot = true
56+
return nil
57+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !windows
2+
13
/*
24
Copyright The containerd Authors.
35

0 commit comments

Comments
 (0)