Skip to content

Commit 14d1009

Browse files
lingdieluanshaotongZllincdependabot[bot]AkihiroSuda
authored
v2.1.6 patch for devbox (#3)
* add option for remove top layer of baseimg * Add Snapshot (#1) * build(deps): bump github.com/go-viper/mapstructure/v2 Bumps [github.com/go-viper/mapstructure/v2](https://github.com/go-viper/mapstructure) from 2.3.0 to 2.4.0. - [Release notes](https://github.com/go-viper/mapstructure/releases) - [Changelog](https://github.com/go-viper/mapstructure/blob/main/CHANGELOG.md) - [Commits](go-viper/mapstructure@v2.3.0...v2.4.0) --- updated-dependencies: - dependency-name: github.com/go-viper/mapstructure/v2 dependency-version: 2.4.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * add snapshot label and snapshotter * fix --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp> Co-authored-by: Cunzili <cunzili@CunzilideMacBook-Pro.local> * add usage for snapshot * fix diff * fix diff layer parent --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: yy <56745951+lingdie@users.noreply.github.com> Co-authored-by: luanshaotong <luanshaotong@163.com> Co-authored-by: Cunzili <140624320+Zllinc@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp> Co-authored-by: Cunzili <cunzili@CunzilideMacBook-Pro.local>
1 parent 59253e9 commit 14d1009

File tree

18 files changed

+739
-6
lines changed

18 files changed

+739
-6
lines changed

cmd/nerdctl/container/container_commit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func CommitCommand() *cobra.Command {
5151
cmd.Flags().Bool("zstdchunked", false, "Convert the committed layer to zstd:chunked for lazy pulling")
5252
cmd.Flags().Int("zstdchunked-compression-level", 3, "zstd:chunked compression level")
5353
cmd.Flags().Int("zstdchunked-chunk-size", 0, "zstd:chunked chunk size")
54+
cmd.Flags().Bool("devbox-remove-layer", false, "Remove the top layer of the base image when committing a devbox container")
5455
return cmd
5556
}
5657

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

132+
removeBaseImageTopLayer, err := cmd.Flags().GetBool("devbox-remove-layer")
133+
if err != nil {
134+
return types.ContainerCommitOptions{}, err
135+
}
136+
131137
return types.ContainerCommitOptions{
132138
Stdout: cmd.OutOrStdout(),
133139
GOptions: globalOptions,
@@ -148,6 +154,9 @@ func commitOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) {
148154
ZstdChunkedCompressionLevel: zstdchunkedCompressionLevel,
149155
ZstdChunkedChunkSize: zstdchunkedChunkSize,
150156
},
157+
DevboxOptions: types.DevboxOptions{
158+
RemoveBaseImageTopLayer: removeBaseImageTopLayer,
159+
},
151160
}, nil
152161
}
153162

cmd/nerdctl/container/container_create.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package container
1919
import (
2020
"fmt"
2121
"runtime"
22+
"strings"
2223

2324
"github.com/spf13/cobra"
2425
cdiparser "tags.cncf.io/container-device-interface/pkg/parser"
@@ -413,6 +414,23 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
413414
if err != nil {
414415
return opt, err
415416
}
417+
418+
// Parse snapshot labels
419+
snapshotLabels, err := cmd.Flags().GetStringArray("snapshot-label")
420+
if err != nil {
421+
return opt, err
422+
}
423+
if len(snapshotLabels) > 0 {
424+
opt.SnapshotLabels = make(map[string]string)
425+
for _, label := range snapshotLabels {
426+
if key, value, ok := strings.Cut(label, "="); ok {
427+
opt.SnapshotLabels[key] = value
428+
} else {
429+
opt.SnapshotLabels[label] = ""
430+
}
431+
}
432+
}
433+
416434
opt.Annotations, err = cmd.Flags().GetStringArray("annotation")
417435
if err != nil {
418436
return opt, err

cmd/nerdctl/container/container_run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ func setCreateFlags(cmd *cobra.Command) {
261261
cmd.Flags().String("name", "", "Assign a name to the container")
262262
// label needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
263263
cmd.Flags().StringArrayP("label", "l", nil, "Set metadata on container")
264+
// snapshot-label needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
265+
cmd.Flags().StringArray("snapshot-label", nil, "Set metadata on snapshot")
264266
// annotation needs to be StringArray, not StringSlice, to prevent "foo=foo1,foo2" from being split to {"foo=foo1", "foo2"}
265267
cmd.Flags().StringArray("annotation", nil, "Add an annotation to the container (passed through to the OCI runtime)")
266268
cmd.RegisterFlagCompletionFunc("annotation", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cmd/nerdctl/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/containerd/nerdctl/v2/cmd/nerdctl/manifest"
4444
"github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace"
4545
"github.com/containerd/nerdctl/v2/cmd/nerdctl/network"
46+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/snapshot"
4647
"github.com/containerd/nerdctl/v2/cmd/nerdctl/system"
4748
"github.com/containerd/nerdctl/v2/cmd/nerdctl/volume"
4849
"github.com/containerd/nerdctl/v2/pkg/config"
@@ -331,6 +332,7 @@ Config file ($NERDCTL_TOML): %s
331332
system.Command(),
332333
namespace.Command(),
333334
builder.Command(),
335+
snapshot.Command(),
334336
// #endregion
335337

336338
// Internal

cmd/nerdctl/snapshot/snapshot.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 snapshot
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
23+
)
24+
25+
func Command() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Annotations: map[string]string{helpers.Category: helpers.Management},
28+
Use: "snapshot",
29+
Short: "Manage snapshots",
30+
RunE: helpers.UnknownSubcommandAction,
31+
SilenceUsage: true,
32+
SilenceErrors: true,
33+
}
34+
cmd.AddCommand(
35+
InfoCommand(),
36+
ListCommand(),
37+
UpdateCommand(),
38+
)
39+
return cmd
40+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 snapshot
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
23+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
24+
"github.com/containerd/nerdctl/v2/pkg/api/types"
25+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
26+
"github.com/containerd/nerdctl/v2/pkg/cmd/snapshot"
27+
)
28+
29+
func InfoCommand() *cobra.Command {
30+
var cmd = &cobra.Command{
31+
Use: "info [flags] SNAPSHOT_ID",
32+
Short: "Get detailed information about a snapshot",
33+
Args: helpers.IsExactArgs(1),
34+
RunE: infoAction,
35+
ValidArgsFunction: infoShellComplete,
36+
SilenceUsage: true,
37+
SilenceErrors: true,
38+
}
39+
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
40+
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
41+
return cmd
42+
}
43+
44+
func infoOptions(cmd *cobra.Command) (types.SnapshotInfoOptions, error) {
45+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
46+
if err != nil {
47+
return types.SnapshotInfoOptions{}, err
48+
}
49+
50+
snapshotter, err := cmd.Flags().GetString("snapshotter")
51+
if err != nil {
52+
return types.SnapshotInfoOptions{}, err
53+
}
54+
55+
return types.SnapshotInfoOptions{
56+
Stdout: cmd.OutOrStdout(),
57+
GOptions: globalOptions,
58+
Snapshotter: snapshotter,
59+
}, nil
60+
}
61+
62+
func infoAction(cmd *cobra.Command, args []string) error {
63+
options, err := infoOptions(cmd)
64+
if err != nil {
65+
return err
66+
}
67+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
68+
if err != nil {
69+
return err
70+
}
71+
defer cancel()
72+
73+
return snapshot.Info(ctx, client, args[0], options)
74+
}
75+
76+
func infoShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
77+
// TODO: We could potentially add snapshot ID completion here in the future
78+
return nil, cobra.ShellCompDirectiveNoFileComp
79+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 snapshot
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
23+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
24+
"github.com/containerd/nerdctl/v2/pkg/api/types"
25+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
26+
"github.com/containerd/nerdctl/v2/pkg/cmd/snapshot"
27+
)
28+
29+
func ListCommand() *cobra.Command {
30+
var cmd = &cobra.Command{
31+
Use: "list [flags]",
32+
Short: "List snapshots",
33+
Aliases: []string{"ls"},
34+
Args: helpers.IsExactArgs(0),
35+
RunE: listAction,
36+
SilenceUsage: true,
37+
SilenceErrors: true,
38+
}
39+
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
40+
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
41+
return cmd
42+
}
43+
44+
func listOptions(cmd *cobra.Command) (types.SnapshotListOptions, error) {
45+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
46+
if err != nil {
47+
return types.SnapshotListOptions{}, err
48+
}
49+
50+
snapshotter, err := cmd.Flags().GetString("snapshotter")
51+
if err != nil {
52+
return types.SnapshotListOptions{}, err
53+
}
54+
55+
return types.SnapshotListOptions{
56+
Stdout: cmd.OutOrStdout(),
57+
GOptions: globalOptions,
58+
Snapshotter: snapshotter,
59+
}, nil
60+
}
61+
62+
func listAction(cmd *cobra.Command, args []string) error {
63+
options, err := listOptions(cmd)
64+
if err != nil {
65+
return err
66+
}
67+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
68+
if err != nil {
69+
return err
70+
}
71+
defer cancel()
72+
73+
return snapshot.List(ctx, client, options)
74+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 snapshot
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
23+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
24+
"github.com/containerd/nerdctl/v2/pkg/api/types"
25+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
26+
"github.com/containerd/nerdctl/v2/pkg/cmd/snapshot"
27+
)
28+
29+
func UpdateCommand() *cobra.Command {
30+
var cmd = &cobra.Command{
31+
Use: "update [flags] SNAPSHOT_ID [LABEL=VALUE...]",
32+
Short: "Update snapshot metadata",
33+
Args: cobra.MinimumNArgs(1),
34+
RunE: updateAction,
35+
ValidArgsFunction: updateShellComplete,
36+
SilenceUsage: true,
37+
SilenceErrors: true,
38+
}
39+
cmd.Flags().StringP("snapshotter", "", "", "Snapshotter name (default: auto-detect)")
40+
cmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames)
41+
return cmd
42+
}
43+
44+
func updateOptions(cmd *cobra.Command) (types.SnapshotUpdateOptions, error) {
45+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
46+
if err != nil {
47+
return types.SnapshotUpdateOptions{}, err
48+
}
49+
50+
snapshotter, err := cmd.Flags().GetString("snapshotter")
51+
if err != nil {
52+
return types.SnapshotUpdateOptions{}, err
53+
}
54+
55+
return types.SnapshotUpdateOptions{
56+
Stdout: cmd.OutOrStdout(),
57+
GOptions: globalOptions,
58+
Snapshotter: snapshotter,
59+
}, nil
60+
}
61+
62+
func updateAction(cmd *cobra.Command, args []string) error {
63+
options, err := updateOptions(cmd)
64+
if err != nil {
65+
return err
66+
}
67+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
68+
if err != nil {
69+
return err
70+
}
71+
defer cancel()
72+
73+
snapshotID := args[0]
74+
labels := args[1:]
75+
return snapshot.Update(ctx, client, snapshotID, labels, options)
76+
}
77+
78+
func updateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
79+
// TODO: We could potentially add snapshot ID completion here in the future
80+
return nil, cobra.ShellCompDirectiveNoFileComp
81+
}

pkg/api/types/container_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ type ContainerCreateOptions struct {
301301

302302
// UserNS name for user namespace mapping of container
303303
UserNS string
304+
305+
// SnapshotLabels set snapshot's labels
306+
SnapshotLabels map[string]string
304307
}
305308

306309
// ContainerStopOptions specifies options for `nerdctl (container) stop`.
@@ -406,6 +409,8 @@ type ContainerCommitOptions struct {
406409
EstargzOptions
407410
// Embed ZstdChunkedOptions for zstd:chunked conversion options
408411
ZstdChunkedOptions
412+
// DevboxOptions for devbox specific options
413+
DevboxOptions
409414
}
410415

411416
type CompressionType string

pkg/api/types/image_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ type ImageConvertOptions struct {
7676
SociConvertOptions
7777
}
7878

79+
type DevboxOptions struct {
80+
// RemoveBaseImageTopLayer remove the top layer of the base image
81+
RemoveBaseImageTopLayer bool
82+
}
83+
7984
// EstargzOptions contains eStargz conversion options
8085
type EstargzOptions struct {
8186
// Estargz convert legacy tar(.gz) layers to eStargz for lazy pulling. Should be used in conjunction with '--oci'

0 commit comments

Comments
 (0)