-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add image squash command #3756
Open
weapons97
wants to merge
2
commits into
containerd:main
Choose a base branch
from
weapons97:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add image squash command #3756
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,97 @@ | ||
/* | ||
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("last-n-layer", "n", 0, "The number of specify squashing the last N (N=layer-count) layers") | ||
cmd.Flags().StringP("author", "a", "nerdctl", `Author (e.g., "nerdctl contributor <[email protected]>")`) | ||
cmd.Flags().StringP("message", "m", "generated by nerdctl squash", "Commit message") | ||
} | ||
|
||
// NewSquashCommand returns a new `squash` command to compress the number of layers of the image | ||
func NewSquashCommand() *cobra.Command { | ||
var squashCommand = &cobra.Command{ | ||
Use: "squash [flags] SOURCE_IMAGE TARGET_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 | ||
} | ||
layerN, err := cmd.Flags().GetInt("last-n-layer") | ||
if err != nil { | ||
return options, err | ||
} | ||
Comment on lines
+55
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a check to make sure |
||
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], | ||
|
||
SquashLayerLastN: layerN, | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
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" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/testutil" | ||
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" | ||
"github.com/containerd/nerdctl/v2/pkg/testutil/test" | ||
) | ||
|
||
func squashIdentifierName(identifier string) string { | ||
return fmt.Sprintf("%s-squash", identifier) | ||
} | ||
|
||
func secondCommitedIdentifierName(identifier string) string { | ||
return fmt.Sprintf("%s-second", identifier) | ||
} | ||
|
||
func TestSquash(t *testing.T) { | ||
testCase := nerdtest.Setup() | ||
|
||
require := test.Require( | ||
test.Not(nerdtest.Docker), | ||
nerdtest.CGroup, | ||
) | ||
|
||
testCase.SubTests = []*test.Case{ | ||
{ | ||
Description: "by last-n-layer", | ||
Require: require, | ||
NoParallel: true, | ||
Cleanup: func(data test.Data, helpers test.Helpers) { | ||
identifier := data.Identifier() | ||
secondIdentifier := secondCommitedIdentifierName(identifier) | ||
squashIdentifier := squashIdentifierName(identifier) | ||
helpers.Anyhow("rm", "-f", identifier) | ||
helpers.Anyhow("rm", "-f", secondIdentifier) | ||
helpers.Anyhow("rm", "-f", squashIdentifier) | ||
|
||
helpers.Anyhow("rmi", "-f", secondIdentifier) | ||
helpers.Anyhow("rmi", "-f", identifier) | ||
helpers.Anyhow("rmi", "-f", squashIdentifier) | ||
helpers.Anyhow("image", "prune", "-f") | ||
}, | ||
Setup: func(data test.Data, helpers test.Helpers) { | ||
identifier := data.Identifier() | ||
helpers.Ensure("run", "-d", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity) | ||
helpers.Ensure("exec", identifier, "sh", "-euxc", `echo hello-first-commit > /foo`) | ||
helpers.Ensure("commit", "-c", `CMD ["cat", "/foo"]`, "-m", `first commit`, "--pause=true", identifier, identifier) | ||
out := helpers.Capture("run", "--rm", identifier) | ||
assert.Equal(t, out, "hello-first-commit\n") | ||
|
||
secondIdentifier := secondCommitedIdentifierName(identifier) | ||
helpers.Ensure("run", "-d", "--name", secondIdentifier, identifier, "sleep", nerdtest.Infinity) | ||
helpers.Ensure("exec", secondIdentifier, "sh", "-euxc", `echo hello-second-commit > /bar && echo hello-squash-commit > /foo`) | ||
helpers.Ensure("commit", "-c", `CMD ["cat", "/foo", "/bar"]`, "-m", `second commit`, "--pause=true", secondIdentifier, secondIdentifier) | ||
out = helpers.Capture("run", "--rm", secondIdentifier) | ||
assert.Equal(t, out, "hello-squash-commit\nhello-second-commit\n") | ||
|
||
squashIdentifier := squashIdentifierName(identifier) | ||
helpers.Ensure("image", "squash", "-n", "2", "-m", "squash commit", secondIdentifier, squashIdentifier) | ||
out = helpers.Capture("run", "--rm", squashIdentifier) | ||
assert.Equal(t, out, "hello-squash-commit\nhello-second-commit\n") | ||
}, | ||
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
identifier := data.Identifier() | ||
|
||
squashIdentifier := squashIdentifierName(identifier) | ||
return helpers.Command("image", "history", "--human=true", "--format=json", squashIdentifier) | ||
}, | ||
Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) { | ||
history, err := decode(stdout) | ||
assert.NilError(t, err, info) | ||
assert.Equal(t, len(history), 3, info) | ||
assert.Equal(t, history[0].Comment, "squash commit", info) | ||
}), | ||
}, | ||
} | ||
|
||
testCase.Run(t) | ||
} |
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 |
---|---|---|
|
@@ -290,3 +290,21 @@ 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 | ||
|
||
// SquashLayerLastN is the number of layers to squash | ||
SquashLayerLastN int | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs docs and tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,I will add it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added, please check it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/containerd/nerdctl/blob/main/docs/command-reference.md needs to be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated, please check it out