Skip to content

Commit

Permalink
Merge pull request #2433 from tonistiigi/gha-export-fix
Browse files Browse the repository at this point in the history
gha: fix handling removed blobs on reexport
  • Loading branch information
AkihiroSuda authored Nov 1, 2021
2 parents d47b46c + 962c287 commit 0279989
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cache/remotecache/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewExporter(ingester content.Ingester, ref string, oci bool) Exporter {

func (ce *contentCacheExporter) Finalize(ctx context.Context) (map[string]string, error) {
res := make(map[string]string)
config, descs, err := ce.chains.Marshal()
config, descs, err := ce.chains.Marshal(ctx)
if 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 @@ -106,7 +106,7 @@ func (ce *exporter) indexKey() string {

func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
// res := make(map[string]string)
config, descs, err := ce.chains.Marshal()
config, descs, err := ce.chains.Marshal(ctx)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cache/remotecache/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (ce *exporter) reset() {
ce.chains = cc
}

func (ce *exporter) ExportForLayers(layers []digest.Digest) ([]byte, error) {
config, descs, err := ce.chains.Marshal()
func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest) ([]byte, error) {
config, descs, err := ce.chains.Marshal(ctx)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +63,7 @@ func (ce *exporter) ExportForLayers(layers []digest.Digest) ([]byte, error) {
return nil, err
}

cfg, _, err := cc.Marshal()
cfg, _, err := cc.Marshal(ctx)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions cache/remotecache/v1/chains.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cacheimport

import (
"context"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *CacheChains) normalize() error {
return nil
}

func (c *CacheChains) Marshal() (*CacheConfig, DescriptorProvider, error) {
func (c *CacheChains) Marshal(ctx context.Context) (*CacheConfig, DescriptorProvider, error) {
if err := c.normalize(); err != nil {
return nil, nil, err
}
Expand All @@ -87,7 +88,7 @@ func (c *CacheChains) Marshal() (*CacheConfig, DescriptorProvider, error) {
}

for _, it := range c.items {
if err := marshalItem(it, st); err != nil {
if err := marshalItem(ctx, it, st); err != nil {
return nil, nil, err
}
}
Expand Down
9 changes: 5 additions & 4 deletions cache/remotecache/v1/chains_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cacheimport

import (
"context"
"encoding/json"
"testing"
"time"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestSimpleMarshal(t *testing.T) {

addRecords()

cfg, _, err := cc.Marshal()
cfg, _, err := cc.Marshal(context.TODO())
require.NoError(t, err)

require.Equal(t, len(cfg.Layers), 2)
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestSimpleMarshal(t *testing.T) {
// adding same info again doesn't produce anything extra
addRecords()

cfg2, descPairs, err := cc.Marshal()
cfg2, descPairs, err := cc.Marshal(context.TODO())
require.NoError(t, err)

require.EqualValues(t, cfg, cfg2)
Expand All @@ -78,13 +79,13 @@ func TestSimpleMarshal(t *testing.T) {
err = Parse(dt, descPairs, newChains)
require.NoError(t, err)

cfg3, _, err := cc.Marshal()
cfg3, _, err := cc.Marshal(context.TODO())
require.NoError(t, err)
require.EqualValues(t, cfg, cfg3)

// add extra item
cc.Add(outputKey(dgst("bay"), 0))
cfg, _, err = cc.Marshal()
cfg, _, err = cc.Marshal(context.TODO())
require.NoError(t, err)

require.Equal(t, len(cfg.Layers), 2)
Expand Down
22 changes: 17 additions & 5 deletions cache/remotecache/v1/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cacheimport

import (
"context"
"fmt"
"sort"

"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/solver"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -277,17 +279,27 @@ type marshalState struct {
recordsByItem map[*item]int
}

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

if cd, ok := r.Provider.(interface {
CheckDescriptor(context.Context, ocispecs.Descriptor) error
}); ok && len(r.Descriptors) > 0 {
for _, d := range r.Descriptors {
if cd.CheckDescriptor(ctx, d) != nil {
return ""
}
}
}
var parentID string
if len(r.Descriptors) > 1 {
r2 := &solver.Remote{
Descriptors: r.Descriptors[:len(r.Descriptors)-1],
Provider: r.Provider,
}
parentID = marshalRemote(r2, state)
parentID = marshalRemote(ctx, r2, state)
}
desc := r.Descriptors[len(r.Descriptors)-1]

Expand Down Expand Up @@ -318,7 +330,7 @@ func marshalRemote(r *solver.Remote, state *marshalState) string {
return id
}

func marshalItem(it *item, state *marshalState) error {
func marshalItem(ctx context.Context, it *item, state *marshalState) error {
if _, ok := state.recordsByItem[it]; ok {
return nil
}
Expand All @@ -330,7 +342,7 @@ func marshalItem(it *item, state *marshalState) error {

for i, m := range it.links {
for l := range m {
if err := marshalItem(l.src, state); err != nil {
if err := marshalItem(ctx, l.src, state); err != nil {
return err
}
idx, ok := state.recordsByItem[l.src]
Expand All @@ -345,7 +357,7 @@ func marshalItem(it *item, state *marshalState) error {
}

if it.result != nil {
id := marshalRemote(it.result, state)
id := marshalRemote(ctx, it.result, state)
if id != "" {
idx, ok := state.chainsByID[id]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions solver/llbsolver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro

func inlineCache(ctx context.Context, e remotecache.Exporter, res solver.CachedResult, compressionopt solver.CompressionOpt, g session.Group) ([]byte, error) {
if efl, ok := e.(interface {
ExportForLayers([]digest.Digest) ([]byte, error)
ExportForLayers(context.Context, []digest.Digest) ([]byte, error)
}); ok {
workerRef, ok := res.Sys().(*worker.WorkerRef)
if !ok {
Expand All @@ -317,7 +317,7 @@ func inlineCache(ctx context.Context, e remotecache.Exporter, res solver.CachedR
return nil, err
}

return efl.ExportForLayers(digests)
return efl.ExportForLayers(ctx, digests)
}
return nil, nil
}
Expand Down

0 comments on commit 0279989

Please sign in to comment.