Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cmd/nerdctl/checkpoint/checkpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkpoint

import (
"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
)

func Command() *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{helpers.Category: helpers.Management},
Use: "checkpoint",
Short: "Manage checkpoints.",
RunE: helpers.UnknownSubcommandAction,
SilenceUsage: true,
SilenceErrors: true,
}

cmd.AddCommand(
CreateCommand(),
checkpointLsCommand(),
)

return cmd
}

func checkpointLsCommand() *cobra.Command {
x := ListCommand()
x.Use = "ls"
x.Aliases = []string{"list"}
return x
}
91 changes: 91 additions & 0 deletions cmd/nerdctl/checkpoint/checkpoint_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkpoint

import (
"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint"
)

func CreateCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "create [OPTIONS] CONTAINER CHECKPOINT",
Short: "Create a checkpoint from a running container",
Args: cobra.ExactArgs(2),
RunE: createAction,
ValidArgsFunction: createShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().Bool("leave-running", false, "Leave the container running after checkpointing")
cmd.Flags().String("checkpoint-dir", "", "Checkpoint directory")
return cmd
}

func processCreateFlags(cmd *cobra.Command) (types.CheckpointCreateOptions, error) {
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return types.CheckpointCreateOptions{}, err
}

leaveRunning, err := cmd.Flags().GetBool("leave-running")
if err != nil {
return types.CheckpointCreateOptions{}, err
}
checkpointDir, err := cmd.Flags().GetString("checkpoint-dir")
if err != nil {
return types.CheckpointCreateOptions{}, err
}
if checkpointDir == "" {
checkpointDir = globalOptions.DataRoot + "/checkpoints"
}

return types.CheckpointCreateOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
LeaveRunning: leaveRunning,
CheckpointDir: checkpointDir,
}, nil
}

func createAction(cmd *cobra.Command, args []string) error {
createOptions, err := processCreateFlags(cmd)
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), createOptions.GOptions.Namespace, createOptions.GOptions.Address)
if err != nil {
return err
}
defer cancel()

err = checkpoint.Create(ctx, client, args[0], args[1], createOptions)
if err != nil {
return err
}

return nil
}

func createShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completion.ImageNames(cmd)
}
119 changes: 119 additions & 0 deletions cmd/nerdctl/checkpoint/checkpoint_create_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkpoint

import (
"errors"
"testing"

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

func TestCheckpointCreateErrors(t *testing.T) {
testCase := nerdtest.Setup()
testCase.SubTests = []*test.Case{
{
Description: "too-few-arguments",
Command: test.Command("checkpoint", "create", "too-few-arguments"),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
}
},
},
{
Description: "too-many-arguments",
Command: test.Command("checkpoint", "create", "too", "many", "arguments"),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
}
},
},
{
Description: "invalid-container-id",
Command: test.Command("checkpoint", "create", "foo", "bar"),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errors.New("error creating checkpoint for container: foo")},
}
},
},
}

testCase.Run(t)
}

func TestCheckpointCreate(t *testing.T) {
const (
checkpointName = "checkpoint-bar"
checkpointDir = "/dir/foo"
)
testCase := nerdtest.Setup()

testCase.SubTests = []*test.Case{
{
Description: "leave-running=true",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier("container-running"), testutil.CommonImage, "sleep", "infinity")
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Custom("rm", "-rf", checkpointDir).Run(&test.Expected{
ExitCode: expect.ExitCodeSuccess,
})
helpers.Anyhow("rm", "-f", data.Identifier("container-running"))
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("checkpoint", "create", "--leave-running", "--checkpoint-dir", checkpointDir, data.Identifier("container-running"), checkpointName)
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Output: expect.Equals(checkpointName + "\n"),
}
},
},
{
Description: "leave-running=false",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier("container-exit"), testutil.CommonImage, "sleep", "infinity")
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Custom("rm", "-rf", checkpointDir).Run(&test.Expected{
ExitCode: expect.ExitCodeSuccess,
})
helpers.Anyhow("rm", "-f", data.Identifier("container-exit"))
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("checkpoint", "create", "--checkpoint-dir", checkpointDir, data.Identifier("container-exit"), checkpointName)
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Output: expect.Equals(checkpointName + "\n"),
}
},
},
}

testCase.Run(t)
}
94 changes: 94 additions & 0 deletions cmd/nerdctl/checkpoint/checkpoint_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkpoint

import (
"fmt"
"text/tabwriter"

Check failure on line 22 in cmd/nerdctl/checkpoint/checkpoint_list.go

View workflow job for this annotation

GitHub Actions / go / linux

File is not properly formatted (gci)

Check failure on line 22 in cmd/nerdctl/checkpoint/checkpoint_list.go

View workflow job for this annotation

GitHub Actions / go / darwin

File is not properly formatted (gci)

Check failure on line 22 in cmd/nerdctl/checkpoint/checkpoint_list.go

View workflow job for this annotation

GitHub Actions / go / freebsd

File is not properly formatted (gci)

Check failure on line 22 in cmd/nerdctl/checkpoint/checkpoint_list.go

View workflow job for this annotation

GitHub Actions / go / windows

File is not properly formatted (gci)
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint"
"github.com/spf13/cobra"
)

func ListCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "list [OPTIONS] CONTAINER",
Short: "List checkpoints for a container",
Args: cobra.ExactArgs(1),
RunE: listAction,
ValidArgsFunction: listShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().String("checkpoint-dir", "", "Checkpoint directory")
return cmd
}

func processListFlags(cmd *cobra.Command) (types.CheckpointListOptions, error) {
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return types.CheckpointListOptions{}, err
}

checkpointDir, err := cmd.Flags().GetString("checkpoint-dir")
if err != nil {
return types.CheckpointListOptions{}, err
}
if checkpointDir == "" {
checkpointDir = globalOptions.DataRoot + "/checkpoints"
}

return types.CheckpointListOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
CheckpointDir: checkpointDir,
}, nil
}

func listAction(cmd *cobra.Command, args []string) error {
listOptions, err := processListFlags(cmd)
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), listOptions.GOptions.Namespace, listOptions.GOptions.Address)
if err != nil {
return err
}
defer cancel()

checkpoints, err := checkpoint.List(ctx, client, args[0], listOptions)
if err != nil {
return err
}

w := tabwriter.NewWriter(listOptions.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "CHECKPOINT NAME")

for _, cp := range checkpoints {
fmt.Fprintln(w, cp.Name)
}

return w.Flush()
}

func listShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completion.ImageNames(cmd)
}
27 changes: 27 additions & 0 deletions cmd/nerdctl/checkpoint/checkpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkpoint

import (
"testing"

"github.com/containerd/nerdctl/v2/pkg/testutil"
)

func TestMain(m *testing.M) {
testutil.M(m)
}
Loading
Loading