Skip to content

Commit 9822961

Browse files
committed
add timeout parameter to cli and driver
1 parent 06d96d6 commit 9822961

File tree

15 files changed

+62
-22
lines changed

15 files changed

+62
-22
lines changed

builder/builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ type CreateOpts struct {
343343
Use bool
344344
Endpoint string
345345
Append bool
346+
Timeout time.Duration
346347
}
347348

348349
func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts CreateOpts) (*Builder, error) {
@@ -522,7 +523,7 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
522523
return nil, err
523524
}
524525

525-
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
526+
timeoutCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
526527
defer cancel()
527528

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

commands/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ type buildOptions struct {
104104
control.ControlOptions
105105

106106
invokeConfig *invokeConfig
107+
108+
timeout time.Duration
107109
}
108110

109111
func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error) {

commands/create.go

Lines changed: 5 additions & 1 deletion
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
@@ -80,7 +83,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
8083
return nil
8184
}
8285

83-
func createCmd(dockerCli command.Cli) *cobra.Command {
86+
func createCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
8487
var options createOptions
8588

8689
var drivers bytes.Buffer
@@ -96,6 +99,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
9699
Short: "Create a new builder instance",
97100
Args: cli.RequiresMaxArgs(1),
98101
RunE: func(cmd *cobra.Command, args []string) error {
102+
options.timeout = rootOpts.timeout
99103
return runCreate(cmd.Context(), dockerCli, options, args)
100104
},
101105
ValidArgsFunction: completion.Disable,

commands/inspect.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
type inspectOptions struct {
2424
bootstrap bool
2525
builder string
26+
timeout time.Duration
2627
}
2728

2829
func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) error {
@@ -34,7 +35,7 @@ func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) e
3435
return err
3536
}
3637

37-
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
38+
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
3839
defer cancel()
3940

4041
nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
@@ -147,6 +148,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
147148
if len(args) > 0 {
148149
options.builder = args[0]
149150
}
151+
options.timeout = rootOpts.timeout
150152
return runInspect(cmd.Context(), dockerCli, options)
151153
},
152154
ValidArgsFunction: completion.BuilderNames(dockerCli),

commands/ls.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const (
3535
)
3636

3737
type lsOptions struct {
38-
format string
38+
format string
39+
timeout time.Duration
3940
}
4041

4142
func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
@@ -55,7 +56,7 @@ func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
5556
return err
5657
}
5758

58-
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
59+
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
5960
defer cancel()
6061

6162
eg, _ := errgroup.WithContext(timeoutCtx)
@@ -92,14 +93,15 @@ func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
9293
return nil
9394
}
9495

95-
func lsCmd(dockerCli command.Cli) *cobra.Command {
96+
func lsCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
9697
var options lsOptions
9798

9899
cmd := &cobra.Command{
99100
Use: "ls",
100101
Short: "List builder instances",
101102
Args: cli.ExactArgs(0),
102103
RunE: func(cmd *cobra.Command, args []string) error {
104+
options.timeout = rootOpts.timeout
103105
return runLs(cmd.Context(), dockerCli, options)
104106
},
105107
ValidArgsFunction: completion.Disable,

commands/rm.go

Lines changed: 3 additions & 1 deletion
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 (
@@ -109,6 +110,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
109110
}
110111
options.builders = args
111112
}
113+
options.timeout = rootOpts.timeout
112114
return runRm(cmd.Context(), dockerCli, options)
113115
},
114116
ValidArgsFunction: completion.BuilderNames(dockerCli),
@@ -150,7 +152,7 @@ func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, i
150152
return err
151153
}
152154

153-
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
155+
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
154156
defer cancel()
155157

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

commands/root.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"os"
5+
"time"
56

67
debugcmd "github.com/docker/buildx/commands/debug"
78
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
@@ -20,6 +21,8 @@ import (
2021
"github.com/spf13/pflag"
2122
)
2223

24+
const defaultTimeoutCli = 20 * time.Second
25+
2326
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
2427
cmd := &cobra.Command{
2528
Short: "Docker Buildx",
@@ -74,6 +77,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
7477

7578
type rootOptions struct {
7679
builder string
80+
timeout time.Duration
7781
}
7882

7983
func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
@@ -83,10 +87,10 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
8387
cmd.AddCommand(
8488
buildCmd(dockerCli, opts, nil),
8589
bakeCmd(dockerCli, opts),
86-
createCmd(dockerCli),
90+
createCmd(dockerCli, opts),
8791
dialStdioCmd(dockerCli, opts),
8892
rmCmd(dockerCli, opts),
89-
lsCmd(dockerCli),
93+
lsCmd(dockerCli, opts),
9094
useCmd(dockerCli, opts),
9195
inspectCmd(dockerCli, opts),
9296
stopCmd(dockerCli, opts),
@@ -112,4 +116,14 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
112116

113117
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
114118
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")
119+
120+
var timeoutDuration = defaultTimeoutCli
121+
if value, ok := os.LookupEnv("BUILDX_TIMEOUT"); ok {
122+
var err error
123+
timeoutDuration, err = time.ParseDuration(value)
124+
if err != nil {
125+
timeoutDuration = defaultTimeoutCli
126+
}
127+
}
128+
flags.DurationVar(&options.timeout, "timeout", timeoutDuration, "Override the default global timeout (20 seconds)")
115129
}

controller/control/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package control
33
import (
44
"context"
55
"io"
6+
"time"
67

78
controllerapi "github.com/docker/buildx/controller/pb"
89
"github.com/docker/buildx/util/progress"
@@ -29,4 +30,5 @@ type ControlOptions struct {
2930
ServerConfig string
3031
Root string
3132
Detach bool
33+
Timeout time.Duration
3234
}

controller/remote/controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"path/filepath"
1414
"strconv"
1515
"syscall"
16-
"time"
1716

1817
"github.com/containerd/log"
1918
"github.com/docker/buildx/build"
@@ -62,7 +61,7 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts
6261
serverRoot := filepath.Join(rootDir, "shared")
6362

6463
// connect to buildx server if it is already running
65-
ctx2, cancel := context.WithTimeout(ctx, 1*time.Second)
64+
ctx2, cancel := context.WithTimeout(ctx, opts.Timeout)
6665
c, err := newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
6766
cancel()
6867
if err != nil {
@@ -90,7 +89,7 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts
9089
go wait()
9190

9291
// wait for buildx server to be ready
93-
ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
92+
ctx2, cancel = context.WithTimeout(ctx, opts.Timeout)
9493
c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
9594
cancel()
9695
if err != nil {

driver/docker-container/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (d *Driver) wait(ctx context.Context, l progress.SubLogger) error {
214214
select {
215215
case <-ctx.Done():
216216
return ctx.Err()
217-
case <-time.After(time.Duration(try*120) * time.Millisecond):
217+
case <-time.After(d.Timeout / time.Microsecond):
218218
try++
219219
continue
220220
}

0 commit comments

Comments
 (0)