Skip to content

Commit b7756fe

Browse files
Your Nameweipeng
Your Name
authored and
weipeng
committed
Add image squash command
Signed-off-by: weipeng <[email protected]>
1 parent 6f55896 commit b7756fe

File tree

5 files changed

+562
-0
lines changed

5 files changed

+562
-0
lines changed

cmd/nerdctl/image/image.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func NewImageCommand() *cobra.Command {
4242
NewLoadCommand(),
4343
NewSaveCommand(),
4444
NewTagCommand(),
45+
NewSquashCommand(),
4546
imageRmCommand(),
4647
newImageConvertCommand(),
4748
newImageInspectCommand(),

cmd/nerdctl/image/image_squash.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 image
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
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/image"
28+
)
29+
30+
func addSquashFlags(cmd *cobra.Command) {
31+
cmd.Flags().IntP("layer-count", "c", 0, "The number of layers that can be compressed")
32+
cmd.Flags().StringP("layer-digest", "d", "", "The digest of the layer to be compressed")
33+
cmd.Flags().StringP("author", "a", "", `Author (e.g., "nerdctl contributor <[email protected]>")`)
34+
cmd.Flags().StringP("message", "m", "", "Commit message")
35+
}
36+
37+
func NewSquashCommand() *cobra.Command {
38+
var squashCommand = &cobra.Command{
39+
Use: "squash [flags] SOURCE_IMAGE TAG_IMAGE",
40+
Short: "Compress the number of layers of the image",
41+
Args: helpers.IsExactArgs(2),
42+
RunE: squashAction,
43+
SilenceUsage: true,
44+
SilenceErrors: true,
45+
}
46+
addSquashFlags(squashCommand)
47+
return squashCommand
48+
}
49+
50+
func processSquashCommandFlags(cmd *cobra.Command, args []string) (options types.ImageSquashOptions, err error) {
51+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
52+
if err != nil {
53+
return options, err
54+
}
55+
layerCount, err := cmd.Flags().GetInt("layer-count")
56+
if err != nil {
57+
return options, err
58+
}
59+
layerDigest, err := cmd.Flags().GetString("layer-digest")
60+
if err != nil {
61+
return options, err
62+
}
63+
author, err := cmd.Flags().GetString("author")
64+
if err != nil {
65+
return options, err
66+
}
67+
message, err := cmd.Flags().GetString("message")
68+
if err != nil {
69+
return options, err
70+
}
71+
72+
options = types.ImageSquashOptions{
73+
GOptions: globalOptions,
74+
75+
Author: author,
76+
Message: message,
77+
78+
SourceImageRef: args[0],
79+
TargetImageName: args[1],
80+
81+
SquashLayerCount: layerCount,
82+
SquashLayerDigest: layerDigest,
83+
}
84+
return options, nil
85+
}
86+
87+
func squashAction(cmd *cobra.Command, args []string) error {
88+
options, err := processSquashCommandFlags(cmd, args)
89+
if err != nil {
90+
return err
91+
}
92+
if !options.GOptions.Experimental {
93+
return fmt.Errorf("squash is an experimental feature, please enable experimental mode")
94+
}
95+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
96+
if err != nil {
97+
return err
98+
}
99+
defer cancel()
100+
101+
return image.Squash(ctx, client, options)
102+
}

cmd/nerdctl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ Config file ($NERDCTL_TOML): %s
295295
image.NewTagCommand(),
296296
image.NewRmiCommand(),
297297
image.NewHistoryCommand(),
298+
image.NewSquashCommand(),
298299
// #endregion
299300

300301
// #region System

pkg/api/types/image_types.go

+20
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,23 @@ type SociOptions struct {
287287
// Minimum layer size to build zTOC for. Smaller layers won't have zTOC and not lazy pulled. Default is 10 MiB.
288288
MinLayerSize int64
289289
}
290+
291+
// ImageSquashOptions specifies options for `nerdctl image squash`.
292+
type ImageSquashOptions struct {
293+
// GOptions is the global options
294+
GOptions GlobalCommandOptions
295+
296+
// Author (e.g., "nerdctl contributor <[email protected]>")
297+
Author string
298+
// Commit message
299+
Message string
300+
// SourceImageRef is the image to be squashed
301+
SourceImageRef string
302+
// TargetImageName is the name of the squashed image
303+
TargetImageName string
304+
305+
// SquashLayerCount is the number of layers to squash
306+
SquashLayerCount int
307+
// SquashLayerDigest is the digest of the layer to squash
308+
SquashLayerDigest string
309+
}

0 commit comments

Comments
 (0)