container_checkpoint_opts.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package containerd
  14. import (
  15. "bytes"
  16. "context"
  17. "errors"
  18. "fmt"
  19. "runtime"
  20. tasks "github.com/containerd/containerd/api/services/tasks/v1"
  21. "github.com/containerd/containerd/containers"
  22. "github.com/containerd/containerd/diff"
  23. "github.com/containerd/containerd/images"
  24. "github.com/containerd/containerd/platforms"
  25. "github.com/containerd/containerd/protobuf"
  26. "github.com/containerd/containerd/protobuf/proto"
  27. "github.com/containerd/containerd/rootfs"
  28. "github.com/containerd/containerd/runtime/v2/runc/options"
  29. "github.com/opencontainers/go-digest"
  30. imagespec "github.com/opencontainers/image-spec/specs-go/v1"
  31. )
  32. var (
  33. // ErrCheckpointRWUnsupported is returned if the container runtime does not support checkpoint
  34. ErrCheckpointRWUnsupported = errors.New("rw checkpoint is only supported on v2 runtimes")
  35. // ErrMediaTypeNotFound returns an error when a media type in the manifest is unknown
  36. ErrMediaTypeNotFound = errors.New("media type not found")
  37. )
  38. // CheckpointOpts are options to manage the checkpoint operation
  39. type CheckpointOpts func(context.Context, *Client, *containers.Container, *imagespec.Index, *options.CheckpointOptions) error
  40. // WithCheckpointImage includes the container image in the checkpoint
  41. func WithCheckpointImage(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  42. ir, err := client.ImageService().Get(ctx, c.Image)
  43. if err != nil {
  44. return err
  45. }
  46. index.Manifests = append(index.Manifests, ir.Target)
  47. return nil
  48. }
  49. // WithCheckpointTask includes the running task
  50. func WithCheckpointTask(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  51. any, err := protobuf.MarshalAnyToProto(copts)
  52. if err != nil {
  53. return nil
  54. }
  55. task, err := client.TaskService().Checkpoint(ctx, &tasks.CheckpointTaskRequest{
  56. ContainerID: c.ID,
  57. Options: any,
  58. })
  59. if err != nil {
  60. return err
  61. }
  62. for _, d := range task.Descriptors {
  63. platformSpec := platforms.DefaultSpec()
  64. index.Manifests = append(index.Manifests, imagespec.Descriptor{
  65. MediaType: d.MediaType,
  66. Size: d.Size,
  67. Digest: digest.Digest(d.Digest),
  68. Platform: &platformSpec,
  69. Annotations: d.Annotations,
  70. })
  71. }
  72. // save copts
  73. data, err := proto.Marshal(any)
  74. if err != nil {
  75. return err
  76. }
  77. r := bytes.NewReader(data)
  78. desc, err := writeContent(ctx, client.ContentStore(), images.MediaTypeContainerd1CheckpointOptions, c.ID+"-checkpoint-options", r)
  79. if err != nil {
  80. return err
  81. }
  82. desc.Platform = &imagespec.Platform{
  83. OS: runtime.GOOS,
  84. Architecture: runtime.GOARCH,
  85. }
  86. index.Manifests = append(index.Manifests, desc)
  87. return nil
  88. }
  89. // WithCheckpointRuntime includes the container runtime info
  90. func WithCheckpointRuntime(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  91. if c.Runtime.Options != nil && c.Runtime.Options.GetValue() != nil {
  92. any := protobuf.FromAny(c.Runtime.Options)
  93. data, err := proto.Marshal(any)
  94. if err != nil {
  95. return err
  96. }
  97. r := bytes.NewReader(data)
  98. desc, err := writeContent(ctx, client.ContentStore(), images.MediaTypeContainerd1CheckpointRuntimeOptions, c.ID+"-runtime-options", r)
  99. if err != nil {
  100. return err
  101. }
  102. desc.Platform = &imagespec.Platform{
  103. OS: runtime.GOOS,
  104. Architecture: runtime.GOARCH,
  105. }
  106. index.Manifests = append(index.Manifests, desc)
  107. }
  108. return nil
  109. }
  110. // WithCheckpointRW includes the rw in the checkpoint
  111. func WithCheckpointRW(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  112. diffOpts := []diff.Opt{
  113. diff.WithReference(fmt.Sprintf("checkpoint-rw-%s", c.SnapshotKey)),
  114. }
  115. rw, err := rootfs.CreateDiff(ctx,
  116. c.SnapshotKey,
  117. client.SnapshotService(c.Snapshotter),
  118. client.DiffService(),
  119. diffOpts...,
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. rw.Platform = &imagespec.Platform{
  125. OS: runtime.GOOS,
  126. Architecture: runtime.GOARCH,
  127. }
  128. index.Manifests = append(index.Manifests, rw)
  129. return nil
  130. }
  131. // WithCheckpointTaskExit causes the task to exit after checkpoint
  132. func WithCheckpointTaskExit(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  133. copts.Exit = true
  134. return nil
  135. }
  136. // GetIndexByMediaType returns the index in a manifest for the specified media type
  137. func GetIndexByMediaType(index *imagespec.Index, mt string) (*imagespec.Descriptor, error) {
  138. for _, d := range index.Manifests {
  139. if d.MediaType == mt {
  140. return &d, nil
  141. }
  142. }
  143. return nil, ErrMediaTypeNotFound
  144. }