| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package zstdchunked |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
|
| 24 |
"github.com/containerd/containerd/archive/compression" |
| 25 |
"github.com/containerd/containerd/content" |
| 26 |
"github.com/containerd/containerd/errdefs" |
| 27 |
"github.com/containerd/containerd/images" |
| 28 |
"github.com/containerd/containerd/images/converter" |
| 29 |
"github.com/containerd/containerd/images/converter/uncompress" |
| 30 |
"github.com/containerd/containerd/labels" |
| 31 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 32 |
"github.com/containerd/stargz-snapshotter/estargz/zstdchunked" |
| 33 |
"github.com/containerd/stargz-snapshotter/util/ioutils" |
| 34 |
"github.com/klauspost/compress/zstd" |
| 35 |
"github.com/opencontainers/go-digest" |
| 36 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 37 |
"github.com/sirupsen/logrus" |
| 38 |
) |
| 39 |
|
| 40 |
type zstdCompression struct { |
| 41 |
*zstdchunked.Decompressor |
| 42 |
*zstdchunked.Compressor |
| 43 |
} |
| 44 |
|
| 45 |
// LayerConvertWithLayerOptsFunc converts legacy tar.gz layers into zstd:chunked layers. |
| 46 |
// |
| 47 |
// This changes Docker MediaType to OCI MediaType so this should be used in |
| 48 |
// conjunction with WithDockerToOCI(). |
| 49 |
// See LayerConvertFunc for more details. The difference between this function and |
| 50 |
// LayerConvertFunc is that this allows to specify additional eStargz options per layer. |
| 51 |
func LayerConvertWithLayerOptsFunc(opts map[digest.Digest][]estargz.Option) converter.ConvertFunc { |
| 52 |
return LayerConvertWithLayerOptsFuncWithCompressionLevel(zstd.SpeedDefault, opts) |
| 53 |
} |
| 54 |
|
| 55 |
// LayerConvertFunc converts legacy tar.gz layers into zstd:chunked layers. |
| 56 |
// |
| 57 |
// This changes Docker MediaType to OCI MediaType so this should be used in |
| 58 |
// conjunction with WithDockerToOCI(). |
| 59 |
// |
| 60 |
// Otherwise "io.containers.zstd-chunked.manifest-checksum" annotation will be lost, |
| 61 |
// because the Docker media type does not support layer annotations. |
| 62 |
// |
| 63 |
// SpeedDefault (level 3) is used for the compression level. |
| 64 |
// See also: https://pkg.go.dev/github.com/klauspost/compress/zstd#EncoderLevel |
| 65 |
func LayerConvertFunc(opts ...estargz.Option) converter.ConvertFunc { |
| 66 |
return LayerConvertFuncWithCompressionLevel(zstd.SpeedDefault, opts...) |
| 67 |
} |
| 68 |
|
| 69 |
// LayerConvertWithLayerOptsFuncWithCompressionLevel converts legacy tar.gz layers into zstd:chunked layers. |
| 70 |
// This function allows to specify the compression level of zstd. |
| 71 |
// |
| 72 |
// This changes Docker MediaType to OCI MediaType so this should be used in |
| 73 |
// conjunction with WithDockerToOCI(). |
| 74 |
// See LayerConvertFunc for more details. The difference between this function and |
| 75 |
// LayerConvertFunc is that this allows to specify additional eStargz options per layer and |
| 76 |
// allows to specify the compression level. |
| 77 |
func LayerConvertWithLayerOptsFuncWithCompressionLevel(compressionLevel zstd.EncoderLevel, opts map[digest.Digest][]estargz.Option) converter.ConvertFunc { |
| 78 |
if opts == nil { |
| 79 |
return LayerConvertFuncWithCompressionLevel(compressionLevel) |
| 80 |
} |
| 81 |
return func(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) { |
| 82 |
// TODO: enable to speciy option per layer "index" because it's possible that there are |
| 83 |
// two layers having same digest in an image (but this should be rare case) |
| 84 |
return LayerConvertFuncWithCompressionLevel(compressionLevel, opts[desc.Digest]...)(ctx, cs, desc) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// LayerConvertFuncWithCompressionLevel converts legacy tar.gz layers into zstd:chunked layers with |
| 89 |
// the specified compression level. |
| 90 |
// |
| 91 |
// This changes Docker MediaType to OCI MediaType so this should be used in |
| 92 |
// conjunction with WithDockerToOCI(). |
| 93 |
// See LayerConvertFunc for more details. The difference between this function and |
| 94 |
// LayerConvertFunc is that this allows configuring the compression level. |
| 95 |
func LayerConvertFuncWithCompressionLevel(compressionLevel zstd.EncoderLevel, opts ...estargz.Option) converter.ConvertFunc { |
| 96 |
return func(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) { |
| 97 |
if !images.IsLayerType(desc.MediaType) { |
| 98 |
// No conversion. No need to return an error here. |
| 99 |
return nil, nil |
| 100 |
} |
| 101 |
uncompressedDesc := &desc |
| 102 |
// We need to uncompress the archive first |
| 103 |
if !uncompress.IsUncompressedType(desc.MediaType) { |
| 104 |
var err error |
| 105 |
uncompressedDesc, err = uncompress.LayerConvertFunc(ctx, cs, desc) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
if uncompressedDesc == nil { |
| 110 |
return nil, fmt.Errorf("unexpectedly got the same blob after compression (%s, %q)", desc.Digest, desc.MediaType) |
| 111 |
} |
| 112 |
defer func() { |
| 113 |
if err := cs.Delete(ctx, uncompressedDesc.Digest); err != nil { |
| 114 |
logrus.WithError(err).WithField("uncompressedDesc", uncompressedDesc).Warn("failed to remove tmp uncompressed layer") |
| 115 |
} |
| 116 |
}() |
| 117 |
logrus.Debugf("zstdchunked: uncompressed %s into %s", desc.Digest, uncompressedDesc.Digest) |
| 118 |
} |
| 119 |
|
| 120 |
info, err := cs.Info(ctx, desc.Digest) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
labelz := info.Labels |
| 125 |
if labelz == nil { |
| 126 |
labelz = make(map[string]string) |
| 127 |
} |
| 128 |
|
| 129 |
uncompressedReaderAt, err := cs.ReaderAt(ctx, *uncompressedDesc) |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
defer uncompressedReaderAt.Close() |
| 134 |
uncompressedSR := io.NewSectionReader(uncompressedReaderAt, 0, uncompressedDesc.Size) |
| 135 |
metadata := make(map[string]string) |
| 136 |
opts = append(opts, estargz.WithCompression(&zstdCompression{ |
| 137 |
new(zstdchunked.Decompressor), |
| 138 |
&zstdchunked.Compressor{ |
| 139 |
CompressionLevel: compressionLevel, |
| 140 |
Metadata: metadata, |
| 141 |
}, |
| 142 |
})) |
| 143 |
blob, err := estargz.Build(uncompressedSR, append(opts, estargz.WithContext(ctx))...) |
| 144 |
if err != nil { |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
defer blob.Close() |
| 148 |
ref := fmt.Sprintf("convert-zstdchunked-from-%s", desc.Digest) |
| 149 |
w, err := cs.Writer(ctx, content.WithRef(ref)) |
| 150 |
if err != nil { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
defer w.Close() |
| 154 |
|
| 155 |
// Reset the writing position |
| 156 |
// Old writer possibly remains without aborted |
| 157 |
// (e.g. conversion interrupted by a signal) |
| 158 |
if err := w.Truncate(0); err != nil { |
| 159 |
return nil, err |
| 160 |
} |
| 161 |
|
| 162 |
// Copy and count the contents |
| 163 |
pr, pw := io.Pipe() |
| 164 |
c := new(ioutils.CountWriter) |
| 165 |
doneCount := make(chan struct{}) |
| 166 |
go func() { |
| 167 |
defer close(doneCount) |
| 168 |
defer pr.Close() |
| 169 |
decompressR, err := compression.DecompressStream(pr) |
| 170 |
if err != nil { |
| 171 |
pr.CloseWithError(err) |
| 172 |
return |
| 173 |
} |
| 174 |
defer decompressR.Close() |
| 175 |
if _, err := io.Copy(c, decompressR); err != nil { |
| 176 |
pr.CloseWithError(err) |
| 177 |
return |
| 178 |
} |
| 179 |
}() |
| 180 |
n, err := io.Copy(w, io.TeeReader(blob, pw)) |
| 181 |
if err != nil { |
| 182 |
return nil, err |
| 183 |
} |
| 184 |
if err := blob.Close(); err != nil { |
| 185 |
return nil, err |
| 186 |
} |
| 187 |
// update diffID label |
| 188 |
labelz[labels.LabelUncompressed] = blob.DiffID().String() |
| 189 |
if err = w.Commit(ctx, n, "", content.WithLabels(labelz)); err != nil && !errdefs.IsAlreadyExists(err) { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
if err := w.Close(); err != nil { |
| 193 |
return nil, err |
| 194 |
} |
| 195 |
newDesc := desc |
| 196 |
newDesc.MediaType, err = convertMediaTypeToZstd(newDesc.MediaType) |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
newDesc.Digest = w.Digest() |
| 201 |
newDesc.Size = n |
| 202 |
if newDesc.Annotations == nil { |
| 203 |
newDesc.Annotations = make(map[string]string, 1) |
| 204 |
} |
| 205 |
tocDgst := blob.TOCDigest().String() |
| 206 |
newDesc.Annotations[estargz.TOCJSONDigestAnnotation] = tocDgst |
| 207 |
newDesc.Annotations[estargz.StoreUncompressedSizeAnnotation] = fmt.Sprintf("%d", c.Size()) |
| 208 |
if p, ok := metadata[zstdchunked.ManifestChecksumAnnotation]; ok { |
| 209 |
newDesc.Annotations[zstdchunked.ManifestChecksumAnnotation] = p |
| 210 |
} |
| 211 |
if p, ok := metadata[zstdchunked.ManifestPositionAnnotation]; ok { |
| 212 |
newDesc.Annotations[zstdchunked.ManifestPositionAnnotation] = p |
| 213 |
} |
| 214 |
return &newDesc, nil |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// NOTE: this converts docker mediatype to OCI mediatype |
| 219 |
func convertMediaTypeToZstd(mt string) (string, error) { |
| 220 |
ociMediaType := converter.ConvertDockerMediaTypeToOCI(mt) |
| 221 |
switch ociMediaType { |
| 222 |
case ocispec.MediaTypeImageLayer, ocispec.MediaTypeImageLayerGzip, ocispec.MediaTypeImageLayerZstd: |
| 223 |
return ocispec.MediaTypeImageLayerZstd, nil |
| 224 |
case ocispec.MediaTypeImageLayerNonDistributable, ocispec.MediaTypeImageLayerNonDistributableGzip, ocispec.MediaTypeImageLayerNonDistributableZstd: |
| 225 |
return ocispec.MediaTypeImageLayerNonDistributableZstd, nil |
| 226 |
default: |
| 227 |
return "", fmt.Errorf("unknown mediatype %q", mt) |
| 228 |
} |
| 229 |
} |