Skip to content

Commit 28ea466

Browse files
committed
checkpoint: support checkpoint create command
Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 05ab86f commit 28ea466

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

cmd/nerdctl/checkpoint/checkpoint.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
func Command() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Annotations: map[string]string{helpers.Category: helpers.Management},
27+
Use: "checkpoint",
28+
Short: "Manage checkpoints.",
29+
RunE: helpers.UnknownSubcommandAction,
30+
SilenceUsage: true,
31+
SilenceErrors: true,
32+
}
33+
34+
cmd.AddCommand(
35+
CreateCommand(),
36+
)
37+
38+
return cmd
39+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
21+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
22+
"github.com/containerd/nerdctl/v2/pkg/api/types"
23+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
24+
"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func CreateCommand() *cobra.Command {
29+
var cmd = &cobra.Command{
30+
Use: "create [OPTIONS] CONTAINER CHECKPOINT",
31+
Short: "Create a checkpoint from a running container",
32+
Args: cobra.ExactArgs(2),
33+
RunE: createAction,
34+
ValidArgsFunction: createShellComplete,
35+
SilenceUsage: true,
36+
SilenceErrors: true,
37+
}
38+
cmd.Flags().Bool("leave-running", false, "Leave the container running after checkpointing")
39+
return cmd
40+
}
41+
42+
func processCreateFlags(cmd *cobra.Command) (types.CheckpointCreateOptions, error) {
43+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
44+
if err != nil {
45+
return types.CheckpointCreateOptions{}, err
46+
}
47+
48+
leaveRunning, err := cmd.Flags().GetBool("leave-running")
49+
50+
if err != nil {
51+
return types.CheckpointCreateOptions{}, err
52+
}
53+
54+
return types.CheckpointCreateOptions{
55+
Stdout: cmd.OutOrStdout(),
56+
GOptions: globalOptions,
57+
LeaveRunning: leaveRunning,
58+
}, nil
59+
}
60+
61+
func createAction(cmd *cobra.Command, args []string) error {
62+
createOptions, err := processCreateFlags(cmd)
63+
if err != nil {
64+
return err
65+
}
66+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), createOptions.GOptions.Namespace, createOptions.GOptions.Address)
67+
if err != nil {
68+
return err
69+
}
70+
defer cancel()
71+
72+
err = checkpoint.Create(ctx, client, args[0], args[1], createOptions)
73+
if err != nil {
74+
return err
75+
}
76+
77+
return nil
78+
}
79+
80+
func createShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
81+
return completion.ImageNames(cmd)
82+
}

cmd/nerdctl/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/log"
3232

3333
"github.com/containerd/nerdctl/v2/cmd/nerdctl/builder"
34+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/checkpoint"
3435
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
3536
"github.com/containerd/nerdctl/v2/cmd/nerdctl/compose"
3637
"github.com/containerd/nerdctl/v2/cmd/nerdctl/container"
@@ -350,6 +351,9 @@ Config file ($NERDCTL_TOML): %s
350351

351352
// Manifest
352353
manifest.Command(),
354+
355+
// Checkpoint
356+
checkpoint.Command(),
353357
)
354358
addApparmorCommand(rootCmd)
355359
container.AddCpCommand(rootCmd)

pkg/api/types/checkpoint_types.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 types
18+
19+
import "io"
20+
21+
// CheckpointCreateOptions specifies options for `nerdctl checkpoint create`.
22+
type CheckpointCreateOptions struct {
23+
Stdout io.Writer
24+
GOptions GlobalCommandOptions
25+
// Leave the container running after checkpointing
26+
LeaveRunning bool
27+
}

pkg/cmd/checkpoint/create.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"fmt"
22+
23+
containerd "github.com/containerd/containerd/v2/client"
24+
"github.com/containerd/nerdctl/v2/pkg/api/types"
25+
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
26+
)
27+
28+
func Create(ctx context.Context, client *containerd.Client, containerID string, checkpointDir string, options types.CheckpointCreateOptions) error {
29+
var container containerd.Container
30+
31+
walker := &containerwalker.ContainerWalker{
32+
Client: client,
33+
OnFound: func(ctx context.Context, found containerwalker.Found) error {
34+
if found.MatchCount > 1 {
35+
return fmt.Errorf("multiple containers found with provided prefix: %s", found.Req)
36+
}
37+
container = found.Container
38+
return nil
39+
},
40+
}
41+
42+
n, err := walker.Walk(ctx, containerID)
43+
if err != nil {
44+
return err
45+
} else if n == 0 {
46+
return fmt.Errorf("no such container: %s", containerID)
47+
}
48+
49+
opts := []containerd.CheckpointOpts{
50+
containerd.WithCheckpointRuntime,
51+
containerd.WithCheckpointRW,
52+
containerd.WithCheckpointTask,
53+
}
54+
if !options.LeaveRunning {
55+
opts = append(opts, containerd.WithCheckpointTaskExit)
56+
}
57+
58+
checkpoint, err := container.Checkpoint(ctx, checkpointDir, opts...)
59+
if err != nil {
60+
return err
61+
}
62+
63+
fmt.Fprintf(options.Stdout, "Checkpoint created: %+v\n", checkpoint.Name())
64+
return nil
65+
}

0 commit comments

Comments
 (0)