Skip to content

Commit

Permalink
builder: use variadic options in LoadNodes func
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Oct 12, 2023
1 parent c0adff1 commit 13e1fba
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 15 deletions.
25 changes: 22 additions & 3 deletions builder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,28 @@ func (b *Builder) Nodes() []Node {
return b.nodes
}

type LoadNodesOption func(*loadNodesOptions)

type loadNodesOptions struct {
data bool
}

func WithData() LoadNodesOption {
return func(o *loadNodesOptions) {
o.data = true
}
}

// LoadNodes loads and returns nodes for this builder.
// TODO: this should be a method on a Node object and lazy load data for each driver.
func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err error) {
func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []Node, err error) {
lno := loadNodesOptions{
data: false,
}
for _, opt := range opts {
opt(&lno)
}

eg, _ := errgroup.WithContext(ctx)
b.nodes = make([]Node, len(b.NodeGroup.Nodes))

Expand Down Expand Up @@ -121,7 +140,7 @@ func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err e
node.Driver = d
node.ImageOpt = imageopt

if withData {
if lno.data {
if err := node.loadData(ctx); err != nil {
node.Err = err
}
Expand All @@ -136,7 +155,7 @@ func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err e
}

// TODO: This should be done in the routine loading driver data
if withData {
if lno.data {
kubernetesDriverCount := 0
for _, d := range b.nodes {
if d.DriverInfo != nil && len(d.DriverInfo.DynamicNodes) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
if err = updateLastActivity(dockerCli, b.NodeGroup); err != nil {
return errors.Wrapf(err, "failed to update builder last activity time")
}
nodes, err = b.LoadNodes(ctx, false)
nodes, err = b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) (err error) {
if err != nil {
return err
}
_, err = b.LoadNodes(ctx, false)
_, err = b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

nodes, err := b.LoadNodes(timeoutCtx, true)
nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/diskusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
return err
}

nodes, err := b.LoadNodes(ctx, false)
nodes, err := b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func runInspect(dockerCli command.Cli, in inspectOptions) error {
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

nodes, err := b.LoadNodes(timeoutCtx, true)
nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
if in.bootstrap {
var ok bool
ok, err = b.Boot(ctx)
if err != nil {
return err
}
if ok {
nodes, err = b.LoadNodes(timeoutCtx, true)
nodes, err = b.LoadNodes(timeoutCtx, builder.WithData())
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func runLs(dockerCli command.Cli, in lsOptions) error {
for _, b := range builders {
func(b *builder.Builder) {
eg.Go(func() error {
_, _ = b.LoadNodes(timeoutCtx, true)
_, _ = b.LoadNodes(timeoutCtx, builder.WithData())
return nil
})
}(b)
Expand Down
2 changes: 1 addition & 1 deletion commands/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
return err
}

nodes, err := b.LoadNodes(ctx, false)
nodes, err := b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
return err
}

nodes, err := b.LoadNodes(ctx, false)
nodes, err := b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, i
for _, b := range builders {
func(b *builder.Builder) {
eg.Go(func() error {
nodes, err := b.LoadNodes(timeoutCtx, true)
nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
if err != nil {
return errors.Wrapf(err, "cannot load %s", b.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func runStop(dockerCli command.Cli, in stopOptions) error {
if err != nil {
return err
}
nodes, err := b.LoadNodes(ctx, false)
nodes, err := b.LoadNodes(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion controller/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
if err = updateLastActivity(dockerCli, b.NodeGroup); err != nil {
return nil, nil, errors.Wrapf(err, "failed to update builder last activity time")
}
nodes, err := b.LoadNodes(ctx, false)
nodes, err := b.LoadNodes(ctx)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 13e1fba

Please sign in to comment.