container_checkpoint_opts.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/rootfs"
  26. "github.com/containerd/containerd/runtime/v2/runc/options"
  27. "github.com/containerd/typeurl"
  28. imagespec "github.com/opencontainers/image-spec/specs-go/v1"
  29. )
  30. var (
  31. // ErrCheckpointRWUnsupported is returned if the container runtime does not support checkpoint
  32. ErrCheckpointRWUnsupported = errors.New("rw checkpoint is only supported on v2 runtimes")
  33. // ErrMediaTypeNotFound returns an error when a media type in the manifest is unknown
  34. ErrMediaTypeNotFound = errors.New("media type not found")
  35. )
  36. // CheckpointOpts are options to manage the checkpoint operation
  37. type CheckpointOpts func(context.Context, *Client, *containers.Container, *imagespec.Index, *options.CheckpointOptions) error
  38. // WithCheckpointImage includes the container image in the checkpoint
  39. func WithCheckpointImage(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  40. ir, err := client.ImageService().Get(ctx, c.Image)
  41. if err != nil {
  42. return err
  43. }
  44. index.Manifests = append(index.Manifests, ir.Target)
  45. return nil
  46. }
  47. // WithCheckpointTask includes the running task
  48. func WithCheckpointTask(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  49. any, err := typeurl.MarshalAny(copts)
  50. if err != nil {
  51. return nil
  52. }
  53. task, err := client.TaskService().Checkpoint(ctx, &tasks.CheckpointTaskRequest{
  54. ContainerID: c.ID,
  55. Options: any,
  56. })
  57. if err != nil {
  58. return err
  59. }
  60. for _, d := range task.Descriptors {
  61. platformSpec := platforms.DefaultSpec()
  62. index.Manifests = append(index.Manifests, imagespec.Descriptor{
  63. MediaType: d.MediaType,
  64. Size: d.Size_,
  65. Digest: d.Digest,
  66. Platform: &platformSpec,
  67. Annotations: d.Annotations,
  68. })
  69. }
  70. // save copts
  71. data, err := any.Marshal()
  72. if err != nil {
  73. return err
  74. }
  75. r := bytes.NewReader(data)
  76. desc, err := writeContent(ctx, client.ContentStore(), images.MediaTypeContainerd1CheckpointOptions, c.ID+"-checkpoint-options", r)
  77. if err != nil {
  78. return err
  79. }
  80. desc.Platform = &imagespec.Platform{
  81. OS: runtime.GOOS,
  82. Architecture: runtime.GOARCH,
  83. }
  84. index.Manifests = append(index.Manifests, desc)
  85. return nil
  86. }
  87. // WithCheckpointRuntime includes the container runtime info
  88. func WithCheckpointRuntime(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  89. if c.Runtime.Options != nil {
  90. data, err := c.Runtime.Options.Marshal()
  91. if err != nil {
  92. return err
  93. }
  94. r := bytes.NewReader(data)
  95. desc, err := writeContent(ctx, client.ContentStore(), images.MediaTypeContainerd1CheckpointRuntimeOptions, c.ID+"-runtime-options", r)
  96. if err != nil {
  97. return err
  98. }
  99. desc.Platform = &imagespec.Platform{
  100. OS: runtime.GOOS,
  101. Architecture: runtime.GOARCH,
  102. }
  103. index.Manifests = append(index.Manifests, desc)
  104. }
  105. return nil
  106. }
  107. // WithCheckpointRW includes the rw in the checkpoint
  108. func WithCheckpointRW(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  109. diffOpts := []diff.Opt{
  110. diff.WithReference(fmt.Sprintf("checkpoint-rw-%s", c.SnapshotKey)),
  111. }
  112. rw, err := rootfs.CreateDiff(ctx,
  113. c.SnapshotKey,
  114. client.SnapshotService(c.Snapshotter),
  115. client.DiffService(),
  116. diffOpts...,
  117. )
  118. if err != nil {
  119. return err
  120. }
  121. rw.Platform = &imagespec.Platform{
  122. OS: runtime.GOOS,
  123. Architecture: runtime.GOARCH,
  124. }
  125. index.Manifests = append(index.Manifests, rw)
  126. return nil
  127. }
  128. // WithCheckpointTaskExit causes the task to exit after checkpoint
  129. func WithCheckpointTaskExit(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
  130. copts.Exit = true
  131. return nil
  132. }
  133. // GetIndexByMediaType returns the index in a manifest for the specified media type
  134. func GetIndexByMediaType(index *imagespec.Index, mt string) (*imagespec.Descriptor, error) {
  135. for _, d := range index.Manifests {
  136. if d.MediaType == mt {
  137. return &d, nil
  138. }
  139. }
  140. return nil, ErrMediaTypeNotFound
  141. }