MCPcopy
hub / github.com/containerd/containerd / loadContainer

Method loadContainer

internal/cri/server/restart.go:265–433  ·  view source on GitHub ↗

loadContainer loads container from containerd and status checkpoint.

(ctx context.Context, cntr containerd.Container)

Source from the content-addressed store, hash-verified

263
264// loadContainer loads container from containerd and status checkpoint.
265func (c *criService) loadContainer(ctx context.Context, cntr containerd.Container) (containerstore.Container, <-chan containerd.ExitStatus, uint32, error) {
266 var exitCh <-chan containerd.ExitStatus
267 var statusPid uint32
268 ctx, cancel := context.WithTimeout(ctx, loadContainerTimeout)
269 defer cancel()
270 id := cntr.ID()
271 containerDir := c.getContainerRootDir(id)
272 var container containerstore.Container
273 // Load container metadata.
274 exts, err := cntr.Extensions(ctx)
275 if err != nil {
276 return container, nil, 0, fmt.Errorf("failed to get container extensions: %w", err)
277 }
278 ext, ok := exts[crilabels.ContainerMetadataExtension]
279 if !ok {
280 return container, nil, 0, fmt.Errorf("metadata extension %q not found", crilabels.ContainerMetadataExtension)
281 }
282 data, err := typeurl.UnmarshalAny(ext)
283 if err != nil {
284 return container, nil, 0, fmt.Errorf("failed to unmarshal metadata extension %q: %w", ext, err)
285 }
286 meta := data.(*containerstore.Metadata)
287
288 // Load status from checkpoint.
289 status, err := containerstore.LoadStatus(containerDir, id)
290 if err != nil {
291 log.G(ctx).WithError(err).Warnf("Failed to load container status for %q", id)
292 status = unknownContainerStatus()
293 }
294
295 var containerIO *cio.ContainerIO
296 err = func() error {
297 // Load up-to-date status from containerd.
298 t, err := cntr.Task(ctx, func(fifos *containerdio.FIFOSet) (_ containerdio.IO, err error) {
299 stdoutWC, stderrWC, err := c.createContainerLoggers(meta.LogPath, meta.Config.GetTty())
300 if err != nil {
301 return nil, err
302 }
303 defer func() {
304 if err != nil {
305 if stdoutWC != nil {
306 stdoutWC.Close()
307 }
308 if stderrWC != nil {
309 stderrWC.Close()
310 }
311 }
312 }()
313 containerIO, err = cio.NewContainerIO(id,
314 cio.WithFIFOs(fifos),
315 )
316 if err != nil {
317 return nil, err
318 }
319 containerIO.AddOutput("log", stdoutWC, stderrWC)
320 containerIO.Pipe()
321 return containerIO, nil
322 })

Callers 1

recoverMethod · 0.95

Implementers 1

criServiceinternal/cri/server/service.go

Calls 15

getContainerRootDirMethod · 0.95
AddOutputMethod · 0.95
PipeMethod · 0.95
createContainerIOMethod · 0.95
unknownContainerStatusFunction · 0.85
IDMethod · 0.65
ExtensionsMethod · 0.65
UnmarshalAnyMethod · 0.65
TaskMethod · 0.65
CloseMethod · 0.65
StatusMethod · 0.65

Tested by

no test coverage detected