Skip to content

Commit 447d4cc

Browse files
committed
checkpoint: support checkpoint rm command
support checkpoint rm command. Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 4bbe454 commit 447d4cc

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

cmd/nerdctl/checkpoint/checkpoint.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func Command() *cobra.Command {
3535
cmd.AddCommand(
3636
CreateCommand(),
3737
checkpointLsCommand(),
38+
checkpointRmCommand(),
3839
)
3940

4041
return cmd
@@ -46,3 +47,9 @@ func checkpointLsCommand() *cobra.Command {
4647
x.Aliases = []string{"list"}
4748
return x
4849
}
50+
func checkpointRmCommand() *cobra.Command {
51+
x := RemoveCommand()
52+
x.Use = "rm"
53+
x.Aliases = []string{"remove"}
54+
return x
55+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"path/filepath"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
26+
"github.com/containerd/nerdctl/v2/pkg/api/types"
27+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
28+
"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint"
29+
)
30+
31+
func RemoveCommand() *cobra.Command {
32+
var cmd = &cobra.Command{
33+
Use: "rm [OPTIONS] CONTAINER CHECKPOINT",
34+
Short: "Remove a checkpoint",
35+
Args: cobra.ExactArgs(2),
36+
RunE: removeAction,
37+
ValidArgsFunction: removeShellComplete,
38+
SilenceUsage: true,
39+
SilenceErrors: true,
40+
}
41+
cmd.Flags().String("checkpoint-dir", "", "Checkpoint directory")
42+
return cmd
43+
}
44+
45+
func processRemoveFlags(cmd *cobra.Command) (types.CheckpointRemoveOptions, error) {
46+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
47+
if err != nil {
48+
return types.CheckpointRemoveOptions{}, err
49+
}
50+
51+
checkpointDir, err := cmd.Flags().GetString("checkpoint-dir")
52+
if err != nil {
53+
return types.CheckpointRemoveOptions{}, err
54+
}
55+
if checkpointDir == "" {
56+
checkpointDir = filepath.Join(globalOptions.DataRoot, "checkpoints")
57+
}
58+
59+
return types.CheckpointRemoveOptions{
60+
Stdout: cmd.OutOrStdout(),
61+
GOptions: globalOptions,
62+
CheckpointDir: checkpointDir,
63+
}, nil
64+
}
65+
66+
func removeAction(cmd *cobra.Command, args []string) error {
67+
removeOptions, err := processRemoveFlags(cmd)
68+
if err != nil {
69+
return err
70+
}
71+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), removeOptions.GOptions.Namespace, removeOptions.GOptions.Address)
72+
if err != nil {
73+
return err
74+
}
75+
defer cancel()
76+
77+
err = checkpoint.Remove(ctx, client, args[0], args[1], removeOptions)
78+
if err != nil {
79+
return err
80+
}
81+
82+
return nil
83+
}
84+
85+
func removeShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
86+
return completion.ImageNames(cmd)
87+
}

pkg/api/types/checkpoint_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ type CheckpointListOptions struct {
3535
CheckpointDir string
3636
}
3737

38+
// CheckpointRemoveOptions specifies options for `nerdctl checkpoint rm`.
39+
type CheckpointRemoveOptions struct {
40+
Stdout io.Writer
41+
GOptions GlobalCommandOptions
42+
// Checkpoint directory
43+
CheckpointDir string
44+
}
3845
type CheckpointSummary struct {
3946
// Name is the name of the checkpoint.
4047
Name string

pkg/cmd/checkpoint/remove.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 Remove(ctx context.Context, client *containerd.Client, containerID string, checkpointName string, options types.CheckpointRemoveOptions) error {
32+
var container containerd.Container
33+
34+
walker := &containerwalker.ContainerWalker{
35+
Client: client,
36+
OnFound: func(ctx context.Context, found containerwalker.Found) error {
37+
if found.MatchCount > 1 {
38+
return fmt.Errorf("multiple containers found with provided prefix: %s", found.Req)
39+
}
40+
container = found.Container
41+
return nil
42+
},
43+
}
44+
45+
n, err := walker.Walk(ctx, containerID)
46+
if err != nil {
47+
return err
48+
} else if n == 0 {
49+
return fmt.Errorf("error removing checkpoint for container: %s, no such container", containerID)
50+
}
51+
52+
targetPath, err := checkpointutil.GetCheckpointDir(options.CheckpointDir, checkpointName, container.ID(), false)
53+
if err != nil {
54+
return err
55+
}
56+
57+
return os.RemoveAll(targetPath)
58+
}

0 commit comments

Comments
 (0)