task_opts.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "syscall"
  20. "github.com/containerd/containerd/api/types"
  21. "github.com/containerd/containerd/content"
  22. "github.com/containerd/containerd/errdefs"
  23. "github.com/containerd/containerd/images"
  24. "github.com/containerd/containerd/mount"
  25. "github.com/containerd/containerd/runtime/linux/runctypes"
  26. "github.com/containerd/containerd/runtime/v2/runc/options"
  27. imagespec "github.com/opencontainers/image-spec/specs-go/v1"
  28. "github.com/opencontainers/runtime-spec/specs-go"
  29. )
  30. // NewTaskOpts allows the caller to set options on a new task
  31. type NewTaskOpts func(context.Context, *Client, *TaskInfo) error
  32. // WithRootFS allows a task to be created without a snapshot being allocated to its container
  33. func WithRootFS(mounts []mount.Mount) NewTaskOpts {
  34. return func(ctx context.Context, c *Client, ti *TaskInfo) error {
  35. ti.RootFS = mounts
  36. return nil
  37. }
  38. }
  39. // WithRuntimePath will force task service to use a custom path to the runtime binary
  40. // instead of resolving it from runtime name.
  41. func WithRuntimePath(absRuntimePath string) NewTaskOpts {
  42. return func(ctx context.Context, client *Client, info *TaskInfo) error {
  43. info.RuntimePath = absRuntimePath
  44. return nil
  45. }
  46. }
  47. // WithTaskCheckpoint allows a task to be created with live runtime and memory data from a
  48. // previous checkpoint. Additional software such as CRIU may be required to
  49. // restore a task from a checkpoint
  50. func WithTaskCheckpoint(im Image) NewTaskOpts {
  51. return func(ctx context.Context, c *Client, info *TaskInfo) error {
  52. desc := im.Target()
  53. id := desc.Digest
  54. index, err := decodeIndex(ctx, c.ContentStore(), desc)
  55. if err != nil {
  56. return err
  57. }
  58. for _, m := range index.Manifests {
  59. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  60. info.Checkpoint = &types.Descriptor{
  61. MediaType: m.MediaType,
  62. Size: m.Size,
  63. Digest: m.Digest.String(),
  64. Annotations: m.Annotations,
  65. }
  66. return nil
  67. }
  68. }
  69. return fmt.Errorf("checkpoint not found in index %s", id)
  70. }
  71. }
  72. func decodeIndex(ctx context.Context, store content.Provider, desc imagespec.Descriptor) (*imagespec.Index, error) {
  73. var index imagespec.Index
  74. p, err := content.ReadBlob(ctx, store, desc)
  75. if err != nil {
  76. return nil, err
  77. }
  78. if err := json.Unmarshal(p, &index); err != nil {
  79. return nil, err
  80. }
  81. return &index, nil
  82. }
  83. // WithCheckpointName sets the image name for the checkpoint
  84. func WithCheckpointName(name string) CheckpointTaskOpts {
  85. return func(r *CheckpointTaskInfo) error {
  86. r.Name = name
  87. return nil
  88. }
  89. }
  90. // WithCheckpointImagePath sets image path for checkpoint option
  91. func WithCheckpointImagePath(path string) CheckpointTaskOpts {
  92. return func(r *CheckpointTaskInfo) error {
  93. if CheckRuntime(r.Runtime(), "io.containerd.runc") {
  94. if r.Options == nil {
  95. r.Options = &options.CheckpointOptions{}
  96. }
  97. opts, ok := r.Options.(*options.CheckpointOptions)
  98. if !ok {
  99. return errors.New("invalid v2 shim checkpoint options format")
  100. }
  101. opts.ImagePath = path
  102. } else {
  103. if r.Options == nil {
  104. r.Options = &runctypes.CheckpointOptions{}
  105. }
  106. opts, ok := r.Options.(*runctypes.CheckpointOptions)
  107. if !ok {
  108. return errors.New("invalid v1 shim checkpoint options format")
  109. }
  110. opts.ImagePath = path
  111. }
  112. return nil
  113. }
  114. }
  115. // WithRestoreImagePath sets image path for create option
  116. func WithRestoreImagePath(path string) NewTaskOpts {
  117. return func(ctx context.Context, c *Client, ti *TaskInfo) error {
  118. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  119. if ti.Options == nil {
  120. ti.Options = &options.Options{}
  121. }
  122. opts, ok := ti.Options.(*options.Options)
  123. if !ok {
  124. return errors.New("invalid v2 shim create options format")
  125. }
  126. opts.CriuImagePath = path
  127. } else {
  128. if ti.Options == nil {
  129. ti.Options = &runctypes.CreateOptions{}
  130. }
  131. opts, ok := ti.Options.(*runctypes.CreateOptions)
  132. if !ok {
  133. return errors.New("invalid v1 shim create options format")
  134. }
  135. opts.CriuImagePath = path
  136. }
  137. return nil
  138. }
  139. }
  140. // ProcessDeleteOpts allows the caller to set options for the deletion of a task
  141. type ProcessDeleteOpts func(context.Context, Process) error
  142. // WithProcessKill will forcefully kill and delete a process
  143. func WithProcessKill(ctx context.Context, p Process) error {
  144. ctx, cancel := context.WithCancel(ctx)
  145. defer cancel()
  146. // ignore errors to wait and kill as we are forcefully killing
  147. // the process and don't care about the exit status
  148. s, err := p.Wait(ctx)
  149. if err != nil {
  150. return err
  151. }
  152. if err := p.Kill(ctx, syscall.SIGKILL, WithKillAll); err != nil {
  153. // Kill might still return an IsNotFound error, even if it actually
  154. // killed the process.
  155. if errdefs.IsNotFound(err) {
  156. select {
  157. case <-ctx.Done():
  158. return ctx.Err()
  159. case <-s:
  160. return nil
  161. }
  162. }
  163. if errdefs.IsFailedPrecondition(err) {
  164. return nil
  165. }
  166. return err
  167. }
  168. // wait for the process to fully stop before letting the rest of the deletion complete
  169. <-s
  170. return nil
  171. }
  172. // KillInfo contains information on how to process a Kill action
  173. type KillInfo struct {
  174. // All kills all processes inside the task
  175. // only valid on tasks, ignored on processes
  176. All bool
  177. // ExecID is the ID of a process to kill
  178. ExecID string
  179. }
  180. // KillOpts allows options to be set for the killing of a process
  181. type KillOpts func(context.Context, *KillInfo) error
  182. // WithKillAll kills all processes for a task
  183. func WithKillAll(ctx context.Context, i *KillInfo) error {
  184. i.All = true
  185. return nil
  186. }
  187. // WithKillExecID specifies the process ID
  188. func WithKillExecID(execID string) KillOpts {
  189. return func(ctx context.Context, i *KillInfo) error {
  190. i.ExecID = execID
  191. return nil
  192. }
  193. }
  194. // WithResources sets the provided resources for task updates. Resources must be
  195. // either a *specs.LinuxResources or a *specs.WindowsResources
  196. func WithResources(resources interface{}) UpdateTaskOpts {
  197. return func(ctx context.Context, client *Client, r *UpdateTaskInfo) error {
  198. switch resources.(type) {
  199. case *specs.LinuxResources:
  200. case *specs.WindowsResources:
  201. default:
  202. return errors.New("WithResources requires a *specs.LinuxResources or *specs.WindowsResources")
  203. }
  204. r.Resources = resources
  205. return nil
  206. }
  207. }
  208. // WithAnnotations sets the provided annotations for task updates.
  209. func WithAnnotations(annotations map[string]string) UpdateTaskOpts {
  210. return func(ctx context.Context, client *Client, r *UpdateTaskInfo) error {
  211. r.Annotations = annotations
  212. return nil
  213. }
  214. }