Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various lazy blob errors involving dedupe by chainID #5560

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,
descHandlers := descHandlersOf(opts...)
if desc.Digest != "" && (descHandlers == nil || descHandlers[desc.Digest] == nil) {
if _, err := cm.ContentStore.Info(ctx, desc.Digest); errors.Is(err, cerrdefs.ErrNotFound) {
return nil, NeedsRemoteProviderError([]digest.Digest{desc.Digest})
return nil, NeedsRemoteProviderError([]DigestDescriptionPair{{Digest: desc.Digest}})
} else if err != nil {
return nil, err
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func (cm *cacheManager) getRecord(ctx context.Context, id string, opts ...RefOpt
if isLazy, err := cr.isLazy(ctx); err != nil {
return err
} else if isLazy && dhs[blob] == nil {
missing = append(missing, blob)
missing = append(missing, DigestDescriptionPair{Digest: blob, Description: cr.GetDescription()})
}
return nil
}); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions cache/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ func descHandlersOf(opts ...RefOption) DescHandlers {

type DescHandlerKey digest.Digest

type NeedsRemoteProviderError []digest.Digest //nolint:errname
type NeedsRemoteProviderError []DigestDescriptionPair //nolint:errname

type DigestDescriptionPair struct {
Digest digest.Digest
Description string
}

func (d DigestDescriptionPair) String() string {
return fmt.Sprintf("%s: %s", d.Digest, d.Description)
}

func (m NeedsRemoteProviderError) Error() string {
return fmt.Sprintf("missing descriptor handlers for lazy blobs %+v", []digest.Digest(m))
return fmt.Sprintf("missing descriptor handlers for lazy blobs %+v", []DigestDescriptionPair(m))
}

type Unlazy session.Group
Expand Down
15 changes: 15 additions & 0 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Ref interface {
RefMetadata
Release(context.Context) error
IdentityMapping() *idtools.IdentityMapping
DescHandlers() DescHandlers
DescHandler(digest.Digest) *DescHandler
}

Expand Down Expand Up @@ -612,6 +613,13 @@ func (sr *immutableRef) LayerChain() RefList {
return l
}

func (sr *immutableRef) DescHandlers() DescHandlers {
// clone to prevent mutation of internal state
dhs := make(DescHandlers)
maps.Copy(dhs, sr.descHandlers)
return dhs
}

func (sr *immutableRef) DescHandler(dgst digest.Digest) *DescHandler {
return sr.descHandlers[dgst]
}
Expand Down Expand Up @@ -640,6 +648,13 @@ func (sr *mutableRef) traceLogFields() logrus.Fields {
return m
}

func (sr *mutableRef) DescHandlers() DescHandlers {
// clone to prevent mutation of internal state
dhs := make(DescHandlers)
maps.Copy(dhs, sr.descHandlers)
return dhs
}

func (sr *mutableRef) DescHandler(dgst digest.Digest) *DescHandler {
return sr.descHandlers[dgst]
}
Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/azblob/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (ci *importer) loadManifest(ctx context.Context, name string) (*v1.CacheCha
progress.OneOff(ctx, fmt.Sprintf("found %d layers in cache", len(allLayers)))(nil)

cc := v1.NewCacheChains()
if err := v1.ParseConfig(config, allLayers, cc); err != nil {
if err := v1.ParseConfig(ctx, config, allLayers, cc); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/gha/gha.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (ci *importer) loadScope(ctx context.Context, scope string) (*v1.CacheChain
}

cc := v1.NewCacheChains()
if err := v1.ParseConfig(config, allLayers, cc); err != nil {
if err := v1.ParseConfig(ctx, config, allLayers, cc); err != nil {
return nil, err
}
return cc, nil
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (ci *contentCacheImporter) Resolve(ctx context.Context, desc ocispecs.Descr
}

cc := v1.NewCacheChains()
if err := v1.Parse(dt, allLayers, cc); err != nil {
if err := v1.Parse(ctx, dt, allLayers, cc); err != nil {
return nil, err
}

Expand Down Expand Up @@ -238,7 +238,7 @@ func (ci *contentCacheImporter) importInlineCache(ctx context.Context, dt []byte
return errors.WithStack(err)
}
cc := v1.NewCacheChains()
if err := v1.ParseConfig(config, layers, cc); err != nil {
if err := v1.ParseConfig(ctx, config, layers, cc); err != nil {
return err
}
mu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest)
}

cc := v1.NewCacheChains()
if err := v1.ParseConfig(*config, descs2, cc); err != nil {
if err := v1.ParseConfig(ctx, *config, descs2, cc); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (i *importer) load(ctx context.Context) (*v1.CacheChains, error) {
}

cc := v1.NewCacheChains()
if err := v1.ParseConfig(config, allLayers, cc); err != nil {
if err := v1.ParseConfig(ctx, config, allLayers, cc); err != nil {
return nil, err
}
return cc, nil
Expand Down
25 changes: 23 additions & 2 deletions cache/remotecache/v1/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"time"

"github.com/containerd/containerd/content"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -215,7 +217,26 @@ func (c *item) removeLink(src *item) bool {
return found
}

func (c *item) AddResult(_ digest.Digest, _ int, createdAt time.Time, result *solver.Remote) {
func (c *item) AddResult(ctx context.Context, _ digest.Digest, _ int, createdAt time.Time, result *solver.Remote) {
if result == nil {
return
}

if len(result.Descriptors) == 0 {
bklog.G(ctx).Warnf("no descriptors for item %s result, skipping", c.dgst)
return
}

if result.Provider != nil {
for _, desc := range result.Descriptors {
_, err := result.Provider.Info(ctx, desc.Digest)
if err != nil && !cerrdefs.IsNotFound(err) {
bklog.G(ctx).Warnf("failed to get info for item %s descriptor %s, skipping item result: %v", c.dgst, desc.Digest, err)
return
}
}
}

c.resultTime = createdAt
c.result = result
}
Expand Down Expand Up @@ -305,7 +326,7 @@ func (c *item) walkAllResults(fn func(i *item) error, visited map[*item]struct{}
type nopRecord struct {
}

func (c *nopRecord) AddResult(_ digest.Digest, _ int, createdAt time.Time, result *solver.Remote) {
func (c *nopRecord) AddResult(_ context.Context, _ digest.Digest, _ int, createdAt time.Time, result *solver.Remote) {
}

func (c *nopRecord) LinkFrom(rec solver.CacheExporterRecord, index int, selector string) {
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/v1/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSimpleMarshal(t *testing.T) {
Digest: dgst("d1"),
}},
}
baz.AddResult("", 0, time.Now(), r0)
baz.AddResult(context.TODO(), "", 0, time.Now(), r0)
}

addRecords()
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestSimpleMarshal(t *testing.T) {
require.NoError(t, err)

newChains := NewCacheChains()
err = Parse(dt, descPairs, newChains)
err = Parse(context.TODO(), dt, descPairs, newChains)
require.NoError(t, err)

cfg3, _, err := cc.Marshal(context.TODO())
Expand Down
17 changes: 9 additions & 8 deletions cache/remotecache/v1/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cacheimport

import (
"context"
"encoding/json"

"github.com/moby/buildkit/solver"
Expand All @@ -9,27 +10,27 @@ import (
"github.com/pkg/errors"
)

func Parse(configJSON []byte, provider DescriptorProvider, t solver.CacheExporterTarget) error {
func Parse(ctx context.Context, configJSON []byte, provider DescriptorProvider, t solver.CacheExporterTarget) error {
var config CacheConfig
if err := json.Unmarshal(configJSON, &config); err != nil {
return errors.WithStack(err)
}

return ParseConfig(config, provider, t)
return ParseConfig(ctx, config, provider, t)
}

func ParseConfig(config CacheConfig, provider DescriptorProvider, t solver.CacheExporterTarget) error {
func ParseConfig(ctx context.Context, config CacheConfig, provider DescriptorProvider, t solver.CacheExporterTarget) error {
cache := map[int]solver.CacheExporterRecord{}

for i := range config.Records {
if _, err := parseRecord(config, i, provider, t, cache); err != nil {
if _, err := parseRecord(ctx, config, i, provider, t, cache); err != nil {
return err
}
}
return nil
}

func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.CacheExporterTarget, cache map[int]solver.CacheExporterRecord) (solver.CacheExporterRecord, error) {
func parseRecord(ctx context.Context, cc CacheConfig, idx int, provider DescriptorProvider, t solver.CacheExporterTarget, cache map[int]solver.CacheExporterRecord) (solver.CacheExporterRecord, error) {
if r, ok := cache[idx]; ok {
if r == nil {
return nil, errors.Errorf("invalid looping record")
Expand All @@ -46,7 +47,7 @@ func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.
cache[idx] = nil
for i, inputs := range rec.Inputs {
for _, inp := range inputs {
src, err := parseRecord(cc, inp.LinkIndex, provider, t, cache)
src, err := parseRecord(ctx, cc, inp.LinkIndex, provider, t, cache)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +62,7 @@ func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.
return nil, err
}
if remote != nil {
r.AddResult("", 0, res.CreatedAt, remote)
r.AddResult(ctx, "", 0, res.CreatedAt, remote)
}
}

Expand All @@ -86,7 +87,7 @@ func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.
}
if remote != nil {
remote.Provider = mp
r.AddResult("", 0, res.CreatedAt, remote)
r.AddResult(ctx, "", 0, res.CreatedAt, remote)
}
}

Expand Down
15 changes: 0 additions & 15 deletions cache/remotecache/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"sort"

cerrdefs "github.com/containerd/errdefs"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
digest "github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -275,20 +274,6 @@ type marshalState struct {
}

func marshalRemote(ctx context.Context, r *solver.Remote, state *marshalState) string {
if len(r.Descriptors) == 0 {
return ""
}

if r.Provider != nil {
for _, d := range r.Descriptors {
if _, err := r.Provider.Info(ctx, d.Digest); err != nil {
if !cerrdefs.IsNotImplemented(err) {
return ""
}
}
}
}

var parentID string
if len(r.Descriptors) > 1 {
r2 := &solver.Remote{
Expand Down
Loading
Loading