Skip to content
Merged
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
9 changes: 9 additions & 0 deletions cmd/nerdctl/container/container_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func CommitCommand() *cobra.Command {
cmd.Flags().Bool("zstdchunked", false, "Convert the committed layer to zstd:chunked for lazy pulling")
cmd.Flags().Int("zstdchunked-compression-level", 3, "zstd:chunked compression level")
cmd.Flags().Int("zstdchunked-chunk-size", 0, "zstd:chunked chunk size")
cmd.Flags().Bool("devbox-remove-layer", false, "Remove the top layer of the base image when committing a devbox container")
return cmd
}

Expand Down Expand Up @@ -128,6 +129,11 @@ func commitOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) {
return types.ContainerCommitOptions{}, errors.New("options --estargz and --zstdchunked lead to conflict, only one of them can be used")
}

removeBaseImageTopLayer, err := cmd.Flags().GetBool("devbox-remove-layer")
if err != nil {
return types.ContainerCommitOptions{}, err
}

return types.ContainerCommitOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Expand All @@ -148,6 +154,9 @@ func commitOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) {
ZstdChunkedCompressionLevel: zstdchunkedCompressionLevel,
ZstdChunkedChunkSize: zstdchunkedChunkSize,
},
DevboxOptions: types.DevboxOptions{
RemoveBaseImageTopLayer: removeBaseImageTopLayer,
},
}, nil
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/nerdctl/container/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package container
import (
"fmt"
"runtime"
"strings"

"github.com/spf13/cobra"
cdiparser "tags.cncf.io/container-device-interface/pkg/parser"
Expand Down Expand Up @@ -413,6 +414,23 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
if err != nil {
return opt, err
}

// Parse snapshot labels
snapshotLabels, err := cmd.Flags().GetStringArray("snapshot-label")
if err != nil {
return opt, err
}
if len(snapshotLabels) > 0 {
opt.SnapshotLabels = make(map[string]string)
for _, label := range snapshotLabels {
if key, value, ok := strings.Cut(label, "="); ok {
opt.SnapshotLabels[key] = value
} else {
opt.SnapshotLabels[label] = ""
}
}
}

opt.Annotations, err = cmd.Flags().GetStringArray("annotation")
if err != nil {
return opt, err
Expand Down
2 changes: 2 additions & 0 deletions cmd/nerdctl/container/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ func setCreateFlags(cmd *cobra.Command) {
cmd.Flags().String("name", "", "Assign a name to the container")
// label needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
cmd.Flags().StringArrayP("label", "l", nil, "Set metadata on container")
// snapshot-label needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
cmd.Flags().StringArray("snapshot-label", nil, "Set metadata on snapshot")
// annotation needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
cmd.Flags().StringArray("annotation", nil, "Add an annotation to the container (passed through to the OCI runtime)")
cmd.RegisterFlagCompletionFunc("annotation", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/nerdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/containerd/nerdctl/v2/cmd/nerdctl/manifest"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/network"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/snapshot"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/system"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/volume"
"github.com/containerd/nerdctl/v2/pkg/config"
Expand Down Expand Up @@ -331,6 +332,7 @@ Config file ($NERDCTL_TOML): %s
system.Command(),
namespace.Command(),
builder.Command(),
snapshot.Command(),
// #endregion

// Internal
Expand Down
40 changes: 40 additions & 0 deletions cmd/nerdctl/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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 snapshot

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: "snapshot",
Short: "Manage snapshots",
RunE: helpers.UnknownSubcommandAction,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.AddCommand(
InfoCommand(),
ListCommand(),
UpdateCommand(),
)
return cmd
}
79 changes: 79 additions & 0 deletions cmd/nerdctl/snapshot/snapshot_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 snapshot

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/snapshot"
)

func InfoCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "info [flags] SNAPSHOT_ID",
Short: "Get detailed information about a snapshot",
Args: helpers.IsExactArgs(1),
RunE: infoAction,
ValidArgsFunction: infoShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
return cmd
}

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

snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return types.SnapshotInfoOptions{}, err
}

return types.SnapshotInfoOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Snapshotter: snapshotter,
}, nil
}

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

return snapshot.Info(ctx, client, args[0], options)
}

func infoShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: We could potentially add snapshot ID completion here in the future
return nil, cobra.ShellCompDirectiveNoFileComp
}
74 changes: 74 additions & 0 deletions cmd/nerdctl/snapshot/snapshot_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 snapshot

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/snapshot"
)

func ListCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "list [flags]",
Short: "List snapshots",
Aliases: []string{"ls"},
Args: helpers.IsExactArgs(0),
RunE: listAction,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
return cmd
}

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

snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return types.SnapshotListOptions{}, err
}

return types.SnapshotListOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Snapshotter: snapshotter,
}, nil
}

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

return snapshot.List(ctx, client, options)
}
81 changes: 81 additions & 0 deletions cmd/nerdctl/snapshot/snapshot_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
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 snapshot

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/snapshot"
)

func UpdateCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "update [flags] SNAPSHOT_ID [LABEL=VALUE...]",
Short: "Update snapshot metadata",
Args: cobra.MinimumNArgs(1),
RunE: updateAction,
ValidArgsFunction: updateShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
return cmd
}

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

snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return types.SnapshotUpdateOptions{}, err
}

return types.SnapshotUpdateOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Snapshotter: snapshotter,
}, nil
}

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

snapshotID := args[0]
labels := args[1:]
return snapshot.Update(ctx, client, snapshotID, labels, options)
}

func updateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: We could potentially add snapshot ID completion here in the future
return nil, cobra.ShellCompDirectiveNoFileComp
}
5 changes: 5 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ type ContainerCreateOptions struct {

// UserNS name for user namespace mapping of container
UserNS string

// SnapshotLabels set snapshot's labels
SnapshotLabels map[string]string
}

// ContainerStopOptions specifies options for `nerdctl (container) stop`.
Expand Down Expand Up @@ -406,6 +409,8 @@ type ContainerCommitOptions struct {
EstargzOptions
// Embed ZstdChunkedOptions for zstd:chunked conversion options
ZstdChunkedOptions
// DevboxOptions for devbox specific options
DevboxOptions
}

type CompressionType string
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ type ImageConvertOptions struct {
SociConvertOptions
}

type DevboxOptions struct {
// RemoveBaseImageTopLayer remove the top layer of the base image
RemoveBaseImageTopLayer bool
}

// EstargzOptions contains eStargz conversion options
type EstargzOptions struct {
// Estargz convert legacy tar(.gz) layers to eStargz for lazy pulling. Should be used in conjunction with '--oci'
Expand Down
Loading