Repository navigation
Pass --parallel limit to bake as --jobs #14352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import ( | |
| "os/exec" | ||
| "path/filepath" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/types" | ||
|
|
@@ -131,6 +132,7 @@ type bakeBuild struct { | |
| localWritePaths []string // local cache destinations bake needs `--allow fs.write` for | ||
| entitlements []string | ||
| secretsEnv []string | ||
| jobs int // bake `--jobs` limit, 0 for unlimited | ||
| } | ||
|
|
||
| func (s *composeService) doBuildBake(ctx context.Context, project *types.Project, serviceToBeBuild types.Services, options api.BuildOptions) (map[string]string, error) { | ||
|
|
@@ -177,6 +179,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| bake.jobs = bakeJobs(bake.cfg, s.maxConcurrency, buildx.Version) | ||
| args := bakeArgs(bake, metadataFile, options) | ||
| logrus.Debugf("Executing bake with args: %v", args) | ||
|
|
||
|
|
@@ -498,12 +501,56 @@ func bakeArgs(bake *bakeBuild, metadataFile string, options api.BuildOptions) [] | |
| if options.Builder != "" { | ||
| args = append(args, "--builder", options.Builder) | ||
| } | ||
| if bake.jobs > 0 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing pins that |
||
| args = append(args, "--jobs", strconv.Itoa(bake.jobs)) | ||
| } | ||
| if options.Quiet { | ||
| args = append(args, "--progress=quiet") | ||
| } | ||
| return args | ||
| } | ||
|
|
||
| // bakeJobs returns how many targets bake can build concurrently | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs: the "Configuring parallelism" section of |
||
| func bakeJobs(cfg bakeConfig, maxConcurrency int, buildxVersion string) int { | ||
| // no limit | ||
| if maxConcurrency <= 0 { | ||
| return 0 | ||
| } | ||
| // limit doesn't hold target back | ||
| targets, linked := bakeTargets(cfg) | ||
| if maxConcurrency >= targets { | ||
| return 0 | ||
| } | ||
| if versions.LessThan(buildxVersion[1:], buildxJobsMinVersion) { | ||
| logrus.Warnf("limiting build concurrency requires buildx %s or later, ignoring parallel limit", buildxJobsMinVersion) | ||
| return 0 | ||
| } | ||
| // bake rejects a limit lower than its target count once any are linked by `target:` contexts | ||
| if linked { | ||
| logrus.Warnf("bake does not support a parallel limit lower than its %d linked build targets, ignoring parallel limit", targets) | ||
| return 0 | ||
| } | ||
| return maxConcurrency | ||
| } | ||
|
|
||
| // bakeTargets counts the targets bake builds for the default group, those | ||
| // linked in by `target:` contexts included, and reports whether any link exists. | ||
| func bakeTargets(cfg bakeConfig) (int, bool) { | ||
| targets := slices.Clone(cfg.Groups["default"].Targets) | ||
| linked := false | ||
| for i := 0; i < len(targets); i++ { | ||
| for _, source := range cfg.Targets[targets[i]].Contexts { | ||
| if target, ok := strings.CutPrefix(source, "target:"); ok { | ||
| linked = true | ||
| if !slices.Contains(targets, target) { | ||
| targets = append(targets, target) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return len(targets), linked | ||
| } | ||
|
|
||
| // waitBake forwards the solve statuses of a started bake command's rawjson | ||
| // stderr stream to ch, then waits for the command to exit. It returns the | ||
| // lines of the stream that are not solve statuses — where bake tells why it | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we indeed can reuse compose's
--parallelflag in this context, whenever it's original goal is not to restrict the build but concurrent API calls.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize I was stretching its meaning, glad you're fine with it though!