MCPcopy
hub / github.com/containerd/containerd / Create

Method Create

core/runtime/v2/task_manager.go:159–275  ·  view source on GitHub ↗

Create launches new shim instance and creates new task

(ctx context.Context, taskID string, opts runtime.CreateOpts)

Source from the content-addressed store, hash-verified

157
158// Create launches new shim instance and creates new task
159func (m *TaskManager) Create(ctx context.Context, taskID string, opts runtime.CreateOpts) (_ runtime.Task, retErr error) {
160 bundle, err := NewBundle(ctx, m.root, m.state, taskID, opts.Spec)
161 if err != nil {
162 return nil, err
163 }
164 defer func() {
165 if retErr != nil {
166 bundle.Delete()
167 }
168 }()
169
170 log.G(ctx).WithFields(log.Fields{
171 "id": taskID,
172 "runtime": opts.Runtime,
173 }).Debug("creating task")
174
175 activateOpts := []mount.ActivateOpt{
176 mount.WithLabels(map[string]string{
177 "containerd.io/gc.bref.container": taskID,
178 }),
179 }
180 if info, err := m.manager.loadShimInfo(ctx, opts.Runtime); err == nil {
181 for _, t := range info.handledMounts {
182 activateOpts = append(activateOpts, mount.WithAllowMountType(t))
183 }
184 } else {
185 log.G(ctx).WithError(err).WithField("runtime", opts.Runtime).Error("failed to load runtime info")
186 }
187
188 // Add options based on runtime
189 if ai, err := m.mounts.Activate(ctx, taskID, opts.Rootfs, activateOpts...); err == nil {
190 opts.Rootfs = ai.System
191 defer func() {
192 if retErr != nil {
193 dctx, cancel := timeout.WithContext(context.WithoutCancel(ctx), cleanupTimeout)
194 defer cancel()
195 if err := m.mounts.Deactivate(dctx, taskID); err != nil {
196 log.G(ctx).WithError(err).WithField("task", taskID).Errorf("failed to deactivate mounts")
197 }
198 }
199 }()
200 } else if errdefs.IsAlreadyExists(err) {
201 // If creation of task with same identifier, use existing mount rather than forcing
202 // deactivation of the old one. The back reference will prevent racing between
203 // deactivation and re-use, as the container with the same ID would still exist.
204 ai, err = m.mounts.Info(ctx, taskID)
205 if err != nil {
206 return nil, fmt.Errorf("failed to get info on already active mount: %w", err)
207 }
208 opts.Rootfs = ai.System
209 } else if !errdefs.IsNotImplemented(err) {
210 return nil, err
211 }
212
213 shim, err := m.manager.Start(ctx, taskID, bundle, opts)
214 if err != nil {
215 return nil, fmt.Errorf("failed to start shim: %w", err)
216 }

Callers

nothing calls this directly

Calls 15

DeleteMethod · 0.95
WithLabelsFunction · 0.92
WithAllowMountTypeFunction · 0.92
WithContextFunction · 0.92
NewBundleFunction · 0.85
newShimTaskFunction · 0.85
loadShimInfoMethod · 0.80
ActivateMethod · 0.65
DeactivateMethod · 0.65
InfoMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected