Merge pull request #2433 from tonistiigi/gha-export-fix

gha: fix handling removed blobs on reexport
master
Akihiro Suda 2021-11-01 13:23:57 +09:00 committed by GitHub
commit 0279989d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 18 deletions

View File

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

View File

@ -106,7 +106,7 @@ func (ce *exporter) indexKey() string {
func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) { func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
// res := make(map[string]string) // res := make(map[string]string)
config, descs, err := ce.chains.Marshal() config, descs, err := ce.chains.Marshal(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

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

View File

@ -1,6 +1,7 @@
package cacheimport package cacheimport
import ( import (
"context"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -75,7 +76,7 @@ func (c *CacheChains) normalize() error {
return nil 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 { if err := c.normalize(); err != nil {
return nil, nil, err return nil, nil, err
} }
@ -87,7 +88,7 @@ func (c *CacheChains) Marshal() (*CacheConfig, DescriptorProvider, error) {
} }
for _, it := range c.items { for _, it := range c.items {
if err := marshalItem(it, st); err != nil { if err := marshalItem(ctx, it, st); err != nil {
return nil, nil, err return nil, nil, err
} }
} }

View File

@ -1,6 +1,7 @@
package cacheimport package cacheimport
import ( import (
"context"
"encoding/json" "encoding/json"
"testing" "testing"
"time" "time"
@ -33,7 +34,7 @@ func TestSimpleMarshal(t *testing.T) {
addRecords() addRecords()
cfg, _, err := cc.Marshal() cfg, _, err := cc.Marshal(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(cfg.Layers), 2) require.Equal(t, len(cfg.Layers), 2)
@ -65,7 +66,7 @@ func TestSimpleMarshal(t *testing.T) {
// adding same info again doesn't produce anything extra // adding same info again doesn't produce anything extra
addRecords() addRecords()
cfg2, descPairs, err := cc.Marshal() cfg2, descPairs, err := cc.Marshal(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, cfg, cfg2) require.EqualValues(t, cfg, cfg2)
@ -78,13 +79,13 @@ func TestSimpleMarshal(t *testing.T) {
err = Parse(dt, descPairs, newChains) err = Parse(dt, descPairs, newChains)
require.NoError(t, err) require.NoError(t, err)
cfg3, _, err := cc.Marshal() cfg3, _, err := cc.Marshal(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, cfg, cfg3) require.EqualValues(t, cfg, cfg3)
// add extra item // add extra item
cc.Add(outputKey(dgst("bay"), 0)) cc.Add(outputKey(dgst("bay"), 0))
cfg, _, err = cc.Marshal() cfg, _, err = cc.Marshal(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(cfg.Layers), 2) require.Equal(t, len(cfg.Layers), 2)

View File

@ -1,12 +1,14 @@
package cacheimport package cacheimport
import ( import (
"context"
"fmt" "fmt"
"sort" "sort"
"github.com/moby/buildkit/exporter/containerimage/exptypes" "github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/solver" "github.com/moby/buildkit/solver"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -277,17 +279,27 @@ type marshalState struct {
recordsByItem map[*item]int 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 { if len(r.Descriptors) == 0 {
return "" 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 var parentID string
if len(r.Descriptors) > 1 { if len(r.Descriptors) > 1 {
r2 := &solver.Remote{ r2 := &solver.Remote{
Descriptors: r.Descriptors[:len(r.Descriptors)-1], Descriptors: r.Descriptors[:len(r.Descriptors)-1],
Provider: r.Provider, Provider: r.Provider,
} }
parentID = marshalRemote(r2, state) parentID = marshalRemote(ctx, r2, state)
} }
desc := r.Descriptors[len(r.Descriptors)-1] desc := r.Descriptors[len(r.Descriptors)-1]
@ -318,7 +330,7 @@ func marshalRemote(r *solver.Remote, state *marshalState) string {
return id 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 { if _, ok := state.recordsByItem[it]; ok {
return nil return nil
} }
@ -330,7 +342,7 @@ func marshalItem(it *item, state *marshalState) error {
for i, m := range it.links { for i, m := range it.links {
for l := range m { for l := range m {
if err := marshalItem(l.src, state); err != nil { if err := marshalItem(ctx, l.src, state); err != nil {
return err return err
} }
idx, ok := state.recordsByItem[l.src] idx, ok := state.recordsByItem[l.src]
@ -345,7 +357,7 @@ func marshalItem(it *item, state *marshalState) error {
} }
if it.result != nil { if it.result != nil {
id := marshalRemote(it.result, state) id := marshalRemote(ctx, it.result, state)
if id != "" { if id != "" {
idx, ok := state.chainsByID[id] idx, ok := state.chainsByID[id]
if !ok { if !ok {

View File

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