Merge pull request #5735 from ktock/diffcompression
Support custom compressor for walking differ
This commit is contained in:
commit
ee27cde735
15
diff/diff.go
15
diff/diff.go
@ -18,6 +18,7 @@ package diff
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/gogo/protobuf/types"
|
"github.com/gogo/protobuf/types"
|
||||||
@ -37,6 +38,12 @@ type Config struct {
|
|||||||
|
|
||||||
// Labels are the labels to apply to the generated content
|
// Labels are the labels to apply to the generated content
|
||||||
Labels map[string]string
|
Labels map[string]string
|
||||||
|
|
||||||
|
// Compressor is a function to compress the diff stream
|
||||||
|
// instead of the default gzip compressor. Differ passes
|
||||||
|
// the MediaType of the target diff content to the compressor.
|
||||||
|
// When using this config, MediaType must be specified as well.
|
||||||
|
Compressor func(dest io.Writer, mediaType string) (io.WriteCloser, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opt is used to configure a diff operation
|
// Opt is used to configure a diff operation
|
||||||
@ -71,6 +78,14 @@ type Applier interface {
|
|||||||
Apply(ctx context.Context, desc ocispec.Descriptor, mount []mount.Mount, opts ...ApplyOpt) (ocispec.Descriptor, error)
|
Apply(ctx context.Context, desc ocispec.Descriptor, mount []mount.Mount, opts ...ApplyOpt) (ocispec.Descriptor, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithCompressor sets the function to be used to compress the diff stream.
|
||||||
|
func WithCompressor(f func(dest io.Writer, mediaType string) (io.WriteCloser, error)) Opt {
|
||||||
|
return func(c *Config) error {
|
||||||
|
c.Compressor = f
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithMediaType sets the media type to use for creating the diff, without
|
// WithMediaType sets the media type to use for creating the diff, without
|
||||||
// specifying the differ will choose a default.
|
// specifying the differ will choose a default.
|
||||||
func WithMediaType(m string) Opt {
|
func WithMediaType(m string) Opt {
|
||||||
|
@ -65,17 +65,24 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.MediaType == "" {
|
|
||||||
config.MediaType = ocispec.MediaTypeImageLayerGzip
|
|
||||||
}
|
|
||||||
|
|
||||||
var isCompressed bool
|
var isCompressed bool
|
||||||
switch config.MediaType {
|
if config.Compressor != nil {
|
||||||
case ocispec.MediaTypeImageLayer:
|
if config.MediaType == "" {
|
||||||
case ocispec.MediaTypeImageLayerGzip:
|
return emptyDesc, errors.New("media type must be explicitly specified when using custom compressor")
|
||||||
|
}
|
||||||
isCompressed = true
|
isCompressed = true
|
||||||
default:
|
} else {
|
||||||
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", config.MediaType)
|
if config.MediaType == "" {
|
||||||
|
config.MediaType = ocispec.MediaTypeImageLayerGzip
|
||||||
|
}
|
||||||
|
|
||||||
|
switch config.MediaType {
|
||||||
|
case ocispec.MediaTypeImageLayer:
|
||||||
|
case ocispec.MediaTypeImageLayerGzip:
|
||||||
|
isCompressed = true
|
||||||
|
default:
|
||||||
|
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", config.MediaType)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ocidesc ocispec.Descriptor
|
var ocidesc ocispec.Descriptor
|
||||||
@ -118,9 +125,16 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
|
|||||||
if isCompressed {
|
if isCompressed {
|
||||||
dgstr := digest.SHA256.Digester()
|
dgstr := digest.SHA256.Digester()
|
||||||
var compressed io.WriteCloser
|
var compressed io.WriteCloser
|
||||||
compressed, errOpen = compression.CompressStream(cw, compression.Gzip)
|
if config.Compressor != nil {
|
||||||
if errOpen != nil {
|
compressed, errOpen = config.Compressor(cw, config.MediaType)
|
||||||
return errors.Wrap(errOpen, "failed to get compressed stream")
|
if errOpen != nil {
|
||||||
|
return errors.Wrap(errOpen, "failed to get compressed stream")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
compressed, errOpen = compression.CompressStream(cw, compression.Gzip)
|
||||||
|
if errOpen != nil {
|
||||||
|
return errors.Wrap(errOpen, "failed to get compressed stream")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
errOpen = archive.WriteDiff(ctx, io.MultiWriter(compressed, dgstr.Hash()), lowerRoot, upperRoot)
|
errOpen = archive.WriteDiff(ctx, io.MultiWriter(compressed, dgstr.Hash()), lowerRoot, upperRoot)
|
||||||
compressed.Close()
|
compressed.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user