Export remote snapshotter label handler
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
This commit is contained in:
@@ -42,10 +42,10 @@ import (
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
containerdimages "github.com/containerd/containerd/images"
|
||||
"github.com/containerd/containerd/labels"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/pkg/cri/annotations"
|
||||
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
||||
snpkg "github.com/containerd/containerd/pkg/snapshotters"
|
||||
distribution "github.com/containerd/containerd/reference/docker"
|
||||
"github.com/containerd/containerd/remotes/docker"
|
||||
"github.com/containerd/containerd/remotes/docker/config"
|
||||
@@ -170,7 +170,7 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
|
||||
pullOpts = append(pullOpts, c.encryptedImagesPullOpts()...)
|
||||
if !c.config.ContainerdConfig.DisableSnapshotAnnotations {
|
||||
pullOpts = append(pullOpts,
|
||||
containerd.WithImageHandlerWrapper(appendInfoHandlerWrapper(ref)))
|
||||
containerd.WithImageHandlerWrapper(snpkg.AppendInfoHandlerWrapper(ref)))
|
||||
}
|
||||
|
||||
if c.config.ContainerdConfig.DiscardUnpackedLayers {
|
||||
@@ -552,76 +552,6 @@ func (c *criService) encryptedImagesPullOpts() []containerd.RemoteOpt {
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
// targetRefLabel is a label which contains image reference and will be passed
|
||||
// to snapshotters.
|
||||
targetRefLabel = "containerd.io/snapshot/cri.image-ref"
|
||||
// targetManifestDigestLabel is a label which contains manifest digest and will be passed
|
||||
// to snapshotters.
|
||||
targetManifestDigestLabel = "containerd.io/snapshot/cri.manifest-digest"
|
||||
// targetLayerDigestLabel is a label which contains layer digest and will be passed
|
||||
// to snapshotters.
|
||||
targetLayerDigestLabel = "containerd.io/snapshot/cri.layer-digest"
|
||||
// targetImageLayersLabel is a label which contains layer digests contained in
|
||||
// the target image and will be passed to snapshotters for preparing layers in
|
||||
// parallel. Skipping some layers is allowed and only affects performance.
|
||||
targetImageLayersLabel = "containerd.io/snapshot/cri.image-layers"
|
||||
)
|
||||
|
||||
// appendInfoHandlerWrapper makes a handler which appends some basic information
|
||||
// of images like digests for manifest and their child layers as annotations during unpack.
|
||||
// These annotations will be passed to snapshotters as labels. These labels will be
|
||||
// used mainly by stargz-based snapshotters for querying image contents from the
|
||||
// registry.
|
||||
func appendInfoHandlerWrapper(ref string) func(f containerdimages.Handler) containerdimages.Handler {
|
||||
return func(f containerdimages.Handler) containerdimages.Handler {
|
||||
return containerdimages.HandlerFunc(func(ctx context.Context, desc imagespec.Descriptor) ([]imagespec.Descriptor, error) {
|
||||
children, err := f.Handle(ctx, desc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch desc.MediaType {
|
||||
case imagespec.MediaTypeImageManifest, containerdimages.MediaTypeDockerSchema2Manifest:
|
||||
for i := range children {
|
||||
c := &children[i]
|
||||
if containerdimages.IsLayerType(c.MediaType) {
|
||||
if c.Annotations == nil {
|
||||
c.Annotations = make(map[string]string)
|
||||
}
|
||||
c.Annotations[targetRefLabel] = ref
|
||||
c.Annotations[targetLayerDigestLabel] = c.Digest.String()
|
||||
c.Annotations[targetImageLayersLabel] = getLayers(ctx, targetImageLayersLabel, children[i:], labels.Validate)
|
||||
c.Annotations[targetManifestDigestLabel] = desc.Digest.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return children, nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// getLayers returns comma-separated digests based on the passed list of
|
||||
// descriptors. The returned list contains as many digests as possible as well
|
||||
// as meets the label validation.
|
||||
func getLayers(ctx context.Context, key string, descs []imagespec.Descriptor, validate func(k, v string) error) (layers string) {
|
||||
var item string
|
||||
for _, l := range descs {
|
||||
if containerdimages.IsLayerType(l.MediaType) {
|
||||
item = l.Digest.String()
|
||||
if layers != "" {
|
||||
item = "," + item
|
||||
}
|
||||
// This avoids the label hits the size limitation.
|
||||
if err := validate(key, layers+item); err != nil {
|
||||
log.G(ctx).WithError(err).WithField("label", key).Debugf("%q is omitted in the layers list", l.Digest.String())
|
||||
break
|
||||
}
|
||||
layers += item
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
// minPullProgressReportInternal is used to prevent the reporter from
|
||||
// eating more CPU resources
|
||||
|
||||
Reference in New Issue
Block a user