Skip to content

Commit 52569a2

Browse files
committed
add commit compression type support
Signed-off-by: rongfu.leng <[email protected]>
1 parent 14c10b1 commit 52569a2

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

cmd/nerdctl/container/container_commit.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func CommitCommand() *cobra.Command {
4040
cmd.Flags().StringP("message", "m", "", "Commit message")
4141
cmd.Flags().StringArrayP("change", "c", nil, "Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])")
4242
cmd.Flags().BoolP("pause", "p", true, "Pause container during commit")
43+
cmd.Flags().StringP("compression", "", "", "commit compression algorithm,value is zstd or gzip, default is gzip")
4344
return cmd
4445
}
4546

@@ -66,13 +67,18 @@ func commitOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) {
6667
return types.ContainerCommitOptions{}, err
6768
}
6869

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

7884
}

docs/command-reference.md

+1
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+
- :whale: `--compression`: Commit compression algorithm,(supported value: zstd or gzip) (default: gzip)
762763

763764
## Image management
764765

pkg/api/types/container_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ 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

376378
// ContainerDiffOptions specifies options for `nerdctl (container) diff`.

pkg/cmd/container/commit.go

+6-5
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

+16-9
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)