Skip to content

Commit 67f3e79

Browse files
committed
Dedupe "containerd.io/uncompressed" constants and literals
Dedupe the several "containerd.io/uncompressed" constants and literals into `github.com/containerd/containerd/labels.LabelUncompressed` Signed-off-by: Akihiro Suda <[email protected]>
1 parent 8656037 commit 67f3e79

File tree

18 files changed

+63
-50
lines changed

18 files changed

+63
-50
lines changed

cache/blobs.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/containerd/containerd/diff"
1010
"github.com/containerd/containerd/diff/walking"
11+
"github.com/containerd/containerd/labels"
1112
"github.com/containerd/containerd/leases"
1213
"github.com/containerd/containerd/mount"
1314
"github.com/moby/buildkit/session"
@@ -25,8 +26,6 @@ import (
2526
var g flightcontrol.Group[struct{}]
2627
var gFileList flightcontrol.Group[[]string]
2728

28-
const containerdUncompressed = "containerd.io/uncompressed"
29-
3029
var ErrNoBlobs = errors.Errorf("no blobs for snapshot")
3130

3231
// computeBlobChain ensures every ref in a parent chain has an associated blob in the content store. If
@@ -231,10 +230,10 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
231230
return struct{}{}, err
232231
}
233232

234-
if diffID, ok := info.Labels[containerdUncompressed]; ok {
235-
desc.Annotations[containerdUncompressed] = diffID
233+
if diffID, ok := info.Labels[labels.LabelUncompressed]; ok {
234+
desc.Annotations[labels.LabelUncompressed] = diffID
236235
} else if mediaType == ocispecs.MediaTypeImageLayer {
237-
desc.Annotations[containerdUncompressed] = desc.Digest.String()
236+
desc.Annotations[labels.LabelUncompressed] = desc.Digest.String()
238237
} else {
239238
return struct{}{}, errors.Errorf("unknown layer compression type")
240239
}

cache/blobs_linux.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/containerd/containerd/content"
1212
"github.com/containerd/containerd/errdefs"
13+
labelspkg "github.com/containerd/containerd/labels"
1314
"github.com/containerd/containerd/mount"
1415
"github.com/moby/buildkit/util/bklog"
1516
"github.com/moby/buildkit/util/compression"
@@ -69,7 +70,7 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
6970
if labels == nil {
7071
labels = map[string]string{}
7172
}
72-
labels[containerdUncompressed] = dgstr.Digest().String()
73+
labels[labelspkg.LabelUncompressed] = dgstr.Digest().String()
7374
} else {
7475
if err = overlay.WriteUpperdir(ctx, bufW, upperdir, lower); err != nil {
7576
return emptyDesc, false, errors.Wrap(err, "failed to write diff")
@@ -101,9 +102,9 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
101102
cinfo.Labels = make(map[string]string)
102103
}
103104
// Set uncompressed label if digest already existed without label
104-
if _, ok := cinfo.Labels[containerdUncompressed]; !ok {
105-
cinfo.Labels[containerdUncompressed] = labels[containerdUncompressed]
106-
if _, err := sr.cm.ContentStore.Update(ctx, cinfo, "labels."+containerdUncompressed); err != nil {
105+
if _, ok := cinfo.Labels[labelspkg.LabelUncompressed]; !ok {
106+
cinfo.Labels[labelspkg.LabelUncompressed] = labels[labelspkg.LabelUncompressed]
107+
if _, err := sr.cm.ContentStore.Update(ctx, cinfo, "labels."+labelspkg.LabelUncompressed); err != nil {
107108
return emptyDesc, false, errors.Wrap(err, "error setting uncompressed label")
108109
}
109110
}

cache/compression_nydus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func MergeNydus(ctx context.Context, ref ImmutableRef, comp compression.Config,
109109

110110
compressedDgst := cw.Digest()
111111
if err := cw.Commit(ctx, 0, compressedDgst, content.WithLabels(map[string]string{
112-
containerdUncompressed: uncompressedDgst.Digest().String(),
112+
labels.LabelUncompressed: uncompressedDgst.Digest().String(),
113113
})); err != nil {
114114
if !errdefs.IsAlreadyExists(err) {
115115
return nil, errors.Wrap(err, "commit to content store")
@@ -129,7 +129,7 @@ func MergeNydus(ctx context.Context, ref ImmutableRef, comp compression.Config,
129129
Size: info.Size,
130130
MediaType: ocispecs.MediaTypeImageLayerGzip,
131131
Annotations: map[string]string{
132-
containerdUncompressed: uncompressedDgst.Digest().String(),
132+
labels.LabelUncompressed: uncompressedDgst.Digest().String(),
133133
// Use this annotation to identify nydus bootstrap layer.
134134
converter.LayerAnnotationNydusBootstrap: "true",
135135
},

cache/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/containerd/containerd/errdefs"
1414
"github.com/containerd/containerd/filters"
1515
"github.com/containerd/containerd/gc"
16+
"github.com/containerd/containerd/labels"
1617
"github.com/containerd/containerd/leases"
1718
"github.com/docker/docker/pkg/idtools"
1819
"github.com/moby/buildkit/cache/metadata"
@@ -1657,7 +1658,7 @@ func sortDeleteRecords(toDelete []*deleteRecord) {
16571658
}
16581659

16591660
func diffIDFromDescriptor(desc ocispecs.Descriptor) (digest.Digest, error) {
1660-
diffIDStr, ok := desc.Annotations["containerd.io/uncompressed"]
1661+
diffIDStr, ok := desc.Annotations[labels.LabelUncompressed]
16611662
if !ok {
16621663
return "", errors.Errorf("missing uncompressed annotation for %s", desc.Digest)
16631664
}

cache/manager_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/containerd/containerd/diff/apply"
2626
"github.com/containerd/containerd/diff/walking"
2727
"github.com/containerd/containerd/errdefs"
28+
"github.com/containerd/containerd/labels"
2829
"github.com/containerd/containerd/leases"
2930
ctdmetadata "github.com/containerd/containerd/metadata"
3031
"github.com/containerd/containerd/mount"
@@ -731,7 +732,7 @@ func TestSetBlob(t *testing.T) {
731732
err = snap.(*immutableRef).setBlob(ctx, ocispecs.Descriptor{
732733
Digest: digest.FromBytes([]byte("foobar")),
733734
Annotations: map[string]string{
734-
"containerd.io/uncompressed": digest.FromBytes([]byte("foobar2")).String(),
735+
labels.LabelUncompressed: digest.FromBytes([]byte("foobar2")).String(),
735736
},
736737
})
737738
require.Error(t, err)
@@ -742,7 +743,7 @@ func TestSetBlob(t *testing.T) {
742743
require.NoError(t, err)
743744

744745
snapRef = snap.(*immutableRef)
745-
require.Equal(t, desc.Annotations["containerd.io/uncompressed"], string(snapRef.getDiffID()))
746+
require.Equal(t, desc.Annotations[labels.LabelUncompressed], string(snapRef.getDiffID()))
746747
require.Equal(t, desc.Digest, snapRef.getBlob())
747748
require.Equal(t, desc.MediaType, snapRef.getMediaType())
748749
require.Equal(t, snapRef.getDiffID(), snapRef.getChainID())
@@ -768,7 +769,7 @@ func TestSetBlob(t *testing.T) {
768769
require.NoError(t, err)
769770

770771
snapRef2 := snap2.(*immutableRef)
771-
require.Equal(t, desc2.Annotations["containerd.io/uncompressed"], string(snapRef2.getDiffID()))
772+
require.Equal(t, desc2.Annotations[labels.LabelUncompressed], string(snapRef2.getDiffID()))
772773
require.Equal(t, desc2.Digest, snapRef2.getBlob())
773774
require.Equal(t, desc2.MediaType, snapRef2.getMediaType())
774775
require.Equal(t, digest.FromBytes([]byte(snapRef.getChainID()+" "+snapRef2.getDiffID())), snapRef2.getChainID())
@@ -786,7 +787,7 @@ func TestSetBlob(t *testing.T) {
786787
require.NoError(t, err)
787788

788789
snapRef3 := snap3.(*immutableRef)
789-
require.Equal(t, desc3.Annotations["containerd.io/uncompressed"], string(snapRef3.getDiffID()))
790+
require.Equal(t, desc3.Annotations[labels.LabelUncompressed], string(snapRef3.getDiffID()))
790791
require.Equal(t, desc3.Digest, snapRef3.getBlob())
791792
require.Equal(t, desc3.MediaType, snapRef3.getMediaType())
792793
require.Equal(t, digest.FromBytes([]byte(snapRef.getChainID()+" "+snapRef3.getDiffID())), snapRef3.getChainID())
@@ -804,7 +805,7 @@ func TestSetBlob(t *testing.T) {
804805
b5, desc5, err := mapToBlob(map[string]string{"foo5": "bar5"}, true)
805806
require.NoError(t, err)
806807

807-
desc5.Annotations["containerd.io/uncompressed"] = snapRef2.getDiffID().String()
808+
desc5.Annotations[labels.LabelUncompressed] = snapRef2.getDiffID().String()
808809

809810
err = content.WriteBlob(ctx, co.cs, "ref5", bytes.NewBuffer(b5), desc5)
810811
require.NoError(t, err)
@@ -833,7 +834,7 @@ func TestSetBlob(t *testing.T) {
833834
require.NoError(t, err)
834835

835836
snapRef6 := snap6.(*immutableRef)
836-
require.Equal(t, desc6.Annotations["containerd.io/uncompressed"], string(snapRef6.getDiffID()))
837+
require.Equal(t, desc6.Annotations[labels.LabelUncompressed], string(snapRef6.getDiffID()))
837838
require.Equal(t, desc6.Digest, snapRef6.getBlob())
838839
require.Equal(t, digest.FromBytes([]byte(snapRef3.getChainID()+" "+snapRef6.getDiffID())), snapRef6.getChainID())
839840
require.Equal(t, digest.FromBytes([]byte(snapRef3.getBlobChainID()+" "+digest.FromBytes([]byte(snapRef6.getBlob()+" "+snapRef6.getDiffID())))), snapRef6.getBlobChainID())
@@ -843,7 +844,7 @@ func TestSetBlob(t *testing.T) {
843844
_, err = cm.GetByBlob(ctx, ocispecs.Descriptor{
844845
Digest: digest.FromBytes([]byte("notexist")),
845846
Annotations: map[string]string{
846-
"containerd.io/uncompressed": digest.FromBytes([]byte("notexist")).String(),
847+
labels.LabelUncompressed: digest.FromBytes([]byte("notexist")).String(),
847848
},
848849
}, snap3)
849850
require.Error(t, err)
@@ -1584,7 +1585,7 @@ func TestConversion(t *testing.T) {
15841585
}
15851586
require.Equal(t, recreatedDesc.Digest, orgDesc.Digest, testName)
15861587
require.NotNil(t, recreatedDesc.Annotations)
1587-
require.Equal(t, recreatedDesc.Annotations["containerd.io/uncompressed"], orgDesc.Digest.String(), testName)
1588+
require.Equal(t, recreatedDesc.Annotations[labels.LabelUncompressed], orgDesc.Digest.String(), testName)
15881589
return nil
15891590
})
15901591
}
@@ -1965,7 +1966,7 @@ func checkInfo(ctx context.Context, t *testing.T, cs content.Store, info content
19651966
if info.Labels == nil {
19661967
return
19671968
}
1968-
uncompressedDgst, ok := info.Labels[containerdUncompressed]
1969+
uncompressedDgst, ok := info.Labels[labels.LabelUncompressed]
19691970
if !ok {
19701971
return
19711972
}
@@ -1987,7 +1988,7 @@ func checkDescriptor(ctx context.Context, t *testing.T, cs content.Store, desc o
19871988
}
19881989

19891990
// Check annotations exist
1990-
uncompressedDgst, ok := desc.Annotations[containerdUncompressed]
1991+
uncompressedDgst, ok := desc.Annotations[labels.LabelUncompressed]
19911992
require.True(t, ok, "uncompressed digest annotation not found: %q", desc.Digest)
19921993
var uncompressedSize int64
19931994
if compressionType == compression.EStargz {
@@ -2579,7 +2580,7 @@ func mapToBlobWithCompression(m map[string]string, compress func(io.Writer) (io.
25792580
MediaType: mediaType,
25802581
Size: int64(buf.Len()),
25812582
Annotations: map[string]string{
2582-
"containerd.io/uncompressed": sha.Digest().String(),
2583+
labels.LabelUncompressed: sha.Digest().String(),
25832584
},
25842585
}, nil
25852586
}
@@ -2631,7 +2632,7 @@ func fileToBlob(file *os.File, compress bool) ([]byte, ocispecs.Descriptor, erro
26312632
MediaType: mediaType,
26322633
Size: int64(buf.Len()),
26332634
Annotations: map[string]string{
2634-
"containerd.io/uncompressed": sha.Digest().String(),
2635+
labels.LabelUncompressed: sha.Digest().String(),
26352636
},
26362637
}, nil
26372638
}
@@ -2688,7 +2689,7 @@ func mapToSystemTarBlob(t *testing.T, m map[string]string) ([]byte, ocispecs.Des
26882689
MediaType: ocispecs.MediaTypeImageLayer,
26892690
Size: int64(len(tarout)),
26902691
Annotations: map[string]string{
2691-
"containerd.io/uncompressed": digest.FromBytes(tarout).String(),
2692+
labels.LabelUncompressed: digest.FromBytes(tarout).String(),
26922693
},
26932694
}, nil
26942695
}

cache/refs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/containerd/containerd/content"
1313
"github.com/containerd/containerd/errdefs"
1414
"github.com/containerd/containerd/images"
15+
"github.com/containerd/containerd/labels"
1516
"github.com/containerd/containerd/leases"
1617
"github.com/containerd/containerd/mount"
1718
"github.com/containerd/containerd/pkg/userns"
@@ -39,7 +40,7 @@ import (
3940
"golang.org/x/sync/errgroup"
4041
)
4142

42-
var additionalAnnotations = append(compression.EStargzAnnotations, containerdUncompressed)
43+
var additionalAnnotations = append(compression.EStargzAnnotations, labels.LabelUncompressed)
4344

4445
// Ref is a reference to cacheable objects.
4546
type Ref interface {
@@ -733,7 +734,7 @@ func (sr *immutableRef) ociDesc(ctx context.Context, dhs DescHandlers, preferNon
733734

734735
diffID := sr.getDiffID()
735736
if diffID != "" {
736-
desc.Annotations["containerd.io/uncompressed"] = string(diffID)
737+
desc.Annotations[labels.LabelUncompressed] = string(diffID)
737738
}
738739

739740
createdAt := sr.GetCreatedAt()

cache/remotecache/azblob/exporter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
1212
"github.com/containerd/containerd/content"
13+
"github.com/containerd/containerd/labels"
1314
"github.com/moby/buildkit/cache/remotecache"
1415
v1 "github.com/moby/buildkit/cache/remotecache/v1"
1516
"github.com/moby/buildkit/session"
@@ -72,7 +73,7 @@ func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
7273
return nil, errors.Errorf("invalid descriptor without annotations")
7374
}
7475
var diffID digest.Digest
75-
v, ok := dgstPair.Descriptor.Annotations["containerd.io/uncompressed"]
76+
v, ok := dgstPair.Descriptor.Annotations[labels.LabelUncompressed]
7677
if !ok {
7778
return nil, errors.Errorf("invalid descriptor without uncompressed annotation")
7879
}

cache/remotecache/azblob/importer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
1111
"github.com/containerd/containerd/content"
12+
"github.com/containerd/containerd/labels"
1213
"github.com/moby/buildkit/cache/remotecache"
1314
v1 "github.com/moby/buildkit/cache/remotecache/v1"
1415
"github.com/moby/buildkit/session"
@@ -147,7 +148,7 @@ func (ci *importer) makeDescriptorProviderPair(l v1.CacheLayer) (*v1.DescriptorP
147148
if l.Annotations.DiffID == "" {
148149
return nil, errors.Errorf("cache layer with missing diffid")
149150
}
150-
annotations["containerd.io/uncompressed"] = l.Annotations.DiffID.String()
151+
annotations[labels.LabelUncompressed] = l.Annotations.DiffID.String()
151152
if !l.Annotations.CreatedAt.IsZero() {
152153
txt, err := l.Annotations.CreatedAt.MarshalText()
153154
if err != nil {

cache/remotecache/gha/gha.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/containerd/containerd/content"
14+
"github.com/containerd/containerd/labels"
1415
"github.com/moby/buildkit/cache/remotecache"
1516
v1 "github.com/moby/buildkit/cache/remotecache/v1"
1617
"github.com/moby/buildkit/session"
@@ -133,7 +134,7 @@ func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
133134
return nil, errors.Errorf("invalid descriptor without annotations")
134135
}
135136
var diffID digest.Digest
136-
v, ok := dgstPair.Descriptor.Annotations["containerd.io/uncompressed"]
137+
v, ok := dgstPair.Descriptor.Annotations[labels.LabelUncompressed]
137138
if !ok {
138139
return nil, errors.Errorf("invalid descriptor without uncompressed annotation")
139140
}
@@ -226,7 +227,7 @@ func (ci *importer) makeDescriptorProviderPair(l v1.CacheLayer) (*v1.DescriptorP
226227
if l.Annotations.DiffID == "" {
227228
return nil, errors.Errorf("cache layer with missing diffid")
228229
}
229-
annotations["containerd.io/uncompressed"] = l.Annotations.DiffID.String()
230+
annotations[labels.LabelUncompressed] = l.Annotations.DiffID.String()
230231
if !l.Annotations.CreatedAt.IsZero() {
231232
txt, err := l.Annotations.CreatedAt.MarshalText()
232233
if err != nil {

cache/remotecache/import.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/containerd/containerd/content"
1212
"github.com/containerd/containerd/images"
13+
"github.com/containerd/containerd/labels"
1314
v1 "github.com/moby/buildkit/cache/remotecache/v1"
1415
"github.com/moby/buildkit/session"
1516
"github.com/moby/buildkit/solver"
@@ -221,7 +222,7 @@ func (ci *contentCacheImporter) importInlineCache(ctx context.Context, dt []byte
221222
if createdBy := createdMsg[i]; createdBy != "" {
222223
m.Annotations["buildkit/description"] = createdBy
223224
}
224-
m.Annotations["containerd.io/uncompressed"] = img.Rootfs.DiffIDs[i].String()
225+
m.Annotations[labels.LabelUncompressed] = img.Rootfs.DiffIDs[i].String()
225226
layers[m.Digest] = v1.DescriptorProviderPair{
226227
Descriptor: m,
227228
Provider: ci.provider,

0 commit comments

Comments
 (0)