Skip to content

Commit c3b3062

Browse files
committed
add task api endpoint in task create options
Signed-off-by: Abel Feng <[email protected]>
1 parent 72fe47b commit c3b3062

File tree

5 files changed

+78
-24
lines changed

5 files changed

+78
-24
lines changed

client/task_opts.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ func WithRuntimePath(absRuntimePath string) NewTaskOpts {
5050
}
5151
}
5252

53+
// WithTaskAPIEndpoint allow task service to manage a task through a given endpoint,
54+
// usually it is served inside a sandbox, and we can get it from sandbox status.
55+
func WithTaskAPIEndpoint(address, protocol string, version uint32) NewTaskOpts {
56+
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")
63+
}
64+
opts.TaskApiAddress = address
65+
opts.TaskApiProtocol = protocol
66+
opts.TaskApiVersion = version
67+
return nil
68+
}
69+
}
70+
5371
// WithTaskCheckpoint allows a task to be created with live runtime and memory data from a
5472
// previous checkpoint. Additional software such as CRIU may be required to
5573
// restore a task from a checkpoint

core/runtime/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type CreateOpts struct {
5151
Runtime string
5252
// SandboxID is an optional ID of sandbox this container belongs to
5353
SandboxID string
54+
// Address is an optional Address for Task API server
55+
Address string
56+
// Protocol is an optional Protocol for Task API connection
57+
Protocol string
58+
// Version is an optional Version of the Task API
59+
Version uint32
5460
}
5561

5662
// Exit information for a process

core/runtime/v2/shim_manager.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,29 @@ func (m *ShimManager) ID() string {
163163
func (m *ShimManager) Start(ctx context.Context, id string, bundle *Bundle, opts runtime.CreateOpts) (_ ShimInstance, retErr error) {
164164
// This container belongs to sandbox which supposed to be already started via sandbox API.
165165
if opts.SandboxID != "" {
166-
process, err := m.Get(ctx, opts.SandboxID)
167-
if err != nil {
168-
return nil, fmt.Errorf("can't find sandbox %s", opts.SandboxID)
166+
var params shimbinary.BootstrapParams
167+
if opts.Address != "" && opts.Protocol != "" {
168+
params = shimbinary.BootstrapParams{
169+
Version: int(opts.Version),
170+
Address: opts.Address,
171+
Protocol: opts.Protocol,
172+
}
173+
} else {
174+
// For those sandbox we can not get endpoint,
175+
// fallback to legacy implementation
176+
p, restoreErr := m.restoreBootstrapParams(ctx, opts.SandboxID)
177+
if restoreErr != nil {
178+
return nil, fmt.Errorf("failed to get bootstrap "+
179+
"params of sandbox %s, %v, legacy restore error %v", opts.SandboxID, err, restoreErr)
180+
}
181+
params = p
169182
}
170183

171184
// Write sandbox ID this task belongs to.
172185
if err := os.WriteFile(filepath.Join(bundle.Path, "sandbox"), []byte(opts.SandboxID), 0600); err != nil {
173186
return nil, err
174187
}
175188

176-
params, err := restoreBootstrapParams(process.Bundle())
177-
if err != nil {
178-
return nil, err
179-
}
180-
181189
if err := writeBootstrapParams(filepath.Join(bundle.Path, "bootstrap.json"), params); err != nil {
182190
return nil, fmt.Errorf("failed to write bootstrap.json for bundle %s: %w", bundle.Path, err)
183191
}
@@ -251,6 +259,18 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string,
251259
return shim, nil
252260
}
253261

262+
func (m *ShimManager) restoreBootstrapParams(ctx context.Context, sandboxID string) (shimbinary.BootstrapParams, error) {
263+
process, err := m.Get(ctx, sandboxID)
264+
if err != nil {
265+
return shimbinary.BootstrapParams{}, fmt.Errorf("can't find sandbox %s", sandboxID)
266+
}
267+
params, err := restoreBootstrapParams(filepath.Join(m.state, process.Namespace(), sandboxID))
268+
if err != nil {
269+
return shimbinary.BootstrapParams{}, err
270+
}
271+
return params, nil
272+
}
273+
254274
// restoreBootstrapParams reads bootstrap.json to restore shim configuration.
255275
// If its an old shim, this will perform migration - read address file and write default bootstrap
256276
// configuration (version = 2, protocol = ttrpc, and address).

internal/cri/server/container_start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContain
123123
endpoint := sandbox.Endpoint
124124
if endpoint.IsValid() {
125125
taskOpts = append(taskOpts,
126-
containerd.WithTaskApiEndpoint(endpoint.Address, endpoint.Protocol, endpoint.Version))
126+
containerd.WithTaskAPIEndpoint(endpoint.Address, endpoint.Protocol, endpoint.Version))
127127
}
128128

129129
task, err := container.NewTask(ctx, ioCreation, taskOpts...)

plugins/services/tasks/local.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,25 @@ func (l *local) Create(ctx context.Context, r *api.CreateTaskRequest, _ ...grpc.
155155
if err != nil {
156156
return nil, errdefs.ToGRPC(err)
157157
}
158-
checkpointPath, err := getRestorePath(container.Runtime.Name, r.Options)
159-
if err != nil {
160-
return nil, err
158+
159+
var (
160+
checkpointPath string
161+
taskAPIAddress string
162+
taskAPIProtocol string
163+
taskAPIVersion uint32
164+
)
165+
166+
if r.Options != nil {
167+
taskOptions, err := formatOptions(container.Runtime.Name, r.Options)
168+
if err != nil {
169+
return nil, err
170+
}
171+
checkpointPath = taskOptions.CriuImagePath
172+
taskAPIAddress = taskOptions.TaskApiAddress
173+
taskAPIProtocol = taskOptions.TaskApiProtocol
174+
taskAPIVersion = taskOptions.TaskApiVersion
161175
}
176+
162177
// jump get checkpointPath from checkpoint image
163178
if checkpointPath == "" && r.Checkpoint != nil {
164179
checkpointPath, err = os.MkdirTemp(os.Getenv("XDG_RUNTIME_DIR"), "ctrd-checkpoint")
@@ -196,6 +211,9 @@ func (l *local) Create(ctx context.Context, r *api.CreateTaskRequest, _ ...grpc.
196211
RuntimeOptions: container.Runtime.Options,
197212
TaskOptions: r.Options,
198213
SandboxID: container.SandboxID,
214+
Address: taskAPIAddress,
215+
Protocol: taskAPIProtocol,
216+
Version: taskAPIVersion,
199217
}
200218
if r.RuntimePath != "" {
201219
opts.Runtime = r.RuntimePath
@@ -723,22 +741,14 @@ func getCheckpointPath(runtime string, option *ptypes.Any) (string, error) {
723741
return checkpointPath, nil
724742
}
725743

726-
// getRestorePath only suitable for runc runtime now
727-
func getRestorePath(runtime string, option *ptypes.Any) (string, error) {
728-
if option == nil {
729-
return "", nil
730-
}
731-
732-
var restorePath string
744+
func formatOptions(runtime string, option *ptypes.Any) (*options.Options, error) {
733745
v, err := typeurl.UnmarshalAny(option)
734746
if err != nil {
735-
return "", err
747+
return nil, err
736748
}
737749
opts, ok := v.(*options.Options)
738750
if !ok {
739-
return "", fmt.Errorf("invalid task create option for %s", runtime)
751+
return nil, fmt.Errorf("invalid task create option for %s", runtime)
740752
}
741-
restorePath = opts.CriuImagePath
742-
743-
return restorePath, nil
753+
return opts, nil
744754
}

0 commit comments

Comments
 (0)