Skip to content

Commit ee27cde

Browse files
authored
Merge pull request #5735 from ktock/diffcompression
Support custom compressor for walking differ
2 parents 6389fc7 + b483177 commit ee27cde

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

diff/diff.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package diff
1818

1919
import (
2020
"context"
21+
"io"
2122

2223
"github.com/containerd/containerd/mount"
2324
"github.com/gogo/protobuf/types"
@@ -37,6 +38,12 @@ type Config struct {
3738

3839
// Labels are the labels to apply to the generated content
3940
Labels map[string]string
41+
42+
// Compressor is a function to compress the diff stream
43+
// instead of the default gzip compressor. Differ passes
44+
// the MediaType of the target diff content to the compressor.
45+
// When using this config, MediaType must be specified as well.
46+
Compressor func(dest io.Writer, mediaType string) (io.WriteCloser, error)
4047
}
4148

4249
// Opt is used to configure a diff operation
@@ -71,6 +78,14 @@ type Applier interface {
7178
Apply(ctx context.Context, desc ocispec.Descriptor, mount []mount.Mount, opts ...ApplyOpt) (ocispec.Descriptor, error)
7279
}
7380

81+
// WithCompressor sets the function to be used to compress the diff stream.
82+
func WithCompressor(f func(dest io.Writer, mediaType string) (io.WriteCloser, error)) Opt {
83+
return func(c *Config) error {
84+
c.Compressor = f
85+
return nil
86+
}
87+
}
88+
7489
// WithMediaType sets the media type to use for creating the diff, without
7590
// specifying the differ will choose a default.
7691
func WithMediaType(m string) Opt {

diff/walking/differ.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,24 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
6565
}
6666
}
6767

68-
if config.MediaType == "" {
69-
config.MediaType = ocispec.MediaTypeImageLayerGzip
70-
}
71-
7268
var isCompressed bool
73-
switch config.MediaType {
74-
case ocispec.MediaTypeImageLayer:
75-
case ocispec.MediaTypeImageLayerGzip:
69+
if config.Compressor != nil {
70+
if config.MediaType == "" {
71+
return emptyDesc, errors.New("media type must be explicitly specified when using custom compressor")
72+
}
7673
isCompressed = true
77-
default:
78-
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", config.MediaType)
74+
} else {
75+
if config.MediaType == "" {
76+
config.MediaType = ocispec.MediaTypeImageLayerGzip
77+
}
78+
79+
switch config.MediaType {
80+
case ocispec.MediaTypeImageLayer:
81+
case ocispec.MediaTypeImageLayerGzip:
82+
isCompressed = true
83+
default:
84+
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", config.MediaType)
85+
}
7986
}
8087

8188
var ocidesc ocispec.Descriptor
@@ -118,9 +125,16 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
118125
if isCompressed {
119126
dgstr := digest.SHA256.Digester()
120127
var compressed io.WriteCloser
121-
compressed, errOpen = compression.CompressStream(cw, compression.Gzip)
122-
if errOpen != nil {
123-
return errors.Wrap(errOpen, "failed to get compressed stream")
128+
if config.Compressor != nil {
129+
compressed, errOpen = config.Compressor(cw, config.MediaType)
130+
if errOpen != nil {
131+
return errors.Wrap(errOpen, "failed to get compressed stream")
132+
}
133+
} else {
134+
compressed, errOpen = compression.CompressStream(cw, compression.Gzip)
135+
if errOpen != nil {
136+
return errors.Wrap(errOpen, "failed to get compressed stream")
137+
}
124138
}
125139
errOpen = archive.WriteDiff(ctx, io.MultiWriter(compressed, dgstr.Hash()), lowerRoot, upperRoot)
126140
compressed.Close()

0 commit comments

Comments
 (0)