Skip to content

Commit

Permalink
build: move solveOpt to local struct type
Browse files Browse the repository at this point in the history
*client.SolveOpt in driver code is only used by build code.
For a clear separation of concerns, move it to an internal
struct type only accessible by BuildWithResultHandler func.

Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Dec 4, 2023
1 parent 5403231 commit 857b793
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
60 changes: 35 additions & 25 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
}
}()

type reqForNode struct {
*resolvedNode
so *client.SolveOpt
}

reqForNodes := make(map[string][]*reqForNode)
eg, ctx := errgroup.WithContext(ctx)

for k, opt := range opt {
Expand All @@ -528,6 +534,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
if err != nil {
logrus.WithError(err).Warn("current commit information was not captured by the build")
}
var reqn []*reqForNode
for _, np := range drivers[k] {
if np.Node().Driver.IsMobyDriver() {
hasMobyDriver = true
Expand All @@ -548,8 +555,12 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
so.FrontendAttrs[k] = v
}
defers = append(defers, release)
np.SolveOpt = so
reqn = append(reqn, &reqForNode{
resolvedNode: np,
so: so,
})
}
reqForNodes[k] = reqn
for _, at := range opt.Session {
if s, ok := at.(interface {
SetLogger(progresswriter.Logger)
Expand All @@ -562,8 +573,8 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s

// validate for multi-node push
if hasMobyDriver && multiDriver {
for _, np := range drivers[k] {
for _, e := range np.SolveOpt.Exports {
for _, np := range reqForNodes[k] {
for _, e := range np.so.Exports {
if e.Type == "moby" {
if ok, _ := strconv.ParseBool(e.Attrs["push"]); ok {
return nil, errors.Errorf("multi-node push can't currently be performed with the docker driver, please switch to a different driver")
Expand All @@ -576,9 +587,9 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s

// validate that all links between targets use same drivers
for name := range opt {
dps := drivers[name]
dps := reqForNodes[name]
for i, dp := range dps {
so := drivers[name][i].SolveOpt
so := reqForNodes[name][i].so
for k, v := range so.FrontendAttrs {
if strings.HasPrefix(k, "context:") && strings.HasPrefix(v, "target:") {
k2 := strings.TrimPrefix(v, "target:")
Expand Down Expand Up @@ -606,7 +617,24 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
results := waitmap.New()

multiTarget := len(opt) > 1
childTargets := calculateChildTargets(drivers, opt)

// returns all the targets that depend on current target for reverse index
childTargets := func(reqForNodes map[string][]*reqForNode, opt map[string]Options) map[string][]string {
out := make(map[string][]string)
for name := range opt {
dps := reqForNodes[name]
for i, dp := range dps {
so := reqForNodes[name][i].so
for k, v := range so.FrontendAttrs {
if strings.HasPrefix(k, "context:") && strings.HasPrefix(v, "target:") {
target := resultKey(dp.driverIndex, strings.TrimPrefix(v, "target:"))
out[target] = append(out[target], resultKey(dp.driverIndex, name))
}
}
}
}
return out
}(reqForNodes, opt)

for k, opt := range opt {
err := func(k string) error {
Expand All @@ -630,7 +658,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
for i, dp := range dps {
i, dp := i, dp
node := dp.Node()
so := drivers[k][i].SolveOpt
so := reqForNodes[k][i].so
if multiDriver {
for i, e := range so.Exports {
switch e.Type {
Expand Down Expand Up @@ -1298,24 +1326,6 @@ func resultKey(index int, name string) string {
return fmt.Sprintf("%d-%s", index, name)
}

// calculateChildTargets returns all the targets that depend on current target for reverse index
func calculateChildTargets(drivers map[string][]*resolvedNode, opt map[string]Options) map[string][]string {
out := make(map[string][]string)
for name := range opt {
dps := drivers[name]
for i, dp := range dps {
so := drivers[name][i].SolveOpt
for k, v := range so.FrontendAttrs {
if strings.HasPrefix(k, "context:") && strings.HasPrefix(v, "target:") {
target := resultKey(dp.driverIndex, strings.TrimPrefix(v, "target:"))
out[target] = append(out[target], resultKey(dp.driverIndex, name))
}
}
}
}
return out
}

func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *client.SolveOpt) error {
m := map[string]string{}
for k, v := range so.FrontendAttrs {
Expand Down
2 changes: 0 additions & 2 deletions build/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
)

type resolvedNode struct {
SolveOpt *client.SolveOpt

resolver *nodeResolver
driverIndex int
platforms []specs.Platform
Expand Down

0 comments on commit 857b793

Please sign in to comment.