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

[Feat] Add commit compression type support #4061

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions cmd/nerdctl/container/container_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package container

import (
"errors"

Check failure on line 20 in cmd/nerdctl/container/container_commit.go

View workflow job for this annotation

GitHub Actions / linux |

File is not properly formatted (gci)

Check failure on line 20 in cmd/nerdctl/container/container_commit.go

View workflow job for this annotation

GitHub Actions / freebsd |

File is not properly formatted (gci)

Check failure on line 20 in cmd/nerdctl/container/container_commit.go

View workflow job for this annotation

GitHub Actions / windows |

File is not properly formatted (gci)

Check failure on line 20 in cmd/nerdctl/container/container_commit.go

View workflow job for this annotation

GitHub Actions / linux | go-canary

File is not properly formatted (gci)
"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
Expand All @@ -40,6 +41,7 @@
cmd.Flags().StringP("message", "m", "", "Commit message")
cmd.Flags().StringArrayP("change", "c", nil, "Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])")
cmd.Flags().BoolP("pause", "p", true, "Pause container during commit")
cmd.Flags().StringP("compression", "", "gzip", "commit compression algorithm (zstd or gzip)")
return cmd
}

Expand All @@ -66,13 +68,18 @@
return types.ContainerCommitOptions{}, err
}

com, err := cmd.Flags().GetString("compression")
if err != nil {
return types.ContainerCommitOptions{}, err
}
return types.ContainerCommitOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Author: author,
Message: message,
Pause: pause,
Change: change,
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
Author: author,
Message: message,
Pause: pause,
Change: change,
Compression: com,
}, nil

}
Expand All @@ -82,7 +89,9 @@
if err != nil {
return err
}

if ok, err := verifyOption(options); !ok {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
if err != nil {
return err
Expand All @@ -98,3 +107,10 @@
}
return nil, cobra.ShellCompDirectiveNoFileComp
}

func verifyOption(op types.ContainerCommitOptions) (bool, error) {
if string(types.ZSTD) != op.Compression && string(types.GZIP) != op.Compression {
return false, errors.New("--compression param only support zstd or gzip")
}
return true, nil
}
1 change: 1 addition & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ Flags:
- :whale: `-m, --message`: Commit message
- :whale: `-c, --change`: Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])
- :whale: `-p, --pause`: Pause container during commit (default: true)
- :nerd_face: `--compression`: Commit compression algorithm,(supported value: zstd or gzip) (default: gzip)

## Image management

Expand Down
9 changes: 9 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package types

Check failure on line 17 in pkg/api/types/container_types.go

View workflow job for this annotation

GitHub Actions / linux |

max-public-structs: you have exceeded the maximum number (21) of public struct declarations (revive)

Check failure on line 17 in pkg/api/types/container_types.go

View workflow job for this annotation

GitHub Actions / freebsd |

max-public-structs: you have exceeded the maximum number (21) of public struct declarations (revive)

Check failure on line 17 in pkg/api/types/container_types.go

View workflow job for this annotation

GitHub Actions / windows |

max-public-structs: you have exceeded the maximum number (21) of public struct declarations (revive)

Check failure on line 17 in pkg/api/types/container_types.go

View workflow job for this annotation

GitHub Actions / linux | go-canary

max-public-structs: you have exceeded the maximum number (21) of public struct declarations (revive)

import (
"io"
Expand Down Expand Up @@ -371,8 +371,17 @@
Change []string
// Pause container during commit
Pause bool
// Compression is set commit compression algorithm
Compression string
}

type CompressionType string

const (
ZSTD CompressionType = "zstd"
GZIP CompressionType = "gzip"
)

// ContainerDiffOptions specifies options for `nerdctl (container) diff`.
type ContainerDiffOptions struct {
Stdout io.Writer
Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/container/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func Commit(ctx context.Context, client *containerd.Client, rawRef string, req s
}

opts := &commit.Opts{
Author: options.Author,
Message: options.Message,
Ref: parsedReference.String(),
Pause: options.Pause,
Changes: changes,
Author: options.Author,
Message: options.Message,
Ref: parsedReference.String(),
Pause: options.Pause,
Changes: changes,
Compression: options.Compression,
}

walker := &containerwalker.ContainerWalker{
Expand Down
25 changes: 16 additions & 9 deletions pkg/imgutil/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ type Changes struct {
}

type Opts struct {
Author string
Message string
Ref string
Pause bool
Changes Changes
Author string
Message string
Ref string
Pause bool
Changes Changes
Compression string
}

var (
Expand Down Expand Up @@ -176,7 +177,7 @@ func Commit(ctx context.Context, client *containerd.Client, container containerd
// Sync filesystem to make sure that all the data writes in container could be persisted to disk.
Sync()

diffLayerDesc, diffID, err := createDiff(ctx, id, sn, client.ContentStore(), differ)
diffLayerDesc, diffID, err := createDiff(ctx, id, sn, client.ContentStore(), differ, opts.Compression)
if err != nil {
return emptyDigest, fmt.Errorf("failed to export layer: %w", err)
}
Expand Down Expand Up @@ -356,8 +357,14 @@ func writeContentsForImage(ctx context.Context, snName string, baseImg container
}

// createDiff creates a layer diff into containerd's content store.
func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer) (ocispec.Descriptor, digest.Digest, error) {
newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer)
func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer, compression string) (ocispec.Descriptor, digest.Digest, error) {
opts := make([]diff.Opt, 0)
mediaType := images.MediaTypeDockerSchema2LayerGzip
if compression == "zstd" {
Copy link
Member

Choose a reason for hiding this comment

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

This should raise an error if the string is invalid

Copy link
Author

Choose a reason for hiding this comment

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

add verify this field compression value whether is gzip or zstd.

opts = append(opts, diff.WithMediaType(ocispec.MediaTypeImageLayerZstd))
mediaType = images.MediaTypeDockerSchema2LayerZstd
}
newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer, opts...)
if err != nil {
return ocispec.Descriptor{}, digest.Digest(""), err
}
Expand All @@ -378,7 +385,7 @@ func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs c
}

return ocispec.Descriptor{
MediaType: images.MediaTypeDockerSchema2LayerGzip,
MediaType: mediaType,
Digest: newDesc.Digest,
Size: info.Size,
}, diffID, nil
Expand Down
Loading