Skip to content

Commit 07202ac

Browse files
committed
checkpoint: support checkpoint ls command
Implement `nerdctl checkpoint ls` command to list checkpoints for a container, matching Docker's output format with "CHECKPOINT NAME" header. Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 9747175 commit 07202ac

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

cmd/nerdctl/checkpoint/checkpoint.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ func Command() *cobra.Command {
3434

3535
cmd.AddCommand(
3636
CreateCommand(),
37+
checkpointLsCommand(),
3738
)
3839

3940
return cmd
4041
}
42+
43+
func checkpointLsCommand() *cobra.Command {
44+
x := ListCommand()
45+
x.Use = "ls"
46+
x.Aliases = []string{"list"}
47+
return x
48+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
"fmt"
21+
"text/tabwriter"
22+
23+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
25+
"github.com/containerd/nerdctl/v2/pkg/api/types"
26+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
27+
"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func ListCommand() *cobra.Command {
32+
var cmd = &cobra.Command{
33+
Use: "list [OPTIONS] CONTAINER",
34+
Short: "List checkpoints for a container",
35+
Args: cobra.ExactArgs(1),
36+
RunE: listAction,
37+
ValidArgsFunction: listShellComplete,
38+
SilenceUsage: true,
39+
SilenceErrors: true,
40+
}
41+
cmd.Flags().String("checkpoint-dir", "", "Checkpoint directory")
42+
return cmd
43+
}
44+
45+
func processListFlags(cmd *cobra.Command) (types.CheckpointListOptions, error) {
46+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
47+
if err != nil {
48+
return types.CheckpointListOptions{}, err
49+
}
50+
51+
checkpointDir, err := cmd.Flags().GetString("checkpoint-dir")
52+
if err != nil {
53+
return types.CheckpointListOptions{}, err
54+
}
55+
if checkpointDir == "" {
56+
checkpointDir = globalOptions.DataRoot + "/checkpoints"
57+
}
58+
59+
return types.CheckpointListOptions{
60+
Stdout: cmd.OutOrStdout(),
61+
GOptions: globalOptions,
62+
CheckpointDir: checkpointDir,
63+
}, nil
64+
}
65+
66+
func listAction(cmd *cobra.Command, args []string) error {
67+
listOptions, err := processListFlags(cmd)
68+
if err != nil {
69+
return err
70+
}
71+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), listOptions.GOptions.Namespace, listOptions.GOptions.Address)
72+
if err != nil {
73+
return err
74+
}
75+
defer cancel()
76+
77+
checkpoints, err := checkpoint.List(ctx, client, args[0], listOptions)
78+
if err != nil {
79+
return err
80+
}
81+
82+
w := tabwriter.NewWriter(listOptions.Stdout, 4, 8, 4, ' ', 0)
83+
fmt.Fprintln(w, "CHECKPOINT NAME")
84+
85+
for _, cp := range checkpoints {
86+
fmt.Fprintln(w, cp.Name)
87+
}
88+
89+
return w.Flush()
90+
}
91+
92+
func listShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
93+
return completion.ImageNames(cmd)
94+
}

pkg/api/types/checkpoint_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ type CheckpointCreateOptions struct {
2727
// Checkpoint directory
2828
CheckpointDir string
2929
}
30+
31+
type CheckpointListOptions struct {
32+
Stdout io.Writer
33+
GOptions GlobalCommandOptions
34+
// Checkpoint directory
35+
CheckpointDir string
36+
}
37+
38+
type CheckpointSummary struct {
39+
// Name is the name of the checkpoint.
40+
Name string
41+
}

pkg/cmd/checkpoint/list.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
"os"
23+
24+
containerd "github.com/containerd/containerd/v2/client"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/api/types"
27+
"github.com/containerd/nerdctl/v2/pkg/checkpointutil"
28+
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
29+
)
30+
31+
func List(ctx context.Context, client *containerd.Client, containerID string, options types.CheckpointListOptions) ([]types.CheckpointSummary, error) {
32+
var container containerd.Container
33+
var out []types.CheckpointSummary
34+
35+
walker := &containerwalker.ContainerWalker{
36+
Client: client,
37+
OnFound: func(ctx context.Context, found containerwalker.Found) error {
38+
if found.MatchCount > 1 {
39+
return fmt.Errorf("multiple containers found with provided prefix: %s", found.Req)
40+
}
41+
container = found.Container
42+
return nil
43+
},
44+
}
45+
46+
n, err := walker.Walk(ctx, containerID)
47+
if err != nil {
48+
return nil, err
49+
} else if n == 0 {
50+
return nil, fmt.Errorf("error list checkpoint for container: %s, no such container", containerID)
51+
}
52+
53+
checkpointDir, err := checkpointutil.GetCheckpointDir(options.CheckpointDir, "", container.ID(), false)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
if err := os.MkdirAll(checkpointDir, 0o755); err != nil {
59+
return nil, err
60+
}
61+
62+
dirs, err := os.ReadDir(checkpointDir)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
for _, d := range dirs {
68+
if !d.IsDir() {
69+
continue
70+
}
71+
out = append(out, types.CheckpointSummary{Name: d.Name()})
72+
}
73+
74+
return out, nil
75+
}

0 commit comments

Comments
 (0)