Skip to content

Commit

Permalink
Add image squash command
Browse files Browse the repository at this point in the history
Signed-off-by: weipeng <[email protected]>
  • Loading branch information
Your Name authored and weipeng committed Dec 12, 2024
1 parent 6f55896 commit b7756fe
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/nerdctl/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewImageCommand() *cobra.Command {
NewLoadCommand(),
NewSaveCommand(),
NewTagCommand(),
NewSquashCommand(),
imageRmCommand(),
newImageConvertCommand(),
newImageInspectCommand(),
Expand Down
102 changes: 102 additions & 0 deletions cmd/nerdctl/image/image_squash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
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 image

import (
"fmt"

"github.com/spf13/cobra"

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

func addSquashFlags(cmd *cobra.Command) {
cmd.Flags().IntP("layer-count", "c", 0, "The number of layers that can be compressed")
cmd.Flags().StringP("layer-digest", "d", "", "The digest of the layer to be compressed")
cmd.Flags().StringP("author", "a", "", `Author (e.g., "nerdctl contributor <[email protected]>")`)
cmd.Flags().StringP("message", "m", "", "Commit message")
}

func NewSquashCommand() *cobra.Command {
var squashCommand = &cobra.Command{
Use: "squash [flags] SOURCE_IMAGE TAG_IMAGE",
Short: "Compress the number of layers of the image",
Args: helpers.IsExactArgs(2),
RunE: squashAction,
SilenceUsage: true,
SilenceErrors: true,
}
addSquashFlags(squashCommand)
return squashCommand
}

func processSquashCommandFlags(cmd *cobra.Command, args []string) (options types.ImageSquashOptions, err error) {
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return options, err
}
layerCount, err := cmd.Flags().GetInt("layer-count")
if err != nil {
return options, err
}
layerDigest, err := cmd.Flags().GetString("layer-digest")
if err != nil {
return options, err
}
author, err := cmd.Flags().GetString("author")
if err != nil {
return options, err
}
message, err := cmd.Flags().GetString("message")
if err != nil {
return options, err
}

options = types.ImageSquashOptions{
GOptions: globalOptions,

Author: author,
Message: message,

SourceImageRef: args[0],
TargetImageName: args[1],

SquashLayerCount: layerCount,
SquashLayerDigest: layerDigest,
}
return options, nil
}

func squashAction(cmd *cobra.Command, args []string) error {
options, err := processSquashCommandFlags(cmd, args)
if err != nil {
return err
}
if !options.GOptions.Experimental {
return fmt.Errorf("squash is an experimental feature, please enable experimental mode")
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
if err != nil {
return err
}
defer cancel()

return image.Squash(ctx, client, options)
}
1 change: 1 addition & 0 deletions cmd/nerdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ Config file ($NERDCTL_TOML): %s
image.NewTagCommand(),
image.NewRmiCommand(),
image.NewHistoryCommand(),
image.NewSquashCommand(),
// #endregion

// #region System
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,23 @@ type SociOptions struct {
// Minimum layer size to build zTOC for. Smaller layers won't have zTOC and not lazy pulled. Default is 10 MiB.
MinLayerSize int64
}

// ImageSquashOptions specifies options for `nerdctl image squash`.
type ImageSquashOptions struct {
// GOptions is the global options
GOptions GlobalCommandOptions

// Author (e.g., "nerdctl contributor <[email protected]>")
Author string
// Commit message
Message string
// SourceImageRef is the image to be squashed
SourceImageRef string
// TargetImageName is the name of the squashed image
TargetImageName string

// SquashLayerCount is the number of layers to squash
SquashLayerCount int
// SquashLayerDigest is the digest of the layer to squash
SquashLayerDigest string
}
Loading

0 comments on commit b7756fe

Please sign in to comment.