Merge pull request #327 from dagger/no-cache

Implemented "--no-cache" option
This commit is contained in:
Andrea Luzzardi 2021-04-15 11:37:45 -07:00 committed by GitHub
commit 493406afe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 24 deletions

View File

@ -60,10 +60,10 @@ func GetCurrentDeploymentState(ctx context.Context, store *dagger.Store) *dagger
}
// Re-compute a deployment (equivalent to `dagger up`).
func DeploymentUp(ctx context.Context, state *dagger.DeploymentState) *dagger.Deployment {
func DeploymentUp(ctx context.Context, state *dagger.DeploymentState, noCache bool) *dagger.Deployment {
lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "")
c, err := dagger.NewClient(ctx, "", noCache)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}

View File

@ -149,7 +149,7 @@ var computeCmd = &cobra.Command{
}
}
deployment := common.DeploymentUp(ctx, st)
deployment := common.DeploymentUp(ctx, st, viper.GetBool("no-cache"))
v := compiler.NewValue()
if err := v.FillPath(cue.MakePath(), deployment.Plan()); err != nil {
@ -173,6 +173,7 @@ func init() {
computeCmd.Flags().StringSlice("input-git", []string{}, "TARGET=REMOTE#REF")
computeCmd.Flags().String("input-json", "", "JSON")
computeCmd.Flags().String("input-yaml", "", "YAML")
computeCmd.Flags().Bool("no-cache", false, "disable cache")
if err := viper.BindPFlags(computeCmd.Flags()); err != nil {
panic(err)

View File

@ -22,7 +22,7 @@ var downCmd = &cobra.Command{
}
func init() {
downCmd.Flags().Bool("--no-cache", false, "Disable all run cache")
downCmd.Flags().Bool("no-cache", false, "Disable all run cache")
if err := viper.BindPFlags(downCmd.Flags()); err != nil {
panic(err)

View File

@ -63,7 +63,7 @@ var newCmd = &cobra.Command{
Msg("deployment created")
if viper.GetBool("up") {
common.DeploymentUp(ctx, st)
common.DeploymentUp(ctx, st, false)
}
},
}

View File

@ -47,7 +47,7 @@ var queryCmd = &cobra.Command{
cuePath = cue.ParsePath(args[0])
}
c, err := dagger.NewClient(ctx, "")
c, err := dagger.NewClient(ctx, "", false)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}

View File

@ -29,9 +29,7 @@ var upCmd = &cobra.Command{
}
state := common.GetCurrentDeploymentState(ctx, store)
// TODO: Implement options: --no-cache
result := common.DeploymentUp(ctx, state)
result := common.DeploymentUp(ctx, state, viper.GetBool("no-cache"))
state.Computed = result.Computed().JSON().String()
if err := store.UpdateDeployment(ctx, state, nil); err != nil {
lg.Fatal().Err(err).Msg("failed to update deployment")
@ -40,7 +38,7 @@ var upCmd = &cobra.Command{
}
func init() {
newCmd.Flags().Bool("--no-cache", false, "Disable all run cache")
upCmd.Flags().Bool("no-cache", false, "Disable all run cache")
if err := viper.BindPFlags(upCmd.Flags()); err != nil {
panic(err)

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

@ -722,6 +722,10 @@ func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value, st llb.S
return st, err
}
if p.s.noCache {
opts["no-cache"] = ""
}
req := bkgw.SolveRequest{
Frontend: "dockerfile.v0",
FrontendOpt: opts,

View File

@ -23,22 +23,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 bkpb.Op
if err := (&op).Unmarshal(dt); err != nil {
return err
}
dgst := digest.FromBytes(dt)
opMetadata, ok := def.Metadata[dgst]
if !ok {
opMetadata = bkpb.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
}

View File

@ -19,15 +19,6 @@ import (
pushTarget: "\(repository):\(tag)"
// Build the image
buildImage: op.#DockerBuild & {
context: source
if dockerfilePath != _|_ {
"dockerfilePath": dockerfilePath
}
buildArg: buildArgs
}
// Use these credentials to push
ecrCreds: ecr.#Credentials & {
config: awsConfig
@ -35,6 +26,7 @@ import (
}
push: #up: [
// Build the docker image
op.#DockerBuild & {
context: source
if dockerfilePath != _|_ {
@ -42,6 +34,7 @@ import (
}
buildArg: buildArgs
},
// Push the image to the registry
op.#PushContainer & {
ref: pushTarget
},