Skip to content

Commit 3766d81

Browse files
committed
fix: address review commentsf from PR #677
Signed-off-by: jxr <[email protected]>
1 parent f3edb5f commit 3766d81

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

pkg/converter/constant.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ const (
2424
LayerAnnotationNydusSourceDigest = "containerd.io/snapshot/nydus-source-digest"
2525
LayerAnnotationNydusTargetDigest = "containerd.io/snapshot/nydus-target-digest"
2626

27-
LayerAnnotationOciSourceDigest = "containerd.io/snapshot/oci-source-digest"
28-
LayerAnnotationOciSourceReference = "containerd.io/snapshot/oci-source-reference"
29-
3027
LayerAnnotationNydusReferenceBlobIDs = "containerd.io/snapshot/nydus-reference-blob-ids"
3128

3229
LayerAnnotationUncompressed = "containerd.io/uncompressed"

pkg/converter/convert_unix.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,39 +1174,55 @@ func LayerReconvertFunc(opt UnpackOption) containerdReconverter.ConvertFunc {
11741174
if err != nil {
11751175
return nil, errors.Wrap(err, "open blob writer")
11761176
}
1177+
defer cw.Close()
11771178

11781179
var gw io.WriteCloser
11791180
var mediaType string
1180-
switch opt.Compressor {
1181+
compressor := opt.Compressor
1182+
if compressor == "" {
1183+
compressor = "gzip"
1184+
}
1185+
switch compressor {
11811186
case "gzip":
11821187
gw = gzip.NewWriter(cw)
11831188
mediaType = ocispec.MediaTypeImageLayerGzip
1184-
default:
1189+
case "zstd":
11851190
gw, err = zstd.NewWriter(cw)
11861191
if err != nil {
11871192
return nil, errors.Wrap(err, "create zstd writer")
11881193
}
11891194
mediaType = ocispec.MediaTypeImageLayerZstd
1195+
case "uncompressed":
1196+
gw = cw
1197+
mediaType = ocispec.MediaTypeImageLayer
1198+
default:
1199+
return nil, errors.Errorf("unsupported compressor type: %s (support: gzip, zstd, uncompressed)", opt.Compressor)
11901200
}
1191-
var data bytes.Buffer
1192-
writer := io.Writer(&data)
1201+
11931202
uncompressedDgster := digest.SHA256.Digester()
1203+
pr, pw := io.Pipe()
11941204

1195-
err = Unpack(ctx, ra, writer, opt)
1196-
if err != nil {
1197-
return nil, errors.Wrap(err, "unpack nydus to tar")
1198-
}
1205+
// Unpack nydus blob to pipe writer in background
1206+
go func() {
1207+
defer pw.Close()
1208+
if err := Unpack(ctx, ra, pw, opt); err != nil {
1209+
pw.CloseWithError(errors.Wrap(err, "unpack nydus to tar"))
1210+
}
1211+
}()
11991212

1213+
// Stream data from pipe reader to compressed writer and digester
12001214
compressed := io.MultiWriter(gw, uncompressedDgster.Hash())
1201-
// _, err = uncompressedDgster.Hash().Write(data.Bytes())
1202-
12031215
buffer := bufPool.Get().(*[]byte)
12041216
defer bufPool.Put(buffer)
1205-
if _, err = io.CopyBuffer(compressed, bytes.NewReader(data.Bytes()), *buffer); err != nil {
1206-
return nil, errors.Wrapf(err, "copy bootstrap targz into content store")
1217+
if _, err = io.CopyBuffer(compressed, pr, *buffer); err != nil {
1218+
return nil, errors.Wrapf(err, "copy to compressed writer")
12071219
}
1208-
if err = gw.Close(); err != nil {
1209-
return nil, errors.Wrap(err, "close gzip writer")
1220+
1221+
// Close compressor writer if different from content writer
1222+
if gw != cw {
1223+
if err = gw.Close(); err != nil {
1224+
return nil, errors.Wrap(err, "close compressor writer")
1225+
}
12101226
}
12111227

12121228
uncompressedDigest := uncompressedDgster.Digest()

pkg/converter/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ type UnpackOption struct {
143143
// but setup a http server to serve the blob data.
144144
Stream bool
145145
// Compressor specifies oci blob compression algorithm.
146+
// Supported values: "uncompressed" (no compression), "gzip", "zstd".
147+
// Defaults to "gzip" if not specified.
146148
Compressor string
147149
// Backend uploads blobs generated by oci-image builder to a backend storage.
148150
Backend Backend

0 commit comments

Comments
 (0)