|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package checkpoint |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 26 | + |
| 27 | + "github.com/containerd/containerd/api/types/runc/options" |
| 28 | + "github.com/containerd/containerd/archive" |
| 29 | + containerd "github.com/containerd/containerd/v2/client" |
| 30 | + "github.com/containerd/containerd/v2/core/content" |
| 31 | + "github.com/containerd/containerd/v2/core/images" |
| 32 | + "github.com/containerd/containerd/v2/plugins" |
| 33 | + |
| 34 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 35 | + "github.com/containerd/nerdctl/v2/pkg/checkpointutil" |
| 36 | + "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" |
| 37 | +) |
| 38 | + |
| 39 | +func Create(ctx context.Context, client *containerd.Client, containerID string, checkpointName string, options types.CheckpointCreateOptions) error { |
| 40 | + var container containerd.Container |
| 41 | + |
| 42 | + walker := &containerwalker.ContainerWalker{ |
| 43 | + Client: client, |
| 44 | + OnFound: func(ctx context.Context, found containerwalker.Found) error { |
| 45 | + if found.MatchCount > 1 { |
| 46 | + return fmt.Errorf("multiple containers found with provided prefix: %s", found.Req) |
| 47 | + } |
| 48 | + container = found.Container |
| 49 | + return nil |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + n, err := walker.Walk(ctx, containerID) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } else if n == 0 { |
| 57 | + return fmt.Errorf("error creating checkpoint for container: %s, no such container", containerID) |
| 58 | + } |
| 59 | + |
| 60 | + info, err := container.Info(ctx) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to get info for container %q: %w", containerID, err) |
| 63 | + } |
| 64 | + |
| 65 | + task, err := container.Task(ctx, nil) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to get task for container %q: %w", containerID, err) |
| 68 | + } |
| 69 | + |
| 70 | + img, err := task.Checkpoint(ctx, withCheckpointOpts(info.Runtime.Name, options.LeaveRunning)) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + defer client.ImageService().Delete(ctx, img.Name()) |
| 76 | + |
| 77 | + cs := client.ContentStore() |
| 78 | + |
| 79 | + rawIndex, err := content.ReadBlob(ctx, cs, img.Target()) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to retrieve checkpoint data: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + var index ocispec.Index |
| 85 | + if err := json.Unmarshal(rawIndex, &index); err != nil { |
| 86 | + return fmt.Errorf("failed to decode checkpoint data: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + var cpDesc *ocispec.Descriptor |
| 90 | + for _, m := range index.Manifests { |
| 91 | + if m.MediaType == images.MediaTypeContainerd1Checkpoint { |
| 92 | + cpDesc = &m //nolint:gosec |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + if cpDesc == nil { |
| 97 | + return errors.New("invalid checkpoint") |
| 98 | + } |
| 99 | + |
| 100 | + targetPath, err := checkpointutil.GetCheckpointDir(options.CheckpointDir, checkpointName, container.ID(), true) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + rat, err := cs.ReaderAt(ctx, *cpDesc) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("failed to get checkpoint reader: %w", err) |
| 108 | + } |
| 109 | + defer rat.Close() |
| 110 | + |
| 111 | + _, err = archive.Apply(ctx, targetPath, content.NewReader(rat)) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to read checkpoint reader: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + fmt.Fprintf(options.Stdout, "%s\n", checkpointName) |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func withCheckpointOpts(rt string, exit bool) containerd.CheckpointTaskOpts { |
| 122 | + return func(r *containerd.CheckpointTaskInfo) error { |
| 123 | + |
| 124 | + switch rt { |
| 125 | + case plugins.RuntimeRuncV2: |
| 126 | + if r.Options == nil { |
| 127 | + r.Options = &options.CheckpointOptions{} |
| 128 | + } |
| 129 | + opts, _ := r.Options.(*options.CheckpointOptions) |
| 130 | + |
| 131 | + opts.Exit = exit |
| 132 | + } |
| 133 | + return nil |
| 134 | + } |
| 135 | +} |
0 commit comments