|
1 | 1 | package estargz
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "strings"
|
6 | 5 |
|
7 |
| - ctdlabels "github.com/containerd/containerd/v2/pkg/labels" |
8 |
| - "github.com/containerd/stargz-snapshotter/estargz" |
| 6 | + "github.com/containerd/containerd/v2/pkg/labels" |
9 | 7 | ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
10 | 8 | )
|
11 | 9 |
|
| 10 | +const ( |
| 11 | + // targetRefLabel is a label which contains image reference. |
| 12 | + // |
| 13 | + // It is a copy of [stargz-snapshotter/fs/source.targetRefLabel]. |
| 14 | + // |
| 15 | + // [stargz-snapshotter/fs/source.targetRefLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L64-L65 |
| 16 | + targetRefLabel = "containerd.io/snapshot/remote/stargz.reference" |
| 17 | + |
| 18 | + // targetDigestLabel is a label which contains layer digest. |
| 19 | + // |
| 20 | + // It is a copy of [stargz-snapshotter/fs/source.targetDigestLabel]. |
| 21 | + // |
| 22 | + // [stargz-snapshotter/fs/source.targetDigestLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L67-L68 |
| 23 | + targetDigestLabel = "containerd.io/snapshot/remote/stargz.digest" |
| 24 | + |
| 25 | + // targetImageLayersLabel is a label which contains layer digests contained in |
| 26 | + // the target image. |
| 27 | + // |
| 28 | + // It is a copy of [stargz-snapshotter/fs/source.targetImageLayersLabel]. |
| 29 | + // |
| 30 | + // [stargz-snapshotter/fs/source.targetImageLayersLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L70-L72 |
| 31 | + targetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + // TOCJSONDigestAnnotation is an annotation for an image layer. This stores the |
| 36 | + // digest of the TOC JSON. |
| 37 | + // This annotation is valid only when it is specified in `.[]layers.annotations` |
| 38 | + // of an image manifest. |
| 39 | + // |
| 40 | + // This is a copy of [estargz.TOCJSONDigestAnnotation] |
| 41 | + // |
| 42 | + // [estargz.TOCJSONDigestAnnotation]: https://pkg.go.dev/github.com/containerd/stargz-snapshotter/[email protected]#TOCJSONDigestAnnotation |
| 43 | + TOCJSONDigestAnnotation = "containerd.io/snapshot/stargz/toc.digest" |
| 44 | + |
| 45 | + // StoreUncompressedSizeAnnotation is an additional annotation key for eStargz to enable lazy |
| 46 | + // pulling on containers/storage. Stargz Store is required to expose the layer's uncompressed size |
| 47 | + // to the runtime but current OCI image doesn't ship this information by default. So we store this |
| 48 | + // to the special annotation. |
| 49 | + // |
| 50 | + // This is a copy of [estargz.StoreUncompressedSizeAnnotation] |
| 51 | + // |
| 52 | + // [estargz.StoreUncompressedSizeAnnotation]: https://pkg.go.dev/github.com/containerd/stargz-snapshotter/[email protected]#StoreUncompressedSizeAnnotation |
| 53 | + StoreUncompressedSizeAnnotation = "io.containers.estargz.uncompressed-size" |
| 54 | +) |
| 55 | + |
12 | 56 | func SnapshotLabels(ref string, descs []ocispecs.Descriptor, targetIndex int) map[string]string {
|
13 | 57 | if len(descs) < targetIndex {
|
14 | 58 | return nil
|
15 | 59 | }
|
16 | 60 | desc := descs[targetIndex]
|
17 |
| - labels := make(map[string]string) |
18 |
| - for _, k := range []string{estargz.TOCJSONDigestAnnotation, estargz.StoreUncompressedSizeAnnotation} { |
19 |
| - labels[k] = desc.Annotations[k] |
20 |
| - } |
21 |
| - labels["containerd.io/snapshot/remote/stargz.reference"] = ref |
22 |
| - labels["containerd.io/snapshot/remote/stargz.digest"] = desc.Digest.String() |
23 |
| - var ( |
24 |
| - layersKey = "containerd.io/snapshot/remote/stargz.layers" |
25 |
| - layers string |
26 |
| - ) |
| 61 | + |
| 62 | + var layers string |
27 | 63 | for _, l := range descs[targetIndex:] {
|
28 |
| - ls := fmt.Sprintf("%s,", l.Digest.String()) |
29 | 64 | // This avoids the label hits the size limitation.
|
30 | 65 | // Skipping layers is allowed here and only affects performance.
|
31 |
| - if err := ctdlabels.Validate(layersKey, layers+ls); err != nil { |
| 66 | + if err := labels.Validate(targetImageLayersLabel, layers+l.Digest.String()); err != nil { |
32 | 67 | break
|
33 | 68 | }
|
34 |
| - layers += ls |
| 69 | + layers += l.Digest.String() + "," |
| 70 | + } |
| 71 | + return map[string]string{ |
| 72 | + TOCJSONDigestAnnotation: desc.Annotations[TOCJSONDigestAnnotation], |
| 73 | + StoreUncompressedSizeAnnotation: desc.Annotations[StoreUncompressedSizeAnnotation], |
| 74 | + targetRefLabel: ref, |
| 75 | + targetDigestLabel: desc.Digest.String(), |
| 76 | + targetImageLayersLabel: strings.TrimSuffix(layers, ","), |
35 | 77 | }
|
36 |
| - labels[layersKey] = strings.TrimSuffix(layers, ",") |
37 |
| - return labels |
38 | 78 | }
|
0 commit comments