Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add image squash command #3756

wants to merge 2 commits into from

Conversation

weapons97
Copy link

This pr adds the squash image command.

The related issue is #3252.

@weapons97 weapons97 force-pushed the main branch 4 times, most recently from b7756fe to 9599d0a Compare December 13, 2024 01:14
cmd.Flags().StringP("message", "m", "", "Commit message")
}

func NewSquashCommand() *cobra.Command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs docs and tests

Copy link
Author

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.

Copy link
Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

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

@@ -295,6 +295,7 @@ Config file ($NERDCTL_TOML): %s
image.NewTagCommand(),
image.NewRmiCommand(),
image.NewHistoryCommand(),
image.NewSquashCommand(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of inflating the number of the top-level commands

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted

}
}

// copied from github.com/containerd/containerd/rootfs/apply.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a permanent URL with the git tag (or the commit hash)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit hash has been added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: weipeng <[email protected]>
// 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 TAG_IMAGE",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: TAG_IMAGE -> TARGET_IMAGE

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had changed

testCase := nerdtest.Setup()

require := test.Require(
test.Linux,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this Linux only?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i deleted

)

func addSquashFlags(cmd *cobra.Command) {
cmd.Flags().IntP("layer-count", "c", 0, "The number of layers that can be compressed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this flag specify squashing the last N (N=layer-count) layers? If so I suggest renaming it to last-n-layer.

Or rename it to from-layer, meaning squashing all layers starting from from-layer (and --from-layer=0 means squash all layers)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I have changed it to last-n-layer.


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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this flag?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed unnecessary, so I have removed this flag.

Comment on lines 33 to 34
cmd.Flags().StringP("author", "a", "", `Author (e.g., "nerdctl contributor <[email protected]>")`)
cmd.Flags().StringP("message", "m", "", "Commit message")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe use something like author="nerdctl" and message="generated by nerdctl squash" as default values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, changed

@@ -994,6 +994,25 @@ Flags:
- `--platform=<PLATFORM>` : Convert content for a specific platform
- `--all-platforms` : Convert content for all platforms (default: false)

### :nerd_face: nerdctl image squash

Squash an image layers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to add some details, e.g., Squash all layers starting from from-layer into a single layer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, changed

}
}

// copied from github.com/containerd/containerd/rootfs/apply.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +55 to +58
layerN, err := cmd.Flags().GetInt("last-n-layer")
if err != nil {
return options, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a check to make sure layerN > 0?

Comment on lines +322 to +324
if matchCount < 1 {
return fmt.Errorf("%s: not found", option.SourceImageRef)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the behavior if matchCount > 1?

return err
}
remainingLayerCount := len(img.manifest.Layers) - len(sLayers)
// Don't gc me and clean the dirty data after 1 hour!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 1 hour? after squash we should just remove the temp data and keep the final new data forever?

}

clientImage := containerd.NewImage(sr.client, containerImage)
manifest, _, err := imgutil.ReadManifest(ctx, clientImage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we pass in an image index instead of manifest? seems index is not supported? can we add a check to return an error if the image is not a manifest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants