Skip to content

Commit 85e9df3

Browse files
authored
Merge pull request moby#4558 from vvoland/multiprovider-info
util/multiprovider: Implement Info
2 parents 8e3fe35 + 55afcdb commit 85e9df3

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

cache/remote.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/moby/buildkit/util/leaseutil"
1919
"github.com/moby/buildkit/util/progress/logs"
2020
"github.com/moby/buildkit/util/pull/pullprogress"
21+
digest "github.com/opencontainers/go-digest"
2122
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
2223
"github.com/pkg/errors"
2324
"golang.org/x/sync/errgroup"
@@ -85,7 +86,7 @@ func (sr *immutableRef) GetRemotes(ctx context.Context, createIfNeeded bool, ref
8586
return res, nil
8687
}
8788

88-
func appendRemote(parents []*solver.Remote, desc ocispecs.Descriptor, p content.Provider) (res []*solver.Remote) {
89+
func appendRemote(parents []*solver.Remote, desc ocispecs.Descriptor, p content.InfoReaderProvider) (res []*solver.Remote) {
8990
for _, pRemote := range parents {
9091
provider := contentutil.NewMultiProvider(pRemote.Provider)
9192
provider.Add(desc.Digest, p)
@@ -276,6 +277,10 @@ func (mp *lazyMultiProvider) ReaderAt(ctx context.Context, desc ocispecs.Descrip
276277
return mp.mprovider.ReaderAt(ctx, desc)
277278
}
278279

280+
func (mp *lazyMultiProvider) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
281+
return mp.mprovider.Info(ctx, dgst)
282+
}
283+
279284
func (mp *lazyMultiProvider) Unlazy(ctx context.Context) error {
280285
eg, egctx := errgroup.WithContext(ctx)
281286
for _, p := range mp.plist {
@@ -304,6 +309,16 @@ func (p lazyRefProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor)
304309
return p.ref.cm.ContentStore.ReaderAt(ctx, desc)
305310
}
306311

312+
func (p lazyRefProvider) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
313+
if dgst != p.desc.Digest {
314+
return content.Info{}, errdefs.ErrNotFound
315+
}
316+
if err := p.Unlazy(ctx); err != nil {
317+
return content.Info{}, errdefs.ErrNotFound
318+
}
319+
return p.ref.cm.ContentStore.Info(ctx, dgst)
320+
}
321+
307322
func (p lazyRefProvider) Unlazy(ctx context.Context) error {
308323
_, err := p.ref.cm.unlazyG.Do(ctx, string(p.desc.Digest), func(ctx context.Context) (_ struct{}, rerr error) {
309324
if isLazy, err := p.ref.isLazy(ctx); err != nil {

cache/remotecache/v1/chains.go

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/moby/buildkit/solver"
1111
digest "github.com/opencontainers/go-digest"
1212
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
13+
"github.com/pkg/errors"
1314
)
1415

1516
func NewCacheChains() *CacheChains {
@@ -120,6 +121,20 @@ type DescriptorProviderPair struct {
120121
Provider content.Provider
121122
}
122123

124+
func (p DescriptorProviderPair) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
125+
return p.Provider.ReaderAt(ctx, desc)
126+
}
127+
128+
func (p DescriptorProviderPair) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
129+
if dgst != p.Descriptor.Digest {
130+
return content.Info{}, errors.Errorf("content not found %s", dgst)
131+
}
132+
return content.Info{
133+
Digest: p.Descriptor.Digest,
134+
Size: p.Descriptor.Size,
135+
}, nil
136+
}
137+
123138
// item is an implementation of a record in the cache chain. After validation,
124139
// normalization and marshalling into the cache config, the item results form
125140
// into the "layers", while the digests and the links form into the "records".

cache/remotecache/v1/parse.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.
8282
}
8383

8484
remote.Descriptors = append(remote.Descriptors, descPair.Descriptor)
85-
mp.Add(descPair.Descriptor.Digest, descPair.Provider)
85+
mp.Add(descPair.Descriptor.Digest, descPair)
8686
}
8787
if remote != nil {
8888
remote.Provider = mp
@@ -123,12 +123,12 @@ func getRemoteChain(layers []CacheLayer, idx int, provider DescriptorProvider, v
123123
}
124124
r.Descriptors = append(r.Descriptors, descPair.Descriptor)
125125
mp := contentutil.NewMultiProvider(r.Provider)
126-
mp.Add(descPair.Descriptor.Digest, descPair.Provider)
126+
mp.Add(descPair.Descriptor.Digest, descPair)
127127
r.Provider = mp
128128
return r, nil
129129
}
130130
return &solver.Remote{
131131
Descriptors: []ocispecs.Descriptor{descPair.Descriptor},
132-
Provider: descPair.Provider,
132+
Provider: descPair,
133133
}, nil
134134
}

solver/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type CacheExporterRecord interface {
142142
// TODO: add closer to keep referenced data from getting deleted
143143
type Remote struct {
144144
Descriptors []ocispecs.Descriptor
145-
Provider content.Provider
145+
Provider content.InfoReaderProvider
146146
}
147147

148148
// CacheLink is a link between two cache records

util/contentutil/multiprovider.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import (
1313
)
1414

1515
// NewMultiProvider creates a new mutable provider with a base provider
16-
func NewMultiProvider(base content.Provider) *MultiProvider {
16+
func NewMultiProvider(base content.InfoReaderProvider) *MultiProvider {
1717
return &MultiProvider{
1818
base: base,
19-
sub: map[digest.Digest]content.Provider{},
19+
sub: map[digest.Digest]content.InfoReaderProvider{},
2020
}
2121
}
2222

2323
// MultiProvider is a provider backed by a mutable map of providers
2424
type MultiProvider struct {
2525
mu sync.RWMutex
26-
base content.Provider
27-
sub map[digest.Digest]content.Provider
26+
base content.InfoReaderProvider
27+
sub map[digest.Digest]content.InfoReaderProvider
2828
}
2929

3030
func (mp *MultiProvider) SnapshotLabels(descs []ocispecs.Descriptor, index int) map[string]string {
@@ -85,8 +85,22 @@ func (mp *MultiProvider) ReaderAt(ctx context.Context, desc ocispecs.Descriptor)
8585
return mp.base.ReaderAt(ctx, desc)
8686
}
8787

88+
// Info returns a content.Info
89+
func (mp *MultiProvider) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
90+
mp.mu.RLock()
91+
if p, ok := mp.sub[dgst]; ok {
92+
mp.mu.RUnlock()
93+
return p.Info(ctx, dgst)
94+
}
95+
mp.mu.RUnlock()
96+
if mp.base == nil {
97+
return content.Info{}, errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
98+
}
99+
return mp.base.Info(ctx, dgst)
100+
}
101+
88102
// Add adds a new child provider for a specific digest
89-
func (mp *MultiProvider) Add(dgst digest.Digest, p content.Provider) {
103+
func (mp *MultiProvider) Add(dgst digest.Digest, p content.InfoReaderProvider) {
90104
mp.mu.Lock()
91105
defer mp.mu.Unlock()
92106
mp.sub[dgst] = p

0 commit comments

Comments
 (0)