dockerfile: add cache-from support

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2018-04-25 15:22:49 -07:00
parent 11ada4c27b
commit 966d3f7b32
4 changed files with 32 additions and 18 deletions

View File

@ -20,6 +20,7 @@ const (
LocalNameDockerfile = "dockerfile" LocalNameDockerfile = "dockerfile"
keyTarget = "target" keyTarget = "target"
keyFilename = "filename" keyFilename = "filename"
keyCacheFrom = "cache-from"
exporterImageConfig = "containerimage.config" exporterImageConfig = "containerimage.config"
defaultDockerfileName = "Dockerfile" defaultDockerfileName = "Dockerfile"
dockerignoreFilename = ".dockerignore" dockerignoreFilename = ".dockerignore"
@ -57,7 +58,7 @@ func Build(ctx context.Context, c client.Client) error {
eg, ctx2 := errgroup.WithContext(ctx) eg, ctx2 := errgroup.WithContext(ctx)
var dtDockerfile []byte var dtDockerfile []byte
eg.Go(func() error { eg.Go(func() error {
ref, err := c.Solve(ctx2, def.ToPB(), "", "", nil, false) ref, err := c.Solve(ctx2, def.ToPB(), "", nil, nil, false)
if err != nil { if err != nil {
return err return err
} }
@ -83,7 +84,7 @@ func Build(ctx context.Context, c client.Client) error {
if err != nil { if err != nil {
return err return err
} }
ref, err := c.Solve(ctx2, def.ToPB(), "", "", nil, false) ref, err := c.Solve(ctx2, def.ToPB(), "", nil, nil, false)
if err != nil { if err != nil {
return err return err
} }
@ -125,7 +126,12 @@ func Build(ctx context.Context, c client.Client) error {
return err return err
} }
_, err = c.Solve(ctx, def.ToPB(), "", opts["cache-from"], map[string][]byte{ var cacheFrom []string
if cacheFromStr := opts[keyCacheFrom]; cacheFromStr != "" {
cacheFrom = strings.Split(cacheFromStr, ",")
}
_, err = c.Solve(ctx, def.ToPB(), "", cacheFrom, map[string][]byte{
exporterImageConfig: config, exporterImageConfig: config,
}, true) }, true)
if err != nil { if err != nil {

View File

@ -26,11 +26,11 @@ type bridgeClient struct {
refs []*ref refs []*ref
} }
func (c *bridgeClient) Solve(ctx context.Context, def *pb.Definition, f string, importRef string, exporterAttr map[string][]byte, final bool) (client.Reference, error) { func (c *bridgeClient) Solve(ctx context.Context, def *pb.Definition, f string, cacheImportRefs []string, exporterAttr map[string][]byte, final bool) (client.Reference, error) {
r, exporterAttrRes, err := c.FrontendLLBBridge.Solve(ctx, frontend.SolveRequest{ r, exporterAttrRes, err := c.FrontendLLBBridge.Solve(ctx, frontend.SolveRequest{
Definition: def, Definition: def,
Frontend: f, Frontend: f,
ImportCacheRef: importRef, ImportCacheRefs: cacheImportRefs,
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -9,7 +9,7 @@ import (
// TODO: make this take same options as LLBBridge. Add Return() // TODO: make this take same options as LLBBridge. Add Return()
type Client interface { type Client interface {
Solve(ctx context.Context, def *pb.Definition, frontend string, importRef string, exporterAttr map[string][]byte, final bool) (Reference, error) Solve(ctx context.Context, def *pb.Definition, frontend string, cacheImportRefs []string, exporterAttr map[string][]byte, final bool) (Reference, error)
ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error) ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error)
Opts() map[string]string Opts() map[string]string
SessionID() string SessionID() string

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/cache" "github.com/moby/buildkit/cache"
"github.com/moby/buildkit/cache/cacheimport" "github.com/moby/buildkit/cache/cacheimport"
"github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor"
@ -32,16 +33,23 @@ func (b *llbBridge) Solve(ctx context.Context, req frontend.SolveRequest) (res s
b.cmsMu.Lock() b.cmsMu.Lock()
var cm solver.CacheManager var cm solver.CacheManager
if prevCm, ok := b.cms[ref]; !ok { if prevCm, ok := b.cms[ref]; !ok {
cm = newLazyCacheManager(ref, func() (solver.CacheManager, error) { r, err := reference.ParseNormalizedNamed(ref)
var cmNew solver.CacheManager if err != nil {
if err := b.builder.Call(ctx, "importing cache manifest from "+ref, func(ctx context.Context) error { return nil, nil, err
cmNew, err = b.ci.Resolve(ctx, ref) }
return err ref = reference.TagNameOnly(r).String()
}); err != nil { func(ref string) {
return nil, err cm = newLazyCacheManager(ref, func() (solver.CacheManager, error) {
} var cmNew solver.CacheManager
return cmNew, nil if err := b.builder.Call(ctx, "importing cache manifest from "+ref, func(ctx context.Context) error {
}) cmNew, err = b.ci.Resolve(ctx, ref)
return err
}); err != nil {
return nil, err
}
return cmNew, nil
})
}(ref)
b.cms[ref] = cm b.cms[ref] = cm
} else { } else {
cm = prevCm cm = prevCm