NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108
(ctx context.Context, client *containerd.Client, container containerd.Container, opts TaskOptions)
| 65 | |
| 66 | // NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108 |
| 67 | func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container, opts TaskOptions) (containerd.Task, error) { |
| 68 | var ( |
| 69 | checkpoint *types.Descriptor |
| 70 | t containerd.Task |
| 71 | err error |
| 72 | ) |
| 73 | |
| 74 | if opts.CheckpointDir != "" { |
| 75 | tar := archive.Diff(ctx, "", opts.CheckpointDir) |
| 76 | cs := client.ContentStore() |
| 77 | writer, err := cs.Writer(ctx, content.WithRef(opts.CheckpointDir)) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | defer writer.Close() |
| 82 | size, err := io.Copy(writer, tar) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | labels := map[string]string{ |
| 87 | "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339), |
| 88 | } |
| 89 | if err = writer.Commit(ctx, size, "", content.WithLabels(labels)); err != nil { |
| 90 | if !errors.Is(err, errdefs.ErrAlreadyExists) { |
| 91 | return nil, err |
| 92 | } |
| 93 | } |
| 94 | checkpoint = &types.Descriptor{ |
| 95 | MediaType: images.MediaTypeContainerd1Checkpoint, |
| 96 | Digest: writer.Digest().String(), |
| 97 | Size: size, |
| 98 | } |
| 99 | defer func() { |
| 100 | if checkpoint != nil { |
| 101 | _ = cs.Delete(ctx, digest.Digest(checkpoint.Digest)) |
| 102 | } |
| 103 | }() |
| 104 | if err = tar.Close(); err != nil { |
| 105 | return nil, fmt.Errorf("failed to close checkpoint tar stream: %w", err) |
| 106 | } |
| 107 | if err != nil { |
| 108 | return nil, fmt.Errorf("failed to upload checkpoint to containerd: %w", err) |
| 109 | } |
| 110 | } |
| 111 | closer := func() { |
| 112 | if opts.DetachC != nil { |
| 113 | opts.DetachC <- struct{}{} |
| 114 | } |
| 115 | // t will be set by container.NewTask at the end of this function. |
| 116 | // |
| 117 | // We cannot use container.Task(ctx, cio.Load) to get the IO here |
| 118 | // because the `cancel` field of the returned `*cio` is nil. [1] |
| 119 | // |
| 120 | // [1] https://github.com/containerd/containerd/blob/8f756bc8c26465bd93e78d9cd42082b66f276e10/cio/io.go#L358-L359 |
| 121 | io := t.IO() |
| 122 | if io == nil { |
| 123 | log.G(ctx).Errorf("got a nil io") |
| 124 | return |
no test coverage detected
searching dependent graphs…