Skip to content

Commit 0756b47

Browse files
committed
util/estargz: export labels and use consts
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent d990e30 commit 0756b47

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

cache/refs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10+
"strconv"
1011
"strings"
1112
"sync"
1213
"time"
@@ -26,6 +27,7 @@ import (
2627
"github.com/moby/buildkit/solver"
2728
"github.com/moby/buildkit/util/bklog"
2829
"github.com/moby/buildkit/util/compression"
30+
"github.com/moby/buildkit/util/estargz"
2931
"github.com/moby/buildkit/util/flightcontrol"
3032
"github.com/moby/buildkit/util/leaseutil"
3133
"github.com/moby/buildkit/util/overlay"
@@ -1155,7 +1157,7 @@ func makeTmpLabelsStargzMode(labels map[string]string, s session.Group) (fields
11551157
res[tmpKey] = v
11561158
}
11571159
for i, sid := range session.AllSessionIDs(s) {
1158-
sidKey := "containerd.io/snapshot/remote/stargz.session." + fmt.Sprintf("%d", i) + "." + id
1160+
sidKey := estargz.TargetSessionLabel + "." + strconv.Itoa(i) + "." + id
11591161
fields = append(fields, "labels."+sidKey)
11601162
res[sidKey] = sid
11611163
}

cmd/buildkitd/main_oci_worker.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/moby/buildkit/session"
3535
"github.com/moby/buildkit/util/bklog"
3636
"github.com/moby/buildkit/util/disk"
37+
"github.com/moby/buildkit/util/estargz"
3738
"github.com/moby/buildkit/util/network/cniprovider"
3839
"github.com/moby/buildkit/util/network/netproviders"
3940
"github.com/moby/buildkit/util/resolver"
@@ -461,22 +462,6 @@ func validOCIBinary() bool {
461462
return true
462463
}
463464

464-
const (
465-
// targetRefLabel is a label which contains image reference.
466-
targetRefLabel = "containerd.io/snapshot/remote/stargz.reference"
467-
468-
// targetDigestLabel is a label which contains layer digest.
469-
targetDigestLabel = "containerd.io/snapshot/remote/stargz.digest"
470-
471-
// targetImageLayersLabel is a label which contains layer digests contained in
472-
// the target image.
473-
targetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers"
474-
475-
// targetSessionLabel is a labeld which contains session IDs usable for
476-
// authenticating the target snapshot.
477-
targetSessionLabel = "containerd.io/snapshot/remote/stargz.session"
478-
)
479-
480465
// sourceWithSession returns a callback which implements a converter from labels to the
481466
// typed snapshot source info. This callback is called everytime the snapshotter resolves a
482467
// snapshot. This callback returns configuration that is based on buildkitd's registry config
@@ -487,15 +472,15 @@ func sourceWithSession(hosts docker.RegistryHosts, sm *session.Manager) sgzsourc
487472
// to the snapshotter API. So, first, get all these IDs
488473
var ids []string
489474
for k := range labels {
490-
if strings.HasPrefix(k, targetRefLabel+".") {
491-
ids = append(ids, strings.TrimPrefix(k, targetRefLabel+"."))
475+
if strings.HasPrefix(k, estargz.TargetRefLabel+".") {
476+
ids = append(ids, strings.TrimPrefix(k, estargz.TargetRefLabel+"."))
492477
}
493478
}
494479

495480
// Parse all labels
496481
for _, id := range ids {
497482
// Parse session labels
498-
ref, ok := labels[targetRefLabel+"."+id]
483+
ref, ok := labels[estargz.TargetRefLabel+"."+id]
499484
if !ok {
500485
continue
501486
}
@@ -505,7 +490,7 @@ func sourceWithSession(hosts docker.RegistryHosts, sm *session.Manager) sgzsourc
505490
}
506491
var sids []string
507492
for i := 0; ; i++ {
508-
sidKey := targetSessionLabel + "." + fmt.Sprintf("%d", i) + "." + id
493+
sidKey := estargz.TargetSessionLabel + "." + fmt.Sprintf("%d", i) + "." + id
509494
sid, ok := labels[sidKey]
510495
if !ok {
511496
break
@@ -520,9 +505,9 @@ func sourceWithSession(hosts docker.RegistryHosts, sm *session.Manager) sgzsourc
520505
HostsFunc(ref.Hostname())
521506
})
522507
if s, err := parse(map[string]string{
523-
targetRefLabel: ref,
524-
targetDigestLabel: labels[targetDigestLabel+"."+id],
525-
targetImageLayersLabel: labels[targetImageLayersLabel+"."+id],
508+
estargz.TargetRefLabel: ref,
509+
estargz.TargetDigestLabel: labels[estargz.TargetDigestLabel+"."+id],
510+
estargz.TargetImageLayersLabel: labels[estargz.TargetImageLayersLabel+"."+id],
526511
}); err == nil {
527512
src = append(src, s...)
528513
}

util/estargz/labels.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,33 @@ import (
88
)
99

1010
const (
11-
// targetRefLabel is a label which contains image reference.
11+
// TargetRefLabel is a label which contains image reference.
1212
//
1313
// It is a copy of [stargz-snapshotter/fs/source.targetRefLabel].
1414
//
1515
// [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"
16+
TargetRefLabel = "containerd.io/snapshot/remote/stargz.reference"
1717

18-
// targetDigestLabel is a label which contains layer digest.
18+
// TargetDigestLabel is a label which contains layer digest.
1919
//
2020
// It is a copy of [stargz-snapshotter/fs/source.targetDigestLabel].
2121
//
2222
// [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"
23+
TargetDigestLabel = "containerd.io/snapshot/remote/stargz.digest"
2424

25-
// targetImageLayersLabel is a label which contains layer digests contained in
25+
// TargetImageLayersLabel is a label which contains layer digests contained in
2626
// the target image.
2727
//
2828
// It is a copy of [stargz-snapshotter/fs/source.targetImageLayersLabel].
2929
//
3030
// [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"
31+
TargetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers"
32+
33+
// TargetSessionLabel is a label which contains session IDs usable for
34+
// authenticating the target snapshot.
35+
//
36+
// It has no equivalent in github.com/containerd/stargz-snapshotter.
37+
TargetSessionLabel = "containerd.io/snapshot/remote/stargz.session"
3238
)
3339

3440
const (
@@ -63,16 +69,16 @@ func SnapshotLabels(ref string, descs []ocispecs.Descriptor, targetIndex int) ma
6369
for _, l := range descs[targetIndex:] {
6470
// This avoids the label hits the size limitation.
6571
// Skipping layers is allowed here and only affects performance.
66-
if err := labels.Validate(targetImageLayersLabel, layers+l.Digest.String()); err != nil {
72+
if err := labels.Validate(TargetImageLayersLabel, layers+l.Digest.String()); err != nil {
6773
break
6874
}
6975
layers += l.Digest.String() + ","
7076
}
7177
return map[string]string{
7278
TOCJSONDigestAnnotation: desc.Annotations[TOCJSONDigestAnnotation],
7379
StoreUncompressedSizeAnnotation: desc.Annotations[StoreUncompressedSizeAnnotation],
74-
targetRefLabel: ref,
75-
targetDigestLabel: desc.Digest.String(),
76-
targetImageLayersLabel: strings.TrimSuffix(layers, ","),
80+
TargetRefLabel: ref,
81+
TargetDigestLabel: desc.Digest.String(),
82+
TargetImageLayersLabel: strings.TrimSuffix(layers, ","),
7783
}
7884
}

0 commit comments

Comments
 (0)