-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: weipeng <[email protected]>
- Loading branch information
Your Name
authored and
weipeng
committed
Dec 12, 2024
1 parent
6f55896
commit b7756fe
Showing
5 changed files
with
562 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} |
Oops, something went wrong.