2017-05-27 05:10:26 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
2017-07-07 21:35:10 +00:00
|
|
|
gocontext "context"
|
2017-07-25 19:11:52 +00:00
|
|
|
"fmt"
|
2017-05-27 05:10:26 +00:00
|
|
|
"net/http"
|
2017-06-15 23:08:20 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-09-05 18:26:03 +00:00
|
|
|
"github.com/BurntSushi/locker"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2017-07-01 04:58:51 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2017-09-15 20:21:02 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/containerd/containerd/remotes"
|
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
|
|
|
"github.com/containerd/containerd/rootfs"
|
|
|
|
"github.com/containerd/containerd/snapshot"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/source"
|
2017-09-05 18:26:03 +00:00
|
|
|
"github.com/moby/buildkit/util/imageutil"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-05-31 22:36:04 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/opencontainers/image-spec/identity"
|
2017-05-27 05:10:26 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
2017-07-07 21:35:10 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-05-27 05:10:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: break apart containerd specifics like contentstore so the resolver
|
|
|
|
// code can be used with any implementation
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
type SourceOpt struct {
|
2017-05-30 21:17:04 +00:00
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
|
|
|
Applier rootfs.Applier
|
|
|
|
CacheAccessor cache.Accessor
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 22:36:04 +00:00
|
|
|
type blobmapper interface {
|
2017-07-07 21:35:10 +00:00
|
|
|
GetBlob(ctx gocontext.Context, key string) (digest.Digest, error)
|
|
|
|
SetBlob(ctx gocontext.Context, key string, blob digest.Digest) error
|
2017-05-31 22:36:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 18:26:03 +00:00
|
|
|
type resolveRecord struct {
|
|
|
|
desc ocispec.Descriptor
|
|
|
|
ts time.Time
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:10:26 +00:00
|
|
|
type imageSource struct {
|
2017-05-27 06:12:13 +00:00
|
|
|
SourceOpt
|
2017-05-27 05:10:26 +00:00
|
|
|
resolver remotes.Resolver
|
2017-09-05 18:26:03 +00:00
|
|
|
lru map[string]resolveRecord
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
func NewSource(opt SourceOpt) (source.Source, error) {
|
2017-05-27 05:10:26 +00:00
|
|
|
is := &imageSource{
|
2017-05-27 06:12:13 +00:00
|
|
|
SourceOpt: opt,
|
2017-09-05 18:26:03 +00:00
|
|
|
lru: map[string]resolveRecord{},
|
|
|
|
resolver: newCachedResolver(docker.NewResolver(docker.ResolverOptions{
|
2017-05-27 05:10:26 +00:00
|
|
|
Client: http.DefaultClient,
|
2017-09-05 18:26:03 +00:00
|
|
|
}), 5*time.Second),
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-05-31 22:36:04 +00:00
|
|
|
|
|
|
|
if _, ok := opt.Snapshotter.(blobmapper); !ok {
|
|
|
|
return nil, errors.Errorf("imagesource requires snapshotter with blobs mapping support")
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:10:26 +00:00
|
|
|
return is, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *imageSource) ID() string {
|
2017-05-27 06:12:13 +00:00
|
|
|
return source.DockerImageScheme
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 22:49:37 +00:00
|
|
|
func (is *imageSource) ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error) {
|
2017-09-05 18:26:03 +00:00
|
|
|
return imageutil.Config(ctx, ref, is.resolver, is.ContentStore)
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
func (is *imageSource) Resolve(ctx context.Context, id source.Identifier) (source.SourceInstance, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
imageIdentifier, ok := id.(*source.ImageIdentifier)
|
2017-05-27 05:10:26 +00:00
|
|
|
if !ok {
|
2017-07-07 21:35:10 +00:00
|
|
|
return nil, errors.Errorf("invalid image identifier %v", id)
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
p := &puller{
|
|
|
|
src: imageIdentifier,
|
|
|
|
is: is,
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:26:03 +00:00
|
|
|
type puller struct {
|
|
|
|
is *imageSource
|
|
|
|
resolveOnce sync.Once
|
|
|
|
src *source.ImageIdentifier
|
|
|
|
desc ocispec.Descriptor
|
|
|
|
ref string
|
|
|
|
resolveErr error
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
func (p *puller) resolve(ctx context.Context) error {
|
|
|
|
p.resolveOnce.Do(func() {
|
|
|
|
resolveProgressDone := oneOffProgress(ctx, "resolve "+p.src.Reference.String())
|
2017-09-05 18:26:03 +00:00
|
|
|
|
|
|
|
dgst := p.src.Reference.Digest()
|
|
|
|
if dgst != "" {
|
|
|
|
info, err := p.is.ContentStore.Info(ctx, dgst)
|
|
|
|
if err == nil {
|
|
|
|
p.ref = p.src.Reference.String()
|
2017-09-22 22:49:37 +00:00
|
|
|
ra, err := p.is.ContentStore.ReaderAt(ctx, dgst)
|
|
|
|
if err == nil {
|
|
|
|
mt, err := imageutil.DetectManifestMediaType(ra)
|
|
|
|
if err == nil {
|
|
|
|
p.desc = ocispec.Descriptor{
|
|
|
|
Size: info.Size,
|
|
|
|
Digest: dgst,
|
|
|
|
MediaType: mt,
|
|
|
|
}
|
|
|
|
resolveProgressDone(nil)
|
|
|
|
return
|
|
|
|
}
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
ref, desc, err := p.is.resolver.Resolve(ctx, p.src.Reference.String())
|
|
|
|
if err != nil {
|
|
|
|
p.resolveErr = err
|
|
|
|
resolveProgressDone(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.desc = desc
|
|
|
|
p.ref = ref
|
|
|
|
resolveProgressDone(nil)
|
|
|
|
})
|
|
|
|
return p.resolveErr
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:15:54 +00:00
|
|
|
func (p *puller) CacheKey(ctx context.Context) (string, error) {
|
2017-07-06 19:07:42 +00:00
|
|
|
if err := p.resolve(ctx); err != nil {
|
2017-07-06 20:15:54 +00:00
|
|
|
return "", err
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
2017-07-06 20:15:54 +00:00
|
|
|
return p.desc.Digest.String(), nil
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) Snapshot(ctx context.Context) (cache.ImmutableRef, error) {
|
|
|
|
if err := p.resolve(ctx); err != nil {
|
|
|
|
return nil, err
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-06-15 23:08:20 +00:00
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
ongoing := newJobs(p.ref)
|
2017-06-17 05:12:27 +00:00
|
|
|
|
2017-06-15 23:08:20 +00:00
|
|
|
pctx, stopProgress := context.WithCancel(ctx)
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
go showProgress(pctx, ongoing, p.is.ContentStore)
|
2017-06-15 23:08:20 +00:00
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
fetcher, err := p.is.resolver.Fetcher(ctx, p.ref)
|
2017-05-27 05:10:26 +00:00
|
|
|
if err != nil {
|
2017-06-15 23:34:42 +00:00
|
|
|
stopProgress()
|
2017-05-27 05:10:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:17:04 +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
|
2017-05-27 05:10:26 +00:00
|
|
|
handlers := []images.Handler{
|
2017-07-07 21:35:10 +00:00
|
|
|
images.HandlerFunc(func(ctx gocontext.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
|
2017-06-17 05:12:27 +00:00
|
|
|
ongoing.add(desc)
|
2017-06-15 23:08:20 +00:00
|
|
|
return nil, nil
|
|
|
|
}),
|
2017-07-06 19:07:42 +00:00
|
|
|
remotes.FetchHandler(p.is.ContentStore, fetcher),
|
|
|
|
images.ChildrenHandler(p.is.ContentStore),
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-07-06 19:07:42 +00:00
|
|
|
if err := images.Dispatch(ctx, images.Handlers(handlers...), p.desc); err != nil {
|
2017-06-15 23:34:42 +00:00
|
|
|
stopProgress()
|
2017-05-27 05:10:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-15 23:08:20 +00:00
|
|
|
stopProgress()
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
unpackProgressDone := oneOffProgress(ctx, "unpacking "+p.src.Reference.String())
|
|
|
|
chainid, err := p.is.unpack(ctx, p.desc)
|
2017-05-30 21:17:04 +00:00
|
|
|
if err != nil {
|
2017-06-15 23:08:20 +00:00
|
|
|
return nil, unpackProgressDone(err)
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-06-15 23:08:20 +00:00
|
|
|
unpackProgressDone(nil)
|
2017-05-30 21:17:04 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
return p.is.CacheAccessor.Get(ctx, chainid, cache.WithDescription(fmt.Sprintf("pulled from %s", p.ref)))
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
func (is *imageSource) unpack(ctx context.Context, desc ocispec.Descriptor) (string, error) {
|
2017-05-27 05:10:26 +00:00
|
|
|
layers, err := getLayers(ctx, is.ContentStore, desc)
|
|
|
|
if err != nil {
|
2017-05-30 21:17:04 +00:00
|
|
|
return "", err
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
chainID, err := rootfs.ApplyLayers(ctx, layers, is.Snapshotter, is.Applier)
|
2017-05-27 05:10:26 +00:00
|
|
|
if err != nil {
|
2017-05-30 21:17:04 +00:00
|
|
|
return "", err
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-05-31 22:36:04 +00:00
|
|
|
|
|
|
|
if err := is.fillBlobMapping(ctx, layers); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
return string(chainID), nil
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 22:36:04 +00:00
|
|
|
func (is *imageSource) 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)
|
|
|
|
if err := is.SourceOpt.Snapshotter.(blobmapper).SetBlob(ctx, string(chainID), l.Blob.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:10:26 +00:00
|
|
|
func getLayers(ctx context.Context, provider content.Provider, desc ocispec.Descriptor) ([]rootfs.Layer, error) {
|
2017-09-15 20:21:02 +00:00
|
|
|
manifest, err := images.Manifest(ctx, provider, desc, platforms.Format(platforms.Default()))
|
2017-05-27 05:10:26 +00:00
|
|
|
if err != nil {
|
2017-09-15 20:21:02 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
image := images.Image{Target: desc}
|
2017-09-15 20:21:02 +00:00
|
|
|
diffIDs, err := image.RootFS(ctx, provider, platforms.Format(platforms.Default()))
|
2017-05-27 05:10:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to resolve rootfs")
|
|
|
|
}
|
|
|
|
if len(diffIDs) != len(manifest.Layers) {
|
2017-09-15 20:21:02 +00:00
|
|
|
return nil, errors.Errorf("mismatched image rootfs and manifest layers %+v %+v", diffIDs, manifest.Layers)
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
layers := make([]rootfs.Layer, len(diffIDs))
|
|
|
|
for i := range diffIDs {
|
|
|
|
layers[i].Diff = ocispec.Descriptor{
|
|
|
|
// TODO: derive media type from compressed type
|
|
|
|
MediaType: ocispec.MediaTypeImageLayer,
|
|
|
|
Digest: diffIDs[i],
|
|
|
|
}
|
|
|
|
layers[i].Blob = manifest.Layers[i]
|
|
|
|
}
|
|
|
|
return layers, nil
|
|
|
|
}
|
2017-06-15 23:08:20 +00:00
|
|
|
|
|
|
|
func showProgress(ctx context.Context, ongoing *jobs, cs content.Store) {
|
|
|
|
var (
|
2017-06-17 05:12:27 +00:00
|
|
|
ticker = time.NewTicker(100 * time.Millisecond)
|
2017-06-15 23:08:20 +00:00
|
|
|
statuses = map[string]statusInfo{}
|
|
|
|
done bool
|
|
|
|
)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
pw, _, ctx := progress.FromContext(ctx)
|
|
|
|
defer pw.Close()
|
|
|
|
|
2017-06-15 23:08:20 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ctx.Done():
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved := "resolved"
|
|
|
|
if !ongoing.isResolved() {
|
|
|
|
resolved = "resolving"
|
|
|
|
}
|
|
|
|
statuses[ongoing.name] = statusInfo{
|
|
|
|
Ref: ongoing.name,
|
|
|
|
Status: resolved,
|
|
|
|
}
|
|
|
|
|
|
|
|
actives := make(map[string]statusInfo)
|
|
|
|
|
|
|
|
if !done {
|
2017-07-13 06:15:12 +00:00
|
|
|
active, err := cs.ListStatuses(ctx, "")
|
2017-06-15 23:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
// log.G(ctx).WithError(err).Error("active check failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// update status of active entries!
|
|
|
|
for _, active := range active {
|
|
|
|
actives[active.Ref] = statusInfo{
|
|
|
|
Ref: active.Ref,
|
|
|
|
Status: "downloading",
|
|
|
|
Offset: active.Offset,
|
|
|
|
Total: active.Total,
|
|
|
|
StartedAt: active.StartedAt,
|
|
|
|
UpdatedAt: active.UpdatedAt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, update the items in jobs that are not in active
|
|
|
|
for _, j := range ongoing.jobs() {
|
|
|
|
refKey := remotes.MakeRefKey(ctx, j.Descriptor)
|
|
|
|
if a, ok := actives[refKey]; ok {
|
2017-09-26 03:57:38 +00:00
|
|
|
started := j.started
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.Write(j.Digest.String(), progress.Status{
|
2017-06-15 23:08:20 +00:00
|
|
|
Action: a.Status,
|
|
|
|
Total: int(a.Total),
|
|
|
|
Current: int(a.Offset),
|
2017-09-26 03:57:38 +00:00
|
|
|
Started: &started,
|
2017-06-15 23:08:20 +00:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !j.done {
|
2017-06-22 19:13:25 +00:00
|
|
|
info, err := cs.Info(context.TODO(), j.Digest)
|
2017-06-15 23:08:20 +00:00
|
|
|
if err != nil {
|
2017-07-01 04:58:51 +00:00
|
|
|
if errdefs.IsNotFound(err) {
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.Write(j.Digest.String(), progress.Status{
|
2017-06-15 23:08:20 +00:00
|
|
|
Action: "waiting",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
j.done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if done || j.done {
|
2017-09-26 03:57:38 +00:00
|
|
|
started := j.started
|
|
|
|
createdAt := info.CreatedAt
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.Write(j.Digest.String(), progress.Status{
|
2017-06-15 23:08:20 +00:00
|
|
|
Action: "done",
|
|
|
|
Current: int(info.Size),
|
|
|
|
Total: int(info.Size),
|
2017-09-26 03:57:38 +00:00
|
|
|
Completed: &createdAt,
|
|
|
|
Started: &started,
|
2017-06-15 23:08:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// jobs provides a way of identifying the download keys for a particular task
|
|
|
|
// encountering during the pull walk.
|
|
|
|
//
|
|
|
|
// This is very minimal and will probably be replaced with something more
|
|
|
|
// featured.
|
|
|
|
type jobs struct {
|
|
|
|
name string
|
|
|
|
added map[digest.Digest]job
|
|
|
|
mu sync.Mutex
|
|
|
|
resolved bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type job struct {
|
|
|
|
ocispec.Descriptor
|
|
|
|
done bool
|
|
|
|
started time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJobs(name string) *jobs {
|
|
|
|
return &jobs{
|
|
|
|
name: name,
|
|
|
|
added: make(map[digest.Digest]job),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (j *jobs) add(desc ocispec.Descriptor) {
|
2017-06-15 23:08:20 +00:00
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := j.added[desc.Digest]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
j.added[desc.Digest] = job{
|
2017-06-17 05:12:27 +00:00
|
|
|
Descriptor: desc,
|
|
|
|
started: time.Now(),
|
2017-06-15 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jobs) jobs() []job {
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
descs := make([]job, 0, len(j.added))
|
|
|
|
for _, j := range j.added {
|
|
|
|
descs = append(descs, j)
|
|
|
|
}
|
|
|
|
return descs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jobs) isResolved() bool {
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
return j.resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
type statusInfo struct {
|
|
|
|
Ref string
|
|
|
|
Status string
|
|
|
|
Offset int64
|
|
|
|
Total int64
|
|
|
|
StartedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneOffProgress(ctx context.Context, id string) func(err error) error {
|
2017-06-17 05:12:27 +00:00
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
2017-06-15 23:08:20 +00:00
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
}
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.Write(id, st)
|
2017-06-15 23:08:20 +00:00
|
|
|
return func(err error) error {
|
|
|
|
// TODO: set error on status
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.Write(id, st)
|
|
|
|
pw.Close()
|
2017-06-15 23:08:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 18:26:03 +00:00
|
|
|
|
|
|
|
func newCachedResolver(r remotes.Resolver, timeout time.Duration) remotes.Resolver {
|
|
|
|
return &cachedResolver{
|
|
|
|
Resolver: r,
|
|
|
|
cache: map[string]cachedResult{},
|
|
|
|
timeout: timeout,
|
|
|
|
locker: locker.NewLocker(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedResolver struct {
|
|
|
|
remotes.Resolver
|
|
|
|
cache map[string]cachedResult
|
|
|
|
timeout time.Duration
|
|
|
|
locker *locker.Locker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *cachedResolver) Resolve(ctx gocontext.Context, ref string) (name string, desc ocispec.Descriptor, err error) {
|
|
|
|
cr.locker.Lock(ref)
|
|
|
|
defer cr.locker.Unlock(ref)
|
|
|
|
r, ok := cr.cache[ref]
|
|
|
|
if ok && time.Since(r.ts) < cr.timeout {
|
|
|
|
return r.name, r.desc, nil
|
|
|
|
}
|
|
|
|
delete(cr.cache, ref)
|
|
|
|
n, d, err := cr.Resolver.Resolve(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return "", d, err
|
|
|
|
}
|
|
|
|
cr.cache[ref] = cachedResult{
|
|
|
|
name: n,
|
|
|
|
desc: d,
|
|
|
|
ts: time.Now(),
|
|
|
|
}
|
|
|
|
return n, d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedResult struct {
|
|
|
|
name string
|
|
|
|
desc ocispec.Descriptor
|
|
|
|
ts time.Time
|
|
|
|
}
|