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`). // 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) lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "") c, err := dagger.NewClient(ctx, "", noCache)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") 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() v := compiler.NewValue()
if err := v.FillPath(cue.MakePath(), deployment.Plan()); err != nil { 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().StringSlice("input-git", []string{}, "TARGET=REMOTE#REF")
computeCmd.Flags().String("input-json", "", "JSON") computeCmd.Flags().String("input-json", "", "JSON")
computeCmd.Flags().String("input-yaml", "", "YAML") computeCmd.Flags().String("input-yaml", "", "YAML")
computeCmd.Flags().Bool("no-cache", false, "disable cache")
if err := viper.BindPFlags(computeCmd.Flags()); err != nil { if err := viper.BindPFlags(computeCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -22,7 +22,7 @@ var downCmd = &cobra.Command{
} }
func init() { 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 { if err := viper.BindPFlags(downCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -63,7 +63,7 @@ var newCmd = &cobra.Command{
Msg("deployment created") Msg("deployment created")
if viper.GetBool("up") { 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]) cuePath = cue.ParsePath(args[0])
} }
c, err := dagger.NewClient(ctx, "") c, err := dagger.NewClient(ctx, "", false)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }

View File

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

View File

@ -29,10 +29,11 @@ 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")
} }
@ -53,7 +54,8 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
return nil, fmt.Errorf("buildkit client: %w", err) return nil, fmt.Errorf("buildkit client: %w", err)
} }
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

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

View File

@ -23,22 +23,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 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) { 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
} }

View File

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