| 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 estargz |
| 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/util/ioutils" |
| 33 |
"github.com/opencontainers/go-digest" |
| 34 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 35 |
) |
| 36 |
|
| 37 |
// LayerConvertWithLayerAndCommonOptsFunc converts legacy tar.gz layers into eStargz tar.gz |
| 38 |
// layers. Media type is unchanged. Should be used in conjunction with WithDockerToOCI(). See |
| 39 |
// LayerConvertFunc for more details. The difference between this function and |
| 40 |
// LayerConvertFunc is that this allows to specify additional eStargz options per layer. |
| 41 |
func LayerConvertWithLayerAndCommonOptsFunc(opts map[digest.Digest][]estargz.Option, commonOpts ...estargz.Option) converter.ConvertFunc { |
| 42 |
if opts == nil { |
| 43 |
return LayerConvertFunc(commonOpts...) |
| 44 |
} |
| 45 |
return func(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) { |
| 46 |
// TODO: enable to speciy option per layer "index" because it's possible that there are |
| 47 |
// two layers having same digest in an image (but this should be rare case) |
| 48 |
return LayerConvertFunc(append(commonOpts, opts[desc.Digest]...)...)(ctx, cs, desc) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// LayerConvertFunc converts legacy tar.gz layers into eStargz tar.gz layers. |
| 53 |
// Media type is unchanged. |
| 54 |
// |
| 55 |
// Should be used in conjunction with WithDockerToOCI(). |
| 56 |
// |
| 57 |
// Otherwise "containerd.io/snapshot/stargz/toc.digest" annotation will be lost, |
| 58 |
// because the Docker media type does not support layer annotations. |
| 59 |
func LayerConvertFunc(opts ...estargz.Option) converter.ConvertFunc { |
| 60 |
return func(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) { |
| 61 |
if !images.IsLayerType(desc.MediaType) { |
| 62 |
// No conversion. No need to return an error here. |
| 63 |
return nil, nil |
| 64 |
} |
| 65 |
info, err := cs.Info(ctx, desc.Digest) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
labelz := info.Labels |
| 70 |
if labelz == nil { |
| 71 |
labelz = make(map[string]string) |
| 72 |
} |
| 73 |
|
| 74 |
ra, err := cs.ReaderAt(ctx, desc) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
defer ra.Close() |
| 79 |
sr := io.NewSectionReader(ra, 0, desc.Size) |
| 80 |
blob, err := estargz.Build(sr, append(opts, estargz.WithContext(ctx))...) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
defer blob.Close() |
| 85 |
ref := fmt.Sprintf("convert-estargz-from-%s", desc.Digest) |
| 86 |
w, err := content.OpenWriter(ctx, cs, content.WithRef(ref)) |
| 87 |
if err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
defer w.Close() |
| 91 |
|
| 92 |
// Reset the writing position |
| 93 |
// Old writer possibly remains without aborted |
| 94 |
// (e.g. conversion interrupted by a signal) |
| 95 |
if err := w.Truncate(0); err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
// Copy and count the contents |
| 100 |
pr, pw := io.Pipe() |
| 101 |
c := new(ioutils.CountWriter) |
| 102 |
doneCount := make(chan struct{}) |
| 103 |
go func() { |
| 104 |
defer close(doneCount) |
| 105 |
defer pr.Close() |
| 106 |
decompressR, err := compression.DecompressStream(pr) |
| 107 |
if err != nil { |
| 108 |
pr.CloseWithError(err) |
| 109 |
return |
| 110 |
} |
| 111 |
defer decompressR.Close() |
| 112 |
if _, err := io.Copy(c, decompressR); err != nil { |
| 113 |
pr.CloseWithError(err) |
| 114 |
return |
| 115 |
} |
| 116 |
}() |
| 117 |
n, err := io.Copy(w, io.TeeReader(blob, pw)) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
if err := blob.Close(); err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
if err := pw.Close(); err != nil { |
| 125 |
return nil, err |
| 126 |
} |
| 127 |
<-doneCount |
| 128 |
|
| 129 |
// update diffID label |
| 130 |
labelz[labels.LabelUncompressed] = blob.DiffID().String() |
| 131 |
if err = w.Commit(ctx, n, "", content.WithLabels(labelz)); err != nil && !errdefs.IsAlreadyExists(err) { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
if err := w.Close(); err != nil { |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
newDesc := desc |
| 138 |
if uncompress.IsUncompressedType(newDesc.MediaType) { |
| 139 |
if images.IsDockerType(newDesc.MediaType) { |
| 140 |
newDesc.MediaType += ".gzip" |
| 141 |
} else { |
| 142 |
newDesc.MediaType += "+gzip" |
| 143 |
} |
| 144 |
} |
| 145 |
newDesc.Digest = w.Digest() |
| 146 |
newDesc.Size = n |
| 147 |
if newDesc.Annotations == nil { |
| 148 |
newDesc.Annotations = make(map[string]string, 1) |
| 149 |
} |
| 150 |
newDesc.Annotations[estargz.TOCJSONDigestAnnotation] = blob.TOCDigest().String() |
| 151 |
newDesc.Annotations[estargz.StoreUncompressedSizeAnnotation] = fmt.Sprintf("%d", c.Size()) |
| 152 |
return &newDesc, nil |
| 153 |
} |
| 154 |
} |