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

@ -30,9 +30,10 @@ import (
// A dagger client // A dagger client
type Client struct { 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 == "" { if host == "" {
host = os.Getenv("BUILDKIT_HOST") host = os.Getenv("BUILDKIT_HOST")
} }
@ -54,6 +55,7 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
} }
return &Client{ return &Client{
c: c, c: c,
noCache: noCache,
}, nil }, nil
} }
@ -111,7 +113,7 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, fn ClientD
Msg("spawning buildkit job") Msg("spawning buildkit job")
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) { 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") lg.Debug().Msg("loading configuration")
if err := deployment.LoadPlan(ctx, s); err != nil { if err := deployment.LoadPlan(ctx, s); err != nil {

View File

@ -14,6 +14,7 @@ import (
bkgw "github.com/moby/buildkit/frontend/gateway/client" bkgw "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/session" "github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider" "github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/solver/pb"
bkpb "github.com/moby/buildkit/solver/pb" bkpb "github.com/moby/buildkit/solver/pb"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -23,22 +24,50 @@ type Solver struct {
events chan *bk.SolveStatus events chan *bk.SolveStatus
control *bk.Client control *bk.Client
gw bkgw.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{ return Solver{
events: events, events: events,
control: control, control: control,
gw: gw, 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) { func (s Solver) Marshal(ctx context.Context, st llb.State) (*bkpb.Definition, error) {
// FIXME: do not hardcode the platform // FIXME: do not hardcode the platform
def, err := st.Marshal(ctx, llb.LinuxAmd64) def, err := st.Marshal(ctx, llb.LinuxAmd64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if s.noCache {
if err := invalidateCache(def); err != nil {
return nil, err
}
}
return def.ToPB(), nil return def.ToPB(), nil
} }