Skip to content

Commit d990e30

Browse files
committed
util/estargz: simplify implementation and define consts for labels
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e143ae1 commit d990e30

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

util/estargz/labels.go

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,78 @@
11
package estargz
22

33
import (
4-
"fmt"
54
"strings"
65

7-
ctdlabels "github.com/containerd/containerd/v2/pkg/labels"
8-
"github.com/containerd/stargz-snapshotter/estargz"
6+
"github.com/containerd/containerd/v2/pkg/labels"
97
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
108
)
119

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+
1256
func SnapshotLabels(ref string, descs []ocispecs.Descriptor, targetIndex int) map[string]string {
1357
if len(descs) < targetIndex {
1458
return nil
1559
}
1660
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
2763
for _, l := range descs[targetIndex:] {
28-
ls := fmt.Sprintf("%s,", l.Digest.String())
2964
// This avoids the label hits the size limitation.
3065
// 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 {
3267
break
3368
}
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, ","),
3577
}
36-
labels[layersKey] = strings.TrimSuffix(layers, ",")
37-
return labels
3878
}

0 commit comments

Comments
 (0)