2020-05-28 20:46:33 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
|
|
|
"github.com/containerd/containerd/reference"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"github.com/moby/buildkit/util/compression"
|
|
|
|
"github.com/moby/buildkit/util/contentutil"
|
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2020-11-15 20:33:37 +00:00
|
|
|
"github.com/moby/buildkit/util/progress/logs"
|
2020-08-03 12:58:15 +00:00
|
|
|
"github.com/moby/buildkit/util/pull/pullprogress"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Unlazier interface {
|
|
|
|
Unlazy(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:09:10 +00:00
|
|
|
// GetRemote gets a *solver.Remote from content store for this ref (potentially pulling lazily).
|
|
|
|
// Note: Use WorkerRef.GetRemote instead as moby integration requires custom GetRemote implementation.
|
2021-03-30 12:59:03 +00:00
|
|
|
func (sr *immutableRef) GetRemote(ctx context.Context, createIfNeeded bool, compressionType compression.Type, forceCompression bool, s session.Group) (*solver.Remote, error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
ctx, done, err := leaseutil.WithLease(ctx, sr.cm.LeaseManager, leaseutil.MakeTemporary)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer done(ctx)
|
|
|
|
|
2021-03-30 12:59:03 +00:00
|
|
|
err = sr.computeBlobChain(ctx, createIfNeeded, compressionType, forceCompression, s)
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-30 12:59:03 +00:00
|
|
|
chain := sr.parentRefChain()
|
|
|
|
mproviderBase := contentutil.NewMultiProvider(nil)
|
|
|
|
mprovider := &lazyMultiProvider{mprovider: mproviderBase}
|
2020-05-28 20:46:33 +00:00
|
|
|
remote := &solver.Remote{
|
|
|
|
Provider: mprovider,
|
|
|
|
}
|
2021-03-30 12:59:03 +00:00
|
|
|
for _, ref := range chain {
|
2021-07-26 02:00:48 +00:00
|
|
|
desc, err := ref.ociDesc(ctx, sr.descHandlers)
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The media type might be missing for some migrated ones
|
|
|
|
// from before lease based storage. If so, we should detect
|
|
|
|
// the media type from blob data.
|
|
|
|
//
|
|
|
|
// Discussion: https://github.com/moby/buildkit/pull/1277#discussion_r352795429
|
|
|
|
if desc.MediaType == "" {
|
|
|
|
desc.MediaType, err = compression.DetectLayerMediaType(ctx, sr.cm.ContentStore, desc.Digest, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
// update distribution source annotation for lazy-refs (non-lazy refs
|
|
|
|
// will already have their dsl stored in the content store, which is
|
|
|
|
// used by the push handlers)
|
2021-07-26 02:00:48 +00:00
|
|
|
isLazy, err := ref.isLazy(ctx)
|
|
|
|
if err != nil {
|
2020-08-05 23:51:19 +00:00
|
|
|
return nil, err
|
|
|
|
} else if isLazy {
|
2021-07-09 00:09:35 +00:00
|
|
|
imageRefs := ref.getImageRefs()
|
2020-08-05 23:51:19 +00:00
|
|
|
for _, imageRef := range imageRefs {
|
|
|
|
refspec, err := reference.Parse(imageRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
u, err := url.Parse("dummy://" + refspec.Locator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
source, repo := u.Hostname(), strings.TrimPrefix(u.Path, "/")
|
|
|
|
if desc.Annotations == nil {
|
|
|
|
desc.Annotations = make(map[string]string)
|
|
|
|
}
|
|
|
|
dslKey := fmt.Sprintf("%s.%s", "containerd.io/distribution.source", source)
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
var existingRepos []string
|
|
|
|
if existings, ok := desc.Annotations[dslKey]; ok {
|
|
|
|
existingRepos = strings.Split(existings, ",")
|
|
|
|
}
|
|
|
|
addNewRepo := true
|
|
|
|
for _, existing := range existingRepos {
|
|
|
|
if existing == repo {
|
|
|
|
addNewRepo = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if addNewRepo {
|
|
|
|
existingRepos = append(existingRepos, repo)
|
|
|
|
}
|
|
|
|
desc.Annotations[dslKey] = strings.Join(existingRepos, ",")
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 12:59:03 +00:00
|
|
|
if forceCompression {
|
2021-07-26 02:00:48 +00:00
|
|
|
if needs, err := needsConversion(desc.MediaType, compressionType); err != nil {
|
2021-03-30 12:59:03 +00:00
|
|
|
return nil, err
|
2021-07-26 02:00:48 +00:00
|
|
|
} else if needs {
|
|
|
|
// ensure the compression type.
|
|
|
|
// compressed blob must be created and stored in the content store.
|
|
|
|
blobDesc, err := ref.getCompressionBlob(ctx, compressionType)
|
2021-03-30 12:59:03 +00:00
|
|
|
if err != nil {
|
2021-07-26 02:00:48 +00:00
|
|
|
return nil, errors.Wrapf(err, "compression blob for %q not found", compressionType)
|
2021-03-30 12:59:03 +00:00
|
|
|
}
|
|
|
|
newDesc := desc
|
2021-07-26 02:00:48 +00:00
|
|
|
newDesc.MediaType = blobDesc.MediaType
|
|
|
|
newDesc.Digest = blobDesc.Digest
|
|
|
|
newDesc.Size = blobDesc.Size
|
|
|
|
for k, v := range blobDesc.Annotations {
|
|
|
|
if newDesc.Annotations == nil {
|
|
|
|
newDesc.Annotations = make(map[string]string)
|
|
|
|
}
|
|
|
|
newDesc.Annotations[k] = v
|
2021-03-30 12:59:03 +00:00
|
|
|
}
|
|
|
|
desc = newDesc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
remote.Descriptors = append(remote.Descriptors, desc)
|
|
|
|
mprovider.Add(lazyRefProvider{
|
2020-10-27 06:13:39 +00:00
|
|
|
ref: ref,
|
|
|
|
desc: desc,
|
|
|
|
dh: sr.descHandlers[desc.Digest],
|
|
|
|
session: s,
|
2020-05-28 20:46:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return remote, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type lazyMultiProvider struct {
|
|
|
|
mprovider *contentutil.MultiProvider
|
|
|
|
plist []lazyRefProvider
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:34:33 +00:00
|
|
|
func (mp *lazyMultiProvider) Add(p lazyRefProvider) {
|
2020-05-28 20:46:33 +00:00
|
|
|
mp.mprovider.Add(p.desc.Digest, p)
|
|
|
|
mp.plist = append(mp.plist, p)
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func (mp *lazyMultiProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
return mp.mprovider.ReaderAt(ctx, desc)
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:34:33 +00:00
|
|
|
func (mp *lazyMultiProvider) Unlazy(ctx context.Context) error {
|
2020-05-28 20:46:33 +00:00
|
|
|
eg, egctx := errgroup.WithContext(ctx)
|
|
|
|
for _, p := range mp.plist {
|
2020-08-17 09:34:33 +00:00
|
|
|
p := p
|
2020-05-28 20:46:33 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
return p.Unlazy(egctx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type lazyRefProvider struct {
|
2020-10-27 06:13:39 +00:00
|
|
|
ref *immutableRef
|
2021-07-26 08:53:30 +00:00
|
|
|
desc ocispecs.Descriptor
|
2020-10-27 06:13:39 +00:00
|
|
|
dh *DescHandler
|
|
|
|
session session.Group
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func (p lazyRefProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
if desc.Digest != p.desc.Digest {
|
|
|
|
return nil, errdefs.ErrNotFound
|
|
|
|
}
|
|
|
|
if err := p.Unlazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p.ref.cm.ContentStore.ReaderAt(ctx, desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p lazyRefProvider) Unlazy(ctx context.Context) error {
|
|
|
|
_, err := p.ref.cm.unlazyG.Do(ctx, string(p.desc.Digest), func(ctx context.Context) (_ interface{}, rerr error) {
|
|
|
|
if isLazy, err := p.ref.isLazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !isLazy {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.dh == nil {
|
|
|
|
// shouldn't happen, if you have a lazy immutable ref it already should be validated
|
|
|
|
// that descriptor handlers exist for it
|
|
|
|
return nil, errors.New("unexpected nil descriptor handler")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.dh.Progress != nil {
|
|
|
|
var stopProgress func(error)
|
|
|
|
ctx, stopProgress = p.dh.Progress.Start(ctx)
|
|
|
|
defer stopProgress(rerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now, just pull down the whole content and then return a ReaderAt from the local content
|
|
|
|
// store. If efficient partial reads are desired in the future, something more like a "tee"
|
|
|
|
// that caches remote partial reads to a local store may need to replace this.
|
2020-08-03 12:58:15 +00:00
|
|
|
err := contentutil.Copy(ctx, p.ref.cm.ContentStore, &pullprogress.ProviderWithProgress{
|
2020-10-27 06:13:39 +00:00
|
|
|
Provider: p.dh.Provider(p.session),
|
2020-08-03 12:58:15 +00:00
|
|
|
Manager: p.ref.cm.ContentStore,
|
2021-07-13 05:28:35 +00:00
|
|
|
}, p.desc, p.dh.Ref, logs.LoggerFromContext(ctx))
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
if imageRefs := p.ref.getImageRefs(); len(imageRefs) > 0 {
|
2020-08-05 23:51:19 +00:00
|
|
|
// just use the first image ref, it's arbitrary
|
|
|
|
imageRef := imageRefs[0]
|
2021-07-09 00:09:35 +00:00
|
|
|
if p.ref.GetDescription() == "" {
|
|
|
|
if err := p.ref.SetDescription("pulled from " + imageRef); err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
|
|
|
|
compressionType := compression.FromMediaType(p.desc.MediaType)
|
|
|
|
if compressionType == compression.UnknownCompression {
|
|
|
|
return nil, errors.Errorf("unhandled layer media type: %q", p.desc.MediaType)
|
|
|
|
}
|
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
if err := p.ref.addCompressionBlob(ctx, p.desc, compressionType); err != nil {
|
2021-07-21 22:53:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-05-28 20:46:33 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|