@@ -31,6 +31,7 @@ import (
3131 "github.com/containerd/containerd"
3232 "github.com/containerd/containerd/errdefs"
3333 containerdimages "github.com/containerd/containerd/images"
34+ "github.com/containerd/containerd/labels"
3435 "github.com/containerd/containerd/log"
3536 distribution "github.com/containerd/containerd/reference/docker"
3637 "github.com/containerd/containerd/remotes/docker"
@@ -455,18 +456,21 @@ const (
455456 // targetRefLabel is a label which contains image reference and will be passed
456457 // to snapshotters.
457458 targetRefLabel = "containerd.io/snapshot/cri.image-ref"
458- // targetDigestLabel is a label which contains layer digest and will be passed
459+ // targetManifestDigestLabel is a label which contains manifest digest and will be passed
459460 // to snapshotters.
460- targetDigestLabel = "containerd.io/snapshot/cri.layer-digest"
461+ targetManifestDigestLabel = "containerd.io/snapshot/cri.manifest-digest"
462+ // targetLayerDigestLabel is a label which contains layer digest and will be passed
463+ // to snapshotters.
464+ targetLayerDigestLabel = "containerd.io/snapshot/cri.layer-digest"
461465 // targetImageLayersLabel is a label which contains layer digests contained in
462466 // the target image and will be passed to snapshotters for preparing layers in
463- // parallel.
467+ // parallel. Skipping some layers is allowed and only affects performance.
464468 targetImageLayersLabel = "containerd.io/snapshot/cri.image-layers"
465469)
466470
467471// appendInfoHandlerWrapper makes a handler which appends some basic information
468- // of images to each layer descriptor as annotations during unpack. These
469- // annotations will be passed to snapshotters as labels. These labels will be
472+ // of images like digests for manifest and their child layers as annotations during unpack.
473+ // These annotations will be passed to snapshotters as labels. These labels will be
470474// used mainly by stargz-based snapshotters for querying image contents from the
471475// registry.
472476func appendInfoHandlerWrapper (ref string ) func (f containerdimages.Handler ) containerdimages.Handler {
@@ -478,28 +482,42 @@ func appendInfoHandlerWrapper(ref string) func(f containerdimages.Handler) conta
478482 }
479483 switch desc .MediaType {
480484 case imagespec .MediaTypeImageManifest , containerdimages .MediaTypeDockerSchema2Manifest :
481- var layers string
482- for _ , c := range children {
483- if containerdimages .IsLayerType (c .MediaType ) {
484- layers += fmt .Sprintf ("%s," , c .Digest .String ())
485- }
486- }
487- if len (layers ) >= 1 {
488- layers = layers [:len (layers )- 1 ]
489- }
490485 for i := range children {
491486 c := & children [i ]
492487 if containerdimages .IsLayerType (c .MediaType ) {
493488 if c .Annotations == nil {
494489 c .Annotations = make (map [string ]string )
495490 }
496491 c .Annotations [targetRefLabel ] = ref
497- c .Annotations [targetDigestLabel ] = c .Digest .String ()
498- c .Annotations [targetImageLayersLabel ] = layers
492+ c .Annotations [targetLayerDigestLabel ] = c .Digest .String ()
493+ c .Annotations [targetImageLayersLabel ] = getLayers (ctx , targetImageLayersLabel , children [i :], labels .Validate )
494+ c .Annotations [targetManifestDigestLabel ] = desc .Digest .String ()
499495 }
500496 }
501497 }
502498 return children , nil
503499 })
504500 }
505501}
502+
503+ // getLayers returns comma-separated digests based on the passed list of
504+ // descriptors. The returned list contains as many digests as possible as well
505+ // as meets the label validation.
506+ func getLayers (ctx context.Context , key string , descs []imagespec.Descriptor , validate func (k , v string ) error ) (layers string ) {
507+ var item string
508+ for _ , l := range descs {
509+ if containerdimages .IsLayerType (l .MediaType ) {
510+ item = l .Digest .String ()
511+ if layers != "" {
512+ item = "," + item
513+ }
514+ // This avoids the label hits the size limitation.
515+ if err := validate (key , layers + item ); err != nil {
516+ log .G (ctx ).WithError (err ).WithField ("label" , key ).Debugf ("%q is omitted in the layers list" , l .Digest .String ())
517+ break
518+ }
519+ layers += item
520+ }
521+ }
522+ return
523+ }
0 commit comments