Skip to content

Commit a183b2d

Browse files
Iceberk8s-infra-cherrypick-robot
authored andcommitted
update taskOptions based on runtimeOptions when creating a task
Signed-off-by: Iceber Gu <[email protected]>
1 parent c146996 commit a183b2d

4 files changed

Lines changed: 54 additions & 52 deletions

File tree

client/container.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
279279
}
280280
}
281281
info := TaskInfo{
282-
runtime: r.Runtime.Name,
282+
runtime: r.Runtime.Name,
283+
runtimeOptions: r.Runtime.Options,
283284
}
284285
for _, o := range opts {
285286
if err := o(ctx, c.client, &info); err != nil {

client/task.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,41 @@ type TaskInfo struct {
146146

147147
// runtime is the runtime name for the container, and cannot be changed.
148148
runtime string
149+
150+
// runtimeOptions is the runtime options for the container, and when task options are set,
151+
// they will be based on the runtimeOptions.
152+
// https://github.com/containerd/containerd/issues/11568
153+
runtimeOptions typeurl.Any
149154
}
150155

151156
// Runtime name for the container
152157
func (i *TaskInfo) Runtime() string {
153158
return i.runtime
154159
}
155160

161+
// getRuncOptions returns a reference to the runtime options for use by the task.
162+
// If the set of options is not set by the opts passed into the NewTask creation
163+
// this function first attempts to initialize the runtime options with a copy of the runtimeOptions,
164+
// otherwise an empty set of options is assigned and returned
165+
func (i *TaskInfo) getRuncOptions() (*options.Options, error) {
166+
if i.Options != nil {
167+
opts, ok := i.Options.(*options.Options)
168+
if !ok {
169+
return nil, errors.New("invalid runtime v2 options format")
170+
}
171+
return opts, nil
172+
}
173+
174+
opts := &options.Options{}
175+
if i.runtimeOptions != nil && i.runtimeOptions.GetValue() != nil {
176+
if err := typeurl.UnmarshalTo(i.runtimeOptions, opts); err != nil {
177+
return nil, fmt.Errorf("failed to get runtime v2 options: %w", err)
178+
}
179+
}
180+
i.Options = opts
181+
return opts, nil
182+
}
183+
156184
// Task is the executable object within containerd
157185
type Task interface {
158186
Process

client/task_opts.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ func WithRuntimePath(absRuntimePath string) NewTaskOpts {
5454
// usually it is served inside a sandbox, and we can get it from sandbox status.
5555
func WithTaskAPIEndpoint(address string, version uint32) NewTaskOpts {
5656
return func(ctx context.Context, client *Client, info *TaskInfo) error {
57-
if info.Options == nil {
58-
info.Options = &options.Options{}
59-
}
60-
opts, ok := info.Options.(*options.Options)
61-
if !ok {
62-
return errors.New("invalid runtime v2 options format")
57+
opts, err := info.getRuncOptions()
58+
if err != nil {
59+
return err
6360
}
6461
opts.TaskApiAddress = address
6562
opts.TaskApiVersion = version
@@ -119,12 +116,9 @@ func WithCheckpointImagePath(path string) CheckpointTaskOpts {
119116
// WithRestoreImagePath sets image path for create option
120117
func WithRestoreImagePath(path string) NewTaskOpts {
121118
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
122-
if ti.Options == nil {
123-
ti.Options = &options.Options{}
124-
}
125-
opts, ok := ti.Options.(*options.Options)
126-
if !ok {
127-
return errors.New("invalid runtime v2 options format")
119+
opts, err := ti.getRuncOptions()
120+
if err != nil {
121+
return err
128122
}
129123
opts.CriuImagePath = path
130124
return nil
@@ -134,12 +128,9 @@ func WithRestoreImagePath(path string) NewTaskOpts {
134128
// WithRestoreWorkPath sets criu work path for create option
135129
func WithRestoreWorkPath(path string) NewTaskOpts {
136130
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
137-
if ti.Options == nil {
138-
ti.Options = &options.Options{}
139-
}
140-
opts, ok := ti.Options.(*options.Options)
141-
if !ok {
142-
return errors.New("invalid runtime v2 options format")
131+
opts, err := ti.getRuncOptions()
132+
if err != nil {
133+
return err
143134
}
144135
opts.CriuWorkPath = path
145136
return nil

client/task_opts_unix.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,24 @@ package client
2020

2121
import (
2222
"context"
23-
"errors"
24-
25-
"github.com/containerd/containerd/api/types/runc/options"
2623
)
2724

2825
// WithNoNewKeyring causes tasks not to be created with a new keyring for secret storage.
2926
// There is an upper limit on the number of keyrings in a linux system
3027
func WithNoNewKeyring(ctx context.Context, c *Client, ti *TaskInfo) error {
31-
if ti.Options == nil {
32-
ti.Options = &options.Options{}
33-
}
34-
opts, ok := ti.Options.(*options.Options)
35-
if !ok {
36-
return errors.New("invalid v2 shim create options format")
28+
opts, err := ti.getRuncOptions()
29+
if err != nil {
30+
return err
3731
}
3832
opts.NoNewKeyring = true
3933
return nil
4034
}
4135

4236
// WithNoPivotRoot instructs the runtime not to you pivot_root
4337
func WithNoPivotRoot(_ context.Context, _ *Client, ti *TaskInfo) error {
44-
if ti.Options == nil {
45-
ti.Options = &options.Options{}
46-
}
47-
opts, ok := ti.Options.(*options.Options)
48-
if !ok {
49-
return errors.New("invalid v2 shim create options format")
38+
opts, err := ti.getRuncOptions()
39+
if err != nil {
40+
return err
5041
}
5142
opts.NoPivotRoot = true
5243
return nil
@@ -55,12 +46,9 @@ func WithNoPivotRoot(_ context.Context, _ *Client, ti *TaskInfo) error {
5546
// WithShimCgroup sets the existing cgroup for the shim
5647
func WithShimCgroup(path string) NewTaskOpts {
5748
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
58-
if ti.Options == nil {
59-
ti.Options = &options.Options{}
60-
}
61-
opts, ok := ti.Options.(*options.Options)
62-
if !ok {
63-
return errors.New("invalid v2 shim create options format")
49+
opts, err := ti.getRuncOptions()
50+
if err != nil {
51+
return err
6452
}
6553
opts.ShimCgroup = path
6654
return nil
@@ -70,12 +58,9 @@ func WithShimCgroup(path string) NewTaskOpts {
7058
// WithUIDOwner allows console I/O to work with the remapped UID in user namespace
7159
func WithUIDOwner(uid uint32) NewTaskOpts {
7260
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
73-
if ti.Options == nil {
74-
ti.Options = &options.Options{}
75-
}
76-
opts, ok := ti.Options.(*options.Options)
77-
if !ok {
78-
return errors.New("invalid v2 shim create options format")
61+
opts, err := ti.getRuncOptions()
62+
if err != nil {
63+
return err
7964
}
8065
opts.IoUid = uid
8166
return nil
@@ -85,12 +70,9 @@ func WithUIDOwner(uid uint32) NewTaskOpts {
8570
// WithGIDOwner allows console I/O to work with the remapped GID in user namespace
8671
func WithGIDOwner(gid uint32) NewTaskOpts {
8772
return func(ctx context.Context, c *Client, ti *TaskInfo) error {
88-
if ti.Options == nil {
89-
ti.Options = &options.Options{}
90-
}
91-
opts, ok := ti.Options.(*options.Options)
92-
if !ok {
93-
return errors.New("invalid v2 shim create options format")
73+
opts, err := ti.getRuncOptions()
74+
if err != nil {
75+
return err
9476
}
9577
opts.IoGid = gid
9678
return nil

0 commit comments

Comments
 (0)