Skip to content

Commit f4cc863

Browse files
committed
commands: make builder status timeouts configurable across cli flows
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 6248145 commit f4cc863

File tree

14 files changed

+97
-45
lines changed

14 files changed

+97
-45
lines changed

builder/builder.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ type CreateOpts struct {
346346
Use bool
347347
Endpoint string
348348
Append bool
349+
Timeout time.Duration
349350
}
350351

351352
func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts CreateOpts) (*Builder, error) {
@@ -525,8 +526,10 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
525526
return nil, err
526527
}
527528

528-
cancelCtx, cancel := context.WithCancelCause(ctx)
529-
timeoutCtx, _ := context.WithTimeoutCause(cancelCtx, 20*time.Second, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
529+
timeoutCtx, cancel := context.WithCancelCause(ctx)
530+
if opts.Timeout > 0 {
531+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, opts.Timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
532+
}
530533
defer func() { cancel(errors.WithStack(context.Canceled)) }()
531534

532535
nodes, err := b.LoadNodes(timeoutCtx, WithData())

commands/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"time"
78

89
"github.com/docker/buildx/builder"
910
"github.com/docker/buildx/driver"
@@ -27,6 +28,7 @@ type createOptions struct {
2728
buildkitdFlags string
2829
buildkitdConfigFile string
2930
bootstrap bool
31+
timeout time.Duration
3032
// upgrade bool // perform upgrade of the driver
3133
}
3234

@@ -61,6 +63,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
6163
Use: in.use,
6264
Endpoint: ep,
6365
Append: in.actionAppend,
66+
Timeout: in.timeout,
6467
})
6568
if err != nil {
6669
return err
@@ -120,6 +123,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
120123
flags.BoolVar(&options.actionAppend, "append", false, "Append a node to builder instead of changing it")
121124
flags.BoolVar(&options.actionLeave, "leave", false, "Remove a node from builder instead of changing it")
122125
flags.BoolVar(&options.use, "use", false, "Set the current builder instance")
126+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
123127

124128
// hide builder persistent flag for this command
125129
cobrautil.HideInheritedFlags(cmd, "builder")

commands/diskusage.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type duOptions struct {
6565
filter opts.FilterOpt
6666
verbose bool
6767
format string
68+
timeout time.Duration
6869
}
6970

7071
func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) error {
@@ -92,7 +93,13 @@ func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) er
9293
return err
9394
}
9495

95-
nodes, err := b.LoadNodes(ctx)
96+
timeoutCtx, cancel := context.WithCancelCause(ctx)
97+
if opts.timeout > 0 {
98+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, opts.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
99+
}
100+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
101+
102+
nodes, err := b.LoadNodes(timeoutCtx)
96103
if err != nil {
97104
return err
98105
}
@@ -197,6 +204,7 @@ func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
197204
flags.Var(&options.filter, "filter", "Provide filter values")
198205
flags.BoolVar(&options.verbose, "verbose", false, `Shorthand for "--format=pretty"`)
199206
flags.StringVar(&options.format, "format", "", "Format the output")
207+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
200208

201209
return cmd
202210
}

commands/inspect.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
type inspectOptions struct {
2525
bootstrap bool
2626
builder string
27+
timeout time.Duration
2728
}
2829

2930
func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) error {
@@ -36,7 +37,9 @@ func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) e
3637
}
3738

3839
timeoutCtx, cancel := context.WithCancelCause(ctx)
39-
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 20*time.Second, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
40+
if in.timeout > 0 {
41+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
42+
}
4043
defer func() { cancel(errors.WithStack(context.Canceled)) }()
4144

4245
nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
@@ -188,6 +191,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
188191

189192
flags := cmd.Flags()
190193
flags.BoolVar(&options.bootstrap, "bootstrap", false, "Ensure builder has booted before inspecting")
194+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
191195

192196
return cmd
193197
}

commands/ls.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
type lsOptions struct {
4141
format string
4242
noTrunc bool
43+
timeout time.Duration
4344
}
4445

4546
func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
@@ -60,7 +61,9 @@ func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
6061
}
6162

6263
timeoutCtx, cancel := context.WithCancelCause(ctx)
63-
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 20*time.Second, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
64+
if in.timeout > 0 {
65+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
66+
}
6467
defer func() { cancel(errors.WithStack(context.Canceled)) }()
6568

6669
eg, _ := errgroup.WithContext(timeoutCtx)
@@ -114,6 +117,7 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
114117
flags := cmd.Flags()
115118
flags.StringVar(&options.format, "format", formatter.TableFormatKey, "Format the output")
116119
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
120+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
117121

118122
// hide builder persistent flag for this command
119123
cobrautil.HideInheritedFlags(cmd, "builder")

commands/prune.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type pruneOptions struct {
3636
minFreeSpace opts.MemBytes
3737
force bool
3838
verbose bool
39+
timeout time.Duration
3940
}
4041

4142
const (
@@ -68,7 +69,13 @@ func runPrune(ctx context.Context, dockerCli command.Cli, opts pruneOptions) err
6869
return err
6970
}
7071

71-
nodes, err := b.LoadNodes(ctx)
72+
timeoutCtx, cancel := context.WithCancelCause(ctx)
73+
if opts.timeout > 0 {
74+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, opts.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
75+
}
76+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
77+
78+
nodes, err := b.LoadNodes(timeoutCtx)
7279
if err != nil {
7380
return err
7481
}
@@ -182,6 +189,7 @@ func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
182189
flags.Var(&options.maxUsedSpace, "max-used-space", "Maximum amount of disk space allowed to keep for cache")
183190
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
184191
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
192+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
185193

186194
flags.Var(&options.reservedSpace, "keep-storage", "Amount of disk space to keep for cache")
187195
flags.MarkDeprecated("keep-storage", "keep-storage flag has been changed to reserved-space")

commands/rm.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type rmOptions struct {
2121
keepDaemon bool
2222
allInactive bool
2323
force bool
24+
timeout time.Duration
2425
}
2526

2627
const (
@@ -46,7 +47,13 @@ func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
4647
return rmAllInactive(ctx, txn, dockerCli, in)
4748
}
4849

49-
eg, _ := errgroup.WithContext(ctx)
50+
timeoutCtx, cancel := context.WithCancelCause(ctx)
51+
if in.timeout > 0 {
52+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
53+
}
54+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
55+
56+
eg, _ := errgroup.WithContext(timeoutCtx)
5057
for _, name := range in.builders {
5158
func(name string) {
5259
eg.Go(func() (err error) {
@@ -67,7 +74,7 @@ func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
6774
return err
6875
}
6976

70-
nodes, err := b.LoadNodes(ctx)
77+
nodes, err := b.LoadNodes(timeoutCtx)
7178
if err != nil {
7279
return err
7380
}
@@ -120,6 +127,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
120127
flags.BoolVar(&options.keepDaemon, "keep-daemon", false, "Keep the BuildKit daemon running")
121128
flags.BoolVar(&options.allInactive, "all-inactive", false, "Remove all inactive builders")
122129
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
130+
setBuilderStatusTimeoutFlag(flags, &options.timeout)
123131

124132
return cmd
125133
}
@@ -152,7 +160,9 @@ func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, i
152160
}
153161

154162
timeoutCtx, cancel := context.WithCancelCause(ctx)
155-
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 20*time.Second, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
163+
if in.timeout > 0 {
164+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
165+
}
156166
defer func() { cancel(errors.WithStack(context.Canceled)) }()
157167

158168
eg, _ := errgroup.WithContext(timeoutCtx)

commands/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
historycmd "github.com/docker/buildx/commands/history"
89
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
@@ -142,3 +143,7 @@ func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
142143
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")
143144
flags.BoolVarP(&options.debug, "debug", "D", debug.IsEnabled(), "Enable debug logging")
144145
}
146+
147+
func setBuilderStatusTimeoutFlag(flags *pflag.FlagSet, target *time.Duration) {
148+
flags.DurationVar(target, "timeout", 20*time.Second, "Override the default timeout for loading builder status")
149+
}

docs/reference/buildx_create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Create a new builder instance
2222
| [`--name`](#name) | `string` | | Builder instance name |
2323
| [`--node`](#node) | `string` | | Create/modify node with given name |
2424
| [`--platform`](#platform) | `stringArray` | | Fixed platforms for current node |
25+
| `--timeout` | `duration` | `20s` | Override the default timeout for loading builder status |
2526
| [`--use`](#use) | `bool` | | Set the current builder instance |
2627

2728

docs/reference/buildx_du.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ Disk usage
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:------------------------|:---------|:--------|:-----------------------------------------|
14-
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
15-
| `-D`, `--debug` | `bool` | | Enable debug logging |
16-
| [`--filter`](#filter) | `filter` | | Provide filter values |
17-
| [`--format`](#format) | `string` | | Format the output |
18-
| [`--verbose`](#verbose) | `bool` | | Shorthand for `--format=pretty` |
12+
| Name | Type | Default | Description |
13+
|:------------------------|:-----------|:--------|:--------------------------------------------------------|
14+
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
15+
| `-D`, `--debug` | `bool` | | Enable debug logging |
16+
| [`--filter`](#filter) | `filter` | | Provide filter values |
17+
| [`--format`](#format) | `string` | | Format the output |
18+
| `--timeout` | `duration` | `20s` | Override the default timeout for loading builder status |
19+
| [`--verbose`](#verbose) | `bool` | | Shorthand for `--format=pretty` |
1920

2021

2122
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)