2018-05-17 20:09:11 +00:00
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/images"
|
|
|
|
"github.com/containerd/containerd/platforms"
|
|
|
|
"github.com/containerd/containerd/reference"
|
|
|
|
"github.com/containerd/containerd/remotes"
|
2019-08-15 00:00:52 +00:00
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
2018-05-17 20:09:11 +00:00
|
|
|
"github.com/containerd/containerd/remotes/docker/schema1"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"github.com/moby/buildkit/util/contentutil"
|
2018-05-17 20:09:11 +00:00
|
|
|
"github.com/moby/buildkit/util/imageutil"
|
2020-08-03 12:58:15 +00:00
|
|
|
"github.com/moby/buildkit/util/pull/pullprogress"
|
2018-05-17 20:09:11 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Puller struct {
|
|
|
|
ContentStore content.Store
|
2020-05-28 20:46:33 +00:00
|
|
|
Resolver remotes.Resolver
|
2018-05-17 20:09:11 +00:00
|
|
|
Src reference.Spec
|
2020-08-03 13:24:46 +00:00
|
|
|
Platform ocispec.Platform
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2018-05-17 20:09:11 +00:00
|
|
|
resolveOnce sync.Once
|
2020-05-28 20:46:33 +00:00
|
|
|
resolveErr error
|
2018-05-17 20:09:11 +00:00
|
|
|
desc ocispec.Descriptor
|
2020-05-28 20:46:33 +00:00
|
|
|
configDesc ocispec.Descriptor
|
2018-05-17 20:09:11 +00:00
|
|
|
ref string
|
2020-05-28 20:46:33 +00:00
|
|
|
layers []ocispec.Descriptor
|
|
|
|
nonlayers []ocispec.Descriptor
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
var _ content.Provider = &Puller{}
|
|
|
|
|
|
|
|
type PulledManifests struct {
|
|
|
|
Ref string
|
|
|
|
MainManifestDesc ocispec.Descriptor
|
|
|
|
ConfigDesc ocispec.Descriptor
|
|
|
|
Nonlayers []ocispec.Descriptor
|
|
|
|
Remote *solver.Remote
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (p *Puller) resolve(ctx context.Context) error {
|
2018-05-17 20:09:11 +00:00
|
|
|
p.resolveOnce.Do(func() {
|
2020-05-28 20:46:33 +00:00
|
|
|
if p.tryLocalResolve(ctx) == nil {
|
|
|
|
return
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
ref, desc, err := p.Resolver.Resolve(ctx, p.Src.String())
|
|
|
|
if err != nil {
|
|
|
|
p.resolveErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.desc = desc
|
|
|
|
p.ref = ref
|
|
|
|
})
|
2020-05-28 20:46:33 +00:00
|
|
|
|
|
|
|
return p.resolveErr
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (p *Puller) tryLocalResolve(ctx context.Context) error {
|
|
|
|
desc := ocispec.Descriptor{
|
|
|
|
Digest: p.Src.Digest(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if desc.Digest == "" {
|
|
|
|
return errors.New("empty digest")
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := p.ContentStore.Info(ctx, desc.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
desc.Size = info.Size
|
|
|
|
p.ref = p.Src.String()
|
|
|
|
ra, err := p.ContentStore.ReaderAt(ctx, desc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mt, err := imageutil.DetectManifestMediaType(ra)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
desc.MediaType = mt
|
|
|
|
p.desc = desc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Puller) PullManifests(ctx context.Context) (*PulledManifests, error) {
|
|
|
|
err := p.resolve(ctx)
|
|
|
|
if err != nil {
|
2018-05-17 20:09:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:24:46 +00:00
|
|
|
platform := platforms.Only(p.Platform)
|
2018-06-24 05:52:19 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
var mu sync.Mutex // images.Dispatch calls handlers in parallel
|
|
|
|
metadata := make(map[digest.Digest]ocispec.Descriptor)
|
2018-05-17 20:09:11 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
// TODO: need a wrapper snapshot interface that combines content
|
|
|
|
// and snapshots as 1) buildkit shouldn't have a dependency on contentstore
|
|
|
|
// or 2) cachemanager should manage the contentstore
|
|
|
|
var handlers []images.Handler
|
2018-05-17 20:09:11 +00:00
|
|
|
|
|
|
|
fetcher, err := p.Resolver.Fetcher(ctx, p.ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var schema1Converter *schema1.Converter
|
|
|
|
if p.desc.MediaType == images.MediaTypeDockerSchema1Manifest {
|
2020-08-03 12:58:15 +00:00
|
|
|
// schema1 images are not lazy at this time, the converter will pull the whole image
|
|
|
|
// including layer blobs
|
|
|
|
schema1Converter = schema1.NewConverter(p.ContentStore, &pullprogress.FetcherWithProgress{
|
|
|
|
Fetcher: fetcher,
|
|
|
|
Manager: p.ContentStore,
|
|
|
|
})
|
2018-05-17 20:09:11 +00:00
|
|
|
handlers = append(handlers, schema1Converter)
|
|
|
|
} else {
|
|
|
|
// Get all the children for a descriptor
|
|
|
|
childrenHandler := images.ChildrenHandler(p.ContentStore)
|
2018-09-18 16:58:30 +00:00
|
|
|
// Filter the children by the platform
|
2018-08-27 23:44:22 +00:00
|
|
|
childrenHandler = images.FilterPlatforms(childrenHandler, platform)
|
|
|
|
// Limit manifests pulled to the best match in an index
|
|
|
|
childrenHandler = images.LimitManifests(childrenHandler, platform, 1)
|
2018-05-17 20:09:11 +00:00
|
|
|
|
2019-08-15 00:00:52 +00:00
|
|
|
dslHandler, err := docker.AppendDistributionSourceLabel(p.ContentStore, p.ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-17 20:09:11 +00:00
|
|
|
handlers = append(handlers,
|
2020-05-28 20:46:33 +00:00
|
|
|
filterLayerBlobs(metadata, &mu),
|
2018-05-17 20:09:11 +00:00
|
|
|
remotes.FetchHandler(p.ContentStore, fetcher),
|
|
|
|
childrenHandler,
|
2019-08-15 00:00:52 +00:00
|
|
|
dslHandler,
|
2018-05-17 20:09:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-13 01:57:35 +00:00
|
|
|
if err := images.Dispatch(ctx, images.Handlers(handlers...), nil, p.desc); err != nil {
|
2018-05-17 20:09:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if schema1Converter != nil {
|
|
|
|
p.desc, err = schema1Converter.Convert(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:58:15 +00:00
|
|
|
// this just gathers metadata about the converted descriptors making up the image, does
|
|
|
|
// not fetch anything
|
|
|
|
if err := images.Dispatch(ctx, images.Handlers(
|
2020-05-28 20:46:33 +00:00
|
|
|
filterLayerBlobs(metadata, &mu),
|
2018-08-27 23:44:22 +00:00
|
|
|
images.FilterPlatforms(images.ChildrenHandler(p.ContentStore), platform),
|
2020-08-03 12:58:15 +00:00
|
|
|
), nil, p.desc); err != nil {
|
2018-05-17 20:09:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
for _, desc := range metadata {
|
|
|
|
p.nonlayers = append(p.nonlayers, desc)
|
|
|
|
switch desc.MediaType {
|
|
|
|
case images.MediaTypeDockerSchema2Config, ocispec.MediaTypeImageConfig:
|
|
|
|
p.configDesc = desc
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
// split all pulled data to layers and rest. layers remain roots and are deleted with snapshots. rest will be linked to layers.
|
|
|
|
p.layers, err = getLayers(ctx, p.ContentStore, p.desc, platform)
|
2019-09-18 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
return &PulledManifests{
|
|
|
|
Ref: p.ref,
|
|
|
|
MainManifestDesc: p.desc,
|
|
|
|
ConfigDesc: p.configDesc,
|
|
|
|
Nonlayers: p.nonlayers,
|
|
|
|
Remote: &solver.Remote{
|
|
|
|
Descriptors: p.layers,
|
|
|
|
Provider: p,
|
|
|
|
},
|
2018-05-17 20:09:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (p *Puller) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) {
|
|
|
|
err := p.resolve(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher, err := p.Resolver.Fetcher(ctx, p.ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentutil.FromFetcher(fetcher).ReaderAt(ctx, desc)
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:58:15 +00:00
|
|
|
// filterLayerBlobs causes layer blobs to be skipped for fetch, which is required to support lazy blobs.
|
|
|
|
// It also stores the non-layer blobs (metadata) it encounters in the provided map.
|
2020-05-28 20:46:33 +00:00
|
|
|
func filterLayerBlobs(metadata map[digest.Digest]ocispec.Descriptor, mu sync.Locker) images.HandlerFunc {
|
|
|
|
return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
|
|
|
|
switch desc.MediaType {
|
|
|
|
case ocispec.MediaTypeImageLayer, images.MediaTypeDockerSchema2Layer, ocispec.MediaTypeImageLayerGzip, images.MediaTypeDockerSchema2LayerGzip, images.MediaTypeDockerSchema2LayerForeign, images.MediaTypeDockerSchema2LayerForeignGzip:
|
|
|
|
return nil, images.ErrSkipDesc
|
|
|
|
default:
|
|
|
|
if metadata != nil {
|
|
|
|
mu.Lock()
|
|
|
|
metadata[desc.Digest] = desc
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
func getLayers(ctx context.Context, provider content.Provider, desc ocispec.Descriptor, platform platforms.MatchComparer) ([]ocispec.Descriptor, error) {
|
2018-06-24 05:52:19 +00:00
|
|
|
manifest, err := images.Manifest(ctx, provider, desc, platform)
|
2018-05-17 20:09:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
image := images.Image{Target: desc}
|
2018-06-24 05:52:19 +00:00
|
|
|
diffIDs, err := image.RootFS(ctx, provider, platform)
|
2018-05-17 20:09:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to resolve rootfs")
|
|
|
|
}
|
|
|
|
if len(diffIDs) != len(manifest.Layers) {
|
|
|
|
return nil, errors.Errorf("mismatched image rootfs and manifest layers %+v %+v", diffIDs, manifest.Layers)
|
|
|
|
}
|
2019-09-18 00:18:32 +00:00
|
|
|
layers := make([]ocispec.Descriptor, len(diffIDs))
|
2018-05-17 20:09:11 +00:00
|
|
|
for i := range diffIDs {
|
2019-09-18 00:18:32 +00:00
|
|
|
desc := manifest.Layers[i]
|
|
|
|
if desc.Annotations == nil {
|
|
|
|
desc.Annotations = map[string]string{}
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
2019-09-18 00:18:32 +00:00
|
|
|
desc.Annotations["containerd.io/uncompressed"] = diffIDs[i].String()
|
|
|
|
layers[i] = desc
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
|
|
|
return layers, nil
|
|
|
|
}
|