solver/client: add ability to disable the cache at the LLB level

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-04-14 16:21:18 -07:00
parent 3bc91530e0
commit 83f56897a7
2 changed files with 36 additions and 5 deletions

View File

@ -29,10 +29,11 @@ import (
// A dagger client
type Client struct {
c *bk.Client
c *bk.Client
noCache bool
}
func NewClient(ctx context.Context, host string) (*Client, error) {
func NewClient(ctx context.Context, host string, noCache bool) (*Client, error) {
if host == "" {
host = os.Getenv("BUILDKIT_HOST")
}
@ -53,7 +54,8 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
return nil, fmt.Errorf("buildkit client: %w", err)
}
return &Client{
c: c,
c: c,
noCache: noCache,
}, nil
}
@ -111,7 +113,7 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, fn ClientD
Msg("spawning buildkit job")
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
s := NewSolver(c.c, gw, ch)
s := NewSolver(c.c, gw, ch, c.noCache)
lg.Debug().Msg("loading configuration")
if err := deployment.LoadPlan(ctx, s); err != nil {

View File

@ -14,6 +14,7 @@ import (
bkgw "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/solver/pb"
bkpb "github.com/moby/buildkit/solver/pb"
"github.com/opencontainers/go-digest"
"github.com/rs/zerolog/log"
@ -23,22 +24,50 @@ type Solver struct {
events chan *bk.SolveStatus
control *bk.Client
gw bkgw.Client
noCache bool
}
func NewSolver(control *bk.Client, gw bkgw.Client, events chan *bk.SolveStatus) Solver {
func NewSolver(control *bk.Client, gw bkgw.Client, events chan *bk.SolveStatus, noCache bool) Solver {
return Solver{
events: events,
control: control,
gw: gw,
noCache: noCache,
}
}
func invalidateCache(def *llb.Definition) error {
for _, dt := range def.Def {
var op pb.Op
if err := (&op).Unmarshal(dt); err != nil {
return err
}
dgst := digest.FromBytes(dt)
opMetadata, ok := def.Metadata[dgst]
if !ok {
opMetadata = pb.OpMetadata{}
}
c := llb.Constraints{Metadata: opMetadata}
llb.IgnoreCache(&c)
def.Metadata[dgst] = c.Metadata
}
return nil
}
func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, error) {
// FIXME: do not hardcode the platform
def, err := st.Marshal(ctx, llb.LinuxAmd64)
if err != nil {
return nil, err
}
if s.noCache {
if err := invalidateCache(def); err != nil {
return nil, err
}
}
return def.ToPB(), nil
}