Skip to content

Commit 6112618

Browse files
committed
add commit compression type support
Signed-off-by: rongfu.leng <rongfu.leng@daocloud.io>
1 parent 14c10b1 commit 6112618

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

cmd/nerdctl/container/container_commit.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package container
1818

1919
import (
20+
"errors"
2021
"github.com/spf13/cobra"
2122

2223
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
@@ -40,6 +41,7 @@ func CommitCommand() *cobra.Command {
4041
cmd.Flags().StringP("message", "m", "", "Commit message")
4142
cmd.Flags().StringArrayP("change", "c", nil, "Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])")
4243
cmd.Flags().BoolP("pause", "p", true, "Pause container during commit")
44+
cmd.Flags().StringP("compression", "", "gzip", "commit compression algorithm (zstd or gzip)")
4345
return cmd
4446
}
4547

@@ -66,13 +68,18 @@ func commitOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) {
6668
return types.ContainerCommitOptions{}, err
6769
}
6870

71+
com, err := cmd.Flags().GetString("compression")
72+
if err != nil {
73+
return types.ContainerCommitOptions{}, err
74+
}
6975
return types.ContainerCommitOptions{
70-
Stdout: cmd.OutOrStdout(),
71-
GOptions: globalOptions,
72-
Author: author,
73-
Message: message,
74-
Pause: pause,
75-
Change: change,
76+
Stdout: cmd.OutOrStdout(),
77+
GOptions: globalOptions,
78+
Author: author,
79+
Message: message,
80+
Pause: pause,
81+
Change: change,
82+
Compression: com,
7683
}, nil
7784

7885
}
@@ -82,7 +89,9 @@ func commitAction(cmd *cobra.Command, args []string) error {
8289
if err != nil {
8390
return err
8491
}
85-
92+
if ok, err := verifyOption(options); !ok {
93+
return err
94+
}
8695
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
8796
if err != nil {
8897
return err
@@ -98,3 +107,10 @@ func commitShellComplete(cmd *cobra.Command, args []string, toComplete string) (
98107
}
99108
return nil, cobra.ShellCompDirectiveNoFileComp
100109
}
110+
111+
func verifyOption(op types.ContainerCommitOptions) (bool, error) {
112+
if string(types.ZSTD) != op.Compression && string(types.GZIP) != op.Compression {
113+
return false, errors.New("--compression param only support zstd or gzip")
114+
}
115+
return true, nil
116+
}

docs/command-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ Flags:
759759
- :whale: `-m, --message`: Commit message
760760
- :whale: `-c, --change`: Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])
761761
- :whale: `-p, --pause`: Pause container during commit (default: true)
762+
- :nerd_face: `--compression`: Commit compression algorithm,(supported value: zstd or gzip) (default: gzip)
762763

763764
## Image management
764765

pkg/api/types/container_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,17 @@ type ContainerCommitOptions struct {
371371
Change []string
372372
// Pause container during commit
373373
Pause bool
374+
// Compression is set commit compression algorithm
375+
Compression string
374376
}
375377

378+
type CompressionType string
379+
380+
const (
381+
ZSTD CompressionType = "zstd"
382+
GZIP CompressionType = "gzip"
383+
)
384+
376385
// ContainerDiffOptions specifies options for `nerdctl (container) diff`.
377386
type ContainerDiffOptions struct {
378387
Stdout io.Writer

pkg/cmd/container/commit.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ func Commit(ctx context.Context, client *containerd.Client, rawRef string, req s
4444
}
4545

4646
opts := &commit.Opts{
47-
Author: options.Author,
48-
Message: options.Message,
49-
Ref: parsedReference.String(),
50-
Pause: options.Pause,
51-
Changes: changes,
47+
Author: options.Author,
48+
Message: options.Message,
49+
Ref: parsedReference.String(),
50+
Pause: options.Pause,
51+
Changes: changes,
52+
Compression: options.Compression,
5253
}
5354

5455
walker := &containerwalker.ContainerWalker{

pkg/imgutil/commit/commit.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ type Changes struct {
5757
}
5858

5959
type Opts struct {
60-
Author string
61-
Message string
62-
Ref string
63-
Pause bool
64-
Changes Changes
60+
Author string
61+
Message string
62+
Ref string
63+
Pause bool
64+
Changes Changes
65+
Compression string
6566
}
6667

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

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

358359
// createDiff creates a layer diff into containerd's content store.
359-
func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer) (ocispec.Descriptor, digest.Digest, error) {
360-
newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer)
360+
func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer, compression string) (ocispec.Descriptor, digest.Digest, error) {
361+
opts := make([]diff.Opt, 0)
362+
mediaType := images.MediaTypeDockerSchema2LayerGzip
363+
if compression == "zstd" {
364+
opts = append(opts, diff.WithMediaType(ocispec.MediaTypeImageLayerZstd))
365+
mediaType = images.MediaTypeDockerSchema2LayerZstd
366+
}
367+
newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer, opts...)
361368
if err != nil {
362369
return ocispec.Descriptor{}, digest.Digest(""), err
363370
}
@@ -378,7 +385,7 @@ func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs c
378385
}
379386

380387
return ocispec.Descriptor{
381-
MediaType: images.MediaTypeDockerSchema2LayerGzip,
388+
MediaType: mediaType,
382389
Digest: newDesc.Digest,
383390
Size: info.Size,
384391
}, diffID, nil

0 commit comments

Comments
 (0)