Skip to content

Commit 73f57ac

Browse files
dmcgowank8s-infra-cherrypick-robot
authored andcommitted
Update differ to handle zstd media types
The differ should be able to generate zstd compressed layers when provided with the zstd media type. Signed-off-by: Derek McGowan <[email protected]>
1 parent b4cab35 commit 73f57ac

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

pkg/archive/compression/compression.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const (
4545
Gzip
4646
// Zstd is zstd compression algorithm.
4747
Zstd
48+
// Unknown is used when a plugin handles the algorithm.
49+
Unknown
4850
)
4951

5052
const (
@@ -257,6 +259,8 @@ func (compression *Compression) Extension() string {
257259
return "gz"
258260
case Zstd:
259261
return "zst"
262+
case Unknown:
263+
return "unknown"
260264
}
261265
return ""
262266
}

plugins/diff/walking/differ.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ import (
2525
"io"
2626
"time"
2727

28+
"github.com/containerd/errdefs"
29+
"github.com/containerd/log"
30+
digest "github.com/opencontainers/go-digest"
31+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
32+
2833
"github.com/containerd/containerd/v2/core/content"
2934
"github.com/containerd/containerd/v2/core/diff"
3035
"github.com/containerd/containerd/v2/core/mount"
3136
"github.com/containerd/containerd/v2/pkg/archive"
3237
"github.com/containerd/containerd/v2/pkg/archive/compression"
3338
"github.com/containerd/containerd/v2/pkg/epoch"
3439
"github.com/containerd/containerd/v2/pkg/labels"
35-
"github.com/containerd/errdefs"
36-
"github.com/containerd/log"
37-
digest "github.com/opencontainers/go-digest"
38-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3940
)
4041

4142
type walkingDiff struct {
@@ -74,12 +75,12 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
7475
writeDiffOpts = append(writeDiffOpts, archive.WithSourceDateEpoch(config.SourceDateEpoch))
7576
}
7677

77-
var isCompressed bool
78+
compressionType := compression.Uncompressed
7879
if config.Compressor != nil {
7980
if config.MediaType == "" {
8081
return emptyDesc, errors.New("media type must be explicitly specified when using custom compressor")
8182
}
82-
isCompressed = true
83+
compressionType = compression.Unknown
8384
} else {
8485
if config.MediaType == "" {
8586
config.MediaType = ocispec.MediaTypeImageLayerGzip
@@ -88,7 +89,9 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
8889
switch config.MediaType {
8990
case ocispec.MediaTypeImageLayer:
9091
case ocispec.MediaTypeImageLayerGzip:
91-
isCompressed = true
92+
compressionType = compression.Gzip
93+
case ocispec.MediaTypeImageLayerZstd:
94+
compressionType = compression.Zstd
9295
default:
9396
return emptyDesc, fmt.Errorf("unsupported diff media type: %v: %w", config.MediaType, errdefs.ErrNotImplemented)
9497
}
@@ -131,7 +134,7 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
131134
}
132135
}
133136

134-
if isCompressed {
137+
if compressionType != compression.Uncompressed {
135138
dgstr := digest.SHA256.Digester()
136139
var compressed io.WriteCloser
137140
if config.Compressor != nil {
@@ -140,7 +143,7 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
140143
return fmt.Errorf("failed to get compressed stream: %w", errOpen)
141144
}
142145
} else {
143-
compressed, errOpen = compression.CompressStream(cw, compression.Gzip)
146+
compressed, errOpen = compression.CompressStream(cw, compressionType)
144147
if errOpen != nil {
145148
return fmt.Errorf("failed to get compressed stream: %w", errOpen)
146149
}

plugins/diff/windows/windows.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ import (
2828
"time"
2929

3030
"github.com/Microsoft/go-winio"
31+
"github.com/containerd/errdefs"
32+
"github.com/containerd/log"
33+
"github.com/containerd/platforms"
34+
"github.com/containerd/plugin"
35+
"github.com/containerd/plugin/registry"
36+
"github.com/opencontainers/go-digest"
37+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
38+
3139
"github.com/containerd/containerd/v2/core/content"
3240
"github.com/containerd/containerd/v2/core/diff"
3341
"github.com/containerd/containerd/v2/core/metadata"
@@ -37,13 +45,6 @@ import (
3745
"github.com/containerd/containerd/v2/pkg/epoch"
3846
"github.com/containerd/containerd/v2/pkg/labels"
3947
"github.com/containerd/containerd/v2/plugins"
40-
"github.com/containerd/errdefs"
41-
"github.com/containerd/log"
42-
"github.com/containerd/platforms"
43-
"github.com/containerd/plugin"
44-
"github.com/containerd/plugin/registry"
45-
"github.com/opencontainers/go-digest"
46-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
4748
)
4849

4950
func init() {
@@ -197,11 +198,13 @@ func (s windowsDiff) Compare(ctx context.Context, lower, upper []mount.Mount, op
197198
config.MediaType = ocispec.MediaTypeImageLayerGzip
198199
}
199200

200-
var isCompressed bool
201+
compressionType := compression.Uncompressed
201202
switch config.MediaType {
202203
case ocispec.MediaTypeImageLayer:
203204
case ocispec.MediaTypeImageLayerGzip:
204-
isCompressed = true
205+
compressionType = compression.Gzip
206+
case ocispec.MediaTypeImageLayerZstd:
207+
compressionType = compression.Zstd
205208
default:
206209
return emptyDesc, fmt.Errorf("unsupported diff media type: %v: %w", config.MediaType, errdefs.ErrNotImplemented)
207210
}
@@ -245,10 +248,10 @@ func (s windowsDiff) Compare(ctx context.Context, lower, upper []mount.Mount, op
245248
return emptyDesc, err
246249
}
247250

248-
if isCompressed {
251+
if compressionType != compression.Uncompressed {
249252
dgstr := digest.SHA256.Digester()
250253
var compressed io.WriteCloser
251-
compressed, err = compression.CompressStream(cw, compression.Gzip)
254+
compressed, err = compression.CompressStream(cw, compressionType)
252255
if err != nil {
253256
return emptyDesc, fmt.Errorf("failed to get compressed stream: %w", err)
254257
}

0 commit comments

Comments
 (0)