2017-10-12 22:04:33 +00:00
|
|
|
package cacheimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-10-15 05:39:28 +00:00
|
|
|
"fmt"
|
2017-10-13 21:24:27 +00:00
|
|
|
"net/http"
|
2017-10-15 05:39:28 +00:00
|
|
|
"time"
|
2017-10-12 22:04:33 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2017-11-06 09:44:23 +00:00
|
|
|
"github.com/containerd/containerd/diff"
|
2017-10-13 21:24:27 +00:00
|
|
|
"github.com/containerd/containerd/remotes"
|
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
2017-10-12 22:04:33 +00:00
|
|
|
"github.com/containerd/containerd/rootfs"
|
2017-11-06 09:44:23 +00:00
|
|
|
cdsnapshot "github.com/containerd/containerd/snapshot"
|
2017-10-12 22:04:33 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/blobs"
|
2017-10-15 05:39:28 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
buildkitidentity "github.com/moby/buildkit/identity"
|
2017-10-15 06:49:55 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/auth"
|
2017-10-12 22:04:33 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2017-10-15 05:39:28 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-10-12 22:04:33 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-10-13 00:53:28 +00:00
|
|
|
"github.com/opencontainers/image-spec/identity"
|
2017-10-12 22:04:33 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
2017-10-13 21:24:27 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2017-10-12 22:04:33 +00:00
|
|
|
)
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
type ImportOpt struct {
|
2017-10-15 06:49:55 +00:00
|
|
|
SessionManager *session.Manager
|
|
|
|
ContentStore content.Store
|
|
|
|
Snapshotter snapshot.Snapshotter
|
2017-11-06 09:44:23 +00:00
|
|
|
Applier diff.Differ
|
2017-10-15 06:49:55 +00:00
|
|
|
CacheAccessor cache.Accessor
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func NewCacheImporter(opt ImportOpt) *CacheImporter {
|
|
|
|
return &CacheImporter{opt: opt}
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
type CacheImporter struct {
|
|
|
|
opt ImportOpt
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 06:49:55 +00:00
|
|
|
func (ci *CacheImporter) getCredentialsFromSession(ctx context.Context) func(string) (string, string, error) {
|
|
|
|
id := session.FromContext(ctx)
|
|
|
|
if id == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(host string) (string, string, error) {
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
caller, err := ci.opt.SessionManager.Get(timeoutCtx, id)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth.CredentialsFunc(context.TODO(), caller)(host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:24:27 +00:00
|
|
|
func (ci *CacheImporter) pull(ctx context.Context, ref string) (*ocispec.Descriptor, remotes.Fetcher, error) {
|
|
|
|
resolver := docker.NewResolver(docker.ResolverOptions{
|
2017-10-15 06:49:55 +00:00
|
|
|
Client: http.DefaultClient,
|
|
|
|
Credentials: ci.getCredentialsFromSession(ctx),
|
2017-10-13 21:24:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ref, desc, err := resolver.Resolve(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher, err := resolver.Fetcher(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := remotes.FetchHandler(ci.opt.ContentStore, fetcher)(ctx, desc); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &desc, fetcher, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *CacheImporter) Import(ctx context.Context, ref string) (InstructionCache, error) {
|
|
|
|
desc, fetcher, err := ci.pull(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := content.ReadBlob(ctx, ci.opt.ContentStore, desc.Digest)
|
2017-10-13 00:53:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
var mfst ocispec.Index
|
|
|
|
if err := json.Unmarshal(dt, &mfst); err != nil {
|
|
|
|
return nil, err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:24:27 +00:00
|
|
|
allDesc := map[digest.Digest]ocispec.Descriptor{}
|
|
|
|
allBlobs := map[digest.Digest]configItem{}
|
|
|
|
byCacheKey := map[digest.Digest]configItem{}
|
2017-10-13 00:53:28 +00:00
|
|
|
byContentKey := map[digest.Digest][]digest.Digest{}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 21:24:27 +00:00
|
|
|
var configDesc ocispec.Descriptor
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
for _, m := range mfst.Manifests {
|
2017-10-13 21:24:27 +00:00
|
|
|
if m.MediaType == mediaTypeConfig {
|
|
|
|
configDesc = m
|
|
|
|
continue
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
2017-10-13 21:24:27 +00:00
|
|
|
allDesc[m.Digest] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
if configDesc.Digest == "" {
|
|
|
|
return nil, errors.Errorf("invalid build cache: %s", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := remotes.FetchHandler(ci.opt.ContentStore, fetcher)(ctx, configDesc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err = content.ReadBlob(ctx, ci.opt.ContentStore, configDesc.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg cacheConfig
|
|
|
|
if err := json.Unmarshal(dt, &cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ci := range cfg.Items {
|
|
|
|
if ci.Blobsum != "" {
|
|
|
|
allBlobs[ci.Blobsum] = ci
|
|
|
|
}
|
|
|
|
if ci.CacheKey != "" {
|
|
|
|
byCacheKey[ci.CacheKey] = ci
|
|
|
|
if ci.ContentKey != "" {
|
2017-10-13 22:35:35 +00:00
|
|
|
byContentKey[ci.ContentKey] = append(byContentKey[ci.ContentKey], ci.CacheKey)
|
2017-10-13 00:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &importInfo{
|
|
|
|
CacheImporter: ci,
|
|
|
|
byCacheKey: byCacheKey,
|
|
|
|
byContentKey: byContentKey,
|
|
|
|
allBlobs: allBlobs,
|
2017-10-13 21:24:27 +00:00
|
|
|
allDesc: allDesc,
|
|
|
|
fetcher: fetcher,
|
2017-10-15 05:39:28 +00:00
|
|
|
ref: ref,
|
2017-10-13 00:53:28 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type importInfo struct {
|
|
|
|
*CacheImporter
|
2017-10-13 21:24:27 +00:00
|
|
|
fetcher remotes.Fetcher
|
|
|
|
byCacheKey map[digest.Digest]configItem
|
2017-10-13 00:53:28 +00:00
|
|
|
byContentKey map[digest.Digest][]digest.Digest
|
2017-10-13 21:24:27 +00:00
|
|
|
allDesc map[digest.Digest]ocispec.Descriptor
|
|
|
|
allBlobs map[digest.Digest]configItem
|
2017-10-15 05:39:28 +00:00
|
|
|
ref string
|
2017-10-13 00:53:28 +00:00
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func (ii *importInfo) Probe(ctx context.Context, key digest.Digest) (bool, error) {
|
|
|
|
_, ok := ii.byCacheKey[key]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ii *importInfo) getChain(dgst digest.Digest) ([]blobs.DiffPair, error) {
|
2017-10-13 21:24:27 +00:00
|
|
|
cfg, ok := ii.allBlobs[dgst]
|
2017-10-13 00:53:28 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("blob %s not found in cache", dgst)
|
|
|
|
}
|
2017-10-13 21:24:27 +00:00
|
|
|
parent := cfg.Parent
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
var out []blobs.DiffPair
|
2017-10-13 21:24:27 +00:00
|
|
|
if parent != "" {
|
|
|
|
parentChain, err := ii.getChain(parent)
|
2017-10-13 00:53:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
2017-10-13 00:53:28 +00:00
|
|
|
out = parentChain
|
|
|
|
}
|
2017-10-13 21:24:27 +00:00
|
|
|
return append(out, blobs.DiffPair{Blobsum: dgst, DiffID: cfg.DiffID}), nil
|
2017-10-13 00:53:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 05:39:28 +00:00
|
|
|
func (ii *importInfo) Lookup(ctx context.Context, key digest.Digest, msg string) (interface{}, error) {
|
2017-10-13 00:53:28 +00:00
|
|
|
desc, ok := ii.byCacheKey[key]
|
2017-10-13 21:24:27 +00:00
|
|
|
if !ok || desc.Blobsum == "" {
|
2017-10-13 00:53:28 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-10-15 05:39:28 +00:00
|
|
|
var out interface{}
|
|
|
|
if err := inVertexContext(ctx, fmt.Sprintf("cache from %s for %s", ii.ref, msg), func(ctx context.Context) error {
|
|
|
|
|
|
|
|
ch, err := ii.getChain(desc.Blobsum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res, err := ii.fetch(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out = res
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2017-10-13 00:53:28 +00:00
|
|
|
return nil, err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
2017-10-15 05:39:28 +00:00
|
|
|
return out, nil
|
2017-10-13 00:53:28 +00:00
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func (ii *importInfo) Set(key digest.Digest, ref interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func (ii *importInfo) SetContentMapping(contentKey, key digest.Digest) error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func (ii *importInfo) GetContentMapping(dgst digest.Digest) ([]digest.Digest, error) {
|
|
|
|
dgsts, ok := ii.byContentKey[dgst]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return dgsts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ii *importInfo) fetch(ctx context.Context, chain []blobs.DiffPair) (cache.ImmutableRef, error) {
|
2017-10-13 21:24:27 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for _, dp := range chain {
|
|
|
|
func(dp blobs.DiffPair) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
desc, ok := ii.allDesc[dp.Blobsum]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("failed to find %s for fetch", dp.Blobsum)
|
|
|
|
}
|
|
|
|
if _, err := remotes.FetchHandler(ii.opt.ContentStore, ii.fetcher)(ctx, desc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(dp)
|
|
|
|
}
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
chainid, err := ii.unpack(ctx, chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
return ii.opt.CacheAccessor.Get(ctx, chainid, cache.WithDescription("imported cache")) // TODO: more descriptive name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ii *importInfo) unpack(ctx context.Context, dpairs []blobs.DiffPair) (string, error) {
|
|
|
|
layers, err := ii.getLayers(ctx, dpairs)
|
2017-10-12 22:04:33 +00:00
|
|
|
if err != nil {
|
2017-10-13 00:53:28 +00:00
|
|
|
return "", err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 09:44:23 +00:00
|
|
|
var chain []digest.Digest
|
|
|
|
for _, layer := range layers {
|
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
"containerd.io/uncompressed": layer.Diff.Digest.String(),
|
|
|
|
}
|
|
|
|
if _, err := rootfs.ApplyLayer(ctx, layer, chain, ii.opt.Snapshotter, ii.opt.Applier, cdsnapshot.WithLabels(labels)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
chain = append(chain, layer.Diff.Digest)
|
2017-10-13 00:53:28 +00:00
|
|
|
}
|
2017-11-06 09:44:23 +00:00
|
|
|
chainID := identity.ChainID(chain)
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
if err := ii.fillBlobMapping(ctx, layers); err != nil {
|
|
|
|
return "", err
|
2017-10-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
return string(chainID), nil
|
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
|
2017-10-13 00:53:28 +00:00
|
|
|
func (ii *importInfo) fillBlobMapping(ctx context.Context, layers []rootfs.Layer) error {
|
|
|
|
var chain []digest.Digest
|
|
|
|
for _, l := range layers {
|
|
|
|
chain = append(chain, l.Diff.Digest)
|
|
|
|
chainID := identity.ChainID(chain)
|
2017-10-13 21:48:58 +00:00
|
|
|
if err := ii.opt.Snapshotter.(blobmapper).SetBlob(ctx, string(chainID), l.Diff.Digest, l.Blob.Digest); err != nil {
|
2017-10-13 00:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 22:04:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-13 00:53:28 +00:00
|
|
|
|
|
|
|
func (ii *importInfo) getLayers(ctx context.Context, dpairs []blobs.DiffPair) ([]rootfs.Layer, error) {
|
|
|
|
layers := make([]rootfs.Layer, len(dpairs))
|
|
|
|
for i := range dpairs {
|
|
|
|
layers[i].Diff = ocispec.Descriptor{
|
|
|
|
// TODO: derive media type from compressed type
|
|
|
|
MediaType: ocispec.MediaTypeImageLayer,
|
|
|
|
Digest: dpairs[i].DiffID,
|
|
|
|
}
|
|
|
|
info, err := ii.opt.ContentStore.Info(ctx, dpairs[i].Blobsum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers[i].Blob = ocispec.Descriptor{
|
|
|
|
// TODO: derive media type from compressed type
|
|
|
|
MediaType: ocispec.MediaTypeImageLayerGzip,
|
|
|
|
Digest: dpairs[i].Blobsum,
|
|
|
|
Size: info.Size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstructionCache interface {
|
|
|
|
Probe(ctx context.Context, key digest.Digest) (bool, error)
|
2017-10-15 05:39:28 +00:00
|
|
|
Lookup(ctx context.Context, key digest.Digest, msg string) (interface{}, error) // TODO: regular ref
|
2017-10-13 00:53:28 +00:00
|
|
|
Set(key digest.Digest, ref interface{}) error
|
|
|
|
SetContentMapping(contentKey, key digest.Digest) error
|
|
|
|
GetContentMapping(dgst digest.Digest) ([]digest.Digest, error)
|
|
|
|
}
|
2017-10-15 05:39:28 +00:00
|
|
|
|
|
|
|
func inVertexContext(ctx context.Context, name string, f func(ctx context.Context) error) error {
|
|
|
|
v := client.Vertex{
|
|
|
|
Digest: digest.FromBytes([]byte(buildkitidentity.NewID())),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
pw, _, ctx := progress.FromContext(ctx, progress.WithMetadata("vertex", v.Digest))
|
|
|
|
notifyStarted(ctx, &v)
|
|
|
|
defer pw.Close()
|
|
|
|
err := f(ctx)
|
|
|
|
notifyCompleted(ctx, &v, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func notifyStarted(ctx context.Context, v *client.Vertex) {
|
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
|
|
|
defer pw.Close()
|
|
|
|
now := time.Now()
|
|
|
|
v.Started = &now
|
|
|
|
v.Completed = nil
|
|
|
|
pw.Write(v.Digest.String(), *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func notifyCompleted(ctx context.Context, v *client.Vertex, err error) {
|
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
|
|
|
defer pw.Close()
|
|
|
|
now := time.Now()
|
|
|
|
if v.Started == nil {
|
|
|
|
v.Started = &now
|
|
|
|
}
|
|
|
|
v.Completed = &now
|
|
|
|
v.Cached = false
|
|
|
|
if err != nil {
|
|
|
|
v.Error = err.Error()
|
|
|
|
}
|
|
|
|
pw.Write(v.Digest.String(), *v)
|
|
|
|
}
|