Skip to content

Commit b6c2272

Browse files
committed
commands: add timeout flag for create, inspect, ls and rm
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 6248145 commit b6c2272

File tree

10 files changed

+48
-24
lines changed

10 files changed

+48
-24
lines changed

builder/builder.go

Lines changed: 2 additions & 1 deletion
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) {
@@ -526,7 +527,7 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
526527
}
527528

528529
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
530+
timeoutCtx, _ := context.WithTimeoutCause(cancelCtx, opts.Timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent
530531
defer func() { cancel(errors.WithStack(context.Canceled)) }()
531532

532533
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+
setTimeoutFlag(flags, &options.timeout)
123127

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

commands/inspect.go

Lines changed: 3 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,7 @@ 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+
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
4041
defer func() { cancel(errors.WithStack(context.Canceled)) }()
4142

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

189190
flags := cmd.Flags()
190191
flags.BoolVar(&options.bootstrap, "bootstrap", false, "Ensure builder has booted before inspecting")
192+
setTimeoutFlag(flags, &options.timeout)
191193

192194
return cmd
193195
}

commands/ls.go

Lines changed: 3 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,7 @@ 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+
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
6465
defer func() { cancel(errors.WithStack(context.Canceled)) }()
6566

6667
eg, _ := errgroup.WithContext(timeoutCtx)
@@ -114,6 +115,7 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
114115
flags := cmd.Flags()
115116
flags.StringVar(&options.format, "format", formatter.TableFormatKey, "Format the output")
116117
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
118+
setTimeoutFlag(flags, &options.timeout)
117119

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

commands/rm.go

Lines changed: 9 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,11 @@ 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+
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
52+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
53+
54+
eg, _ := errgroup.WithContext(timeoutCtx)
5055
for _, name := range in.builders {
5156
func(name string) {
5257
eg.Go(func() (err error) {
@@ -67,7 +72,7 @@ func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
6772
return err
6873
}
6974

70-
nodes, err := b.LoadNodes(ctx)
75+
nodes, err := b.LoadNodes(timeoutCtx)
7176
if err != nil {
7277
return err
7378
}
@@ -120,6 +125,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
120125
flags.BoolVar(&options.keepDaemon, "keep-daemon", false, "Keep the BuildKit daemon running")
121126
flags.BoolVar(&options.allInactive, "all-inactive", false, "Remove all inactive builders")
122127
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
128+
setTimeoutFlag(flags, &options.timeout)
123129

124130
return cmd
125131
}
@@ -152,7 +158,7 @@ func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, i
152158
}
153159

154160
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
161+
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
156162
defer func() { cancel(errors.WithStack(context.Canceled)) }()
157163

158164
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 setTimeoutFlag(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_inspect.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ Inspect current builder instance
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------------------|:---------|:--------|:--------------------------------------------|
14-
| [`--bootstrap`](#bootstrap) | `bool` | | Ensure builder has booted before inspecting |
15-
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
16-
| `-D`, `--debug` | `bool` | | Enable debug logging |
12+
| Name | Type | Default | Description |
13+
|:----------------------------|:-----------|:--------|:--------------------------------------------------------|
14+
| [`--bootstrap`](#bootstrap) | `bool` | | Ensure builder has booted before inspecting |
15+
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
16+
| `-D`, `--debug` | `bool` | | Enable debug logging |
17+
| `--timeout` | `duration` | `20s` | Override the default timeout for loading builder status |
1718

1819

1920
<!---MARKER_GEN_END-->

docs/reference/buildx_ls.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ List builder instances
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------------|:---------|:--------|:----------------------|
14-
| `-D`, `--debug` | `bool` | | Enable debug logging |
15-
| [`--format`](#format) | `string` | `table` | Format the output |
16-
| `--no-trunc` | `bool` | | Don't truncate output |
12+
| Name | Type | Default | Description |
13+
|:----------------------|:-----------|:--------|:--------------------------------------------------------|
14+
| `-D`, `--debug` | `bool` | | Enable debug logging |
15+
| [`--format`](#format) | `string` | `table` | Format the output |
16+
| `--no-trunc` | `bool` | | Don't truncate output |
17+
| `--timeout` | `duration` | `20s` | Override the default timeout for loading builder status |
1718

1819

1920
<!---MARKER_GEN_END-->

docs/reference/buildx_rm.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ Remove one or more builder instances
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:------------------------------------|:---------|:--------|:-----------------------------------------|
14-
| [`--all-inactive`](#all-inactive) | `bool` | | Remove all inactive builders |
15-
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
16-
| `-D`, `--debug` | `bool` | | Enable debug logging |
17-
| [`-f`](#force), [`--force`](#force) | `bool` | | Do not prompt for confirmation |
18-
| [`--keep-daemon`](#keep-daemon) | `bool` | | Keep the BuildKit daemon running |
19-
| [`--keep-state`](#keep-state) | `bool` | | Keep BuildKit state |
12+
| Name | Type | Default | Description |
13+
|:------------------------------------|:-----------|:--------|:--------------------------------------------------------|
14+
| [`--all-inactive`](#all-inactive) | `bool` | | Remove all inactive builders |
15+
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
16+
| `-D`, `--debug` | `bool` | | Enable debug logging |
17+
| [`-f`](#force), [`--force`](#force) | `bool` | | Do not prompt for confirmation |
18+
| [`--keep-daemon`](#keep-daemon) | `bool` | | Keep the BuildKit daemon running |
19+
| [`--keep-state`](#keep-state) | `bool` | | Keep BuildKit state |
20+
| `--timeout` | `duration` | `20s` | Override the default timeout for loading builder status |
2021

2122

2223
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)