Skip to content

Commit

Permalink
add timeout parameter to cli and driver
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidik committed Jul 10, 2024
1 parent 06d96d6 commit 9822961
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 22 deletions.
3 changes: 2 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ type CreateOpts struct {
Use bool
Endpoint string
Append bool
Timeout time.Duration
}

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

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
defer cancel()

nodes, err := b.LoadNodes(timeoutCtx, WithData())
Expand Down
2 changes: 2 additions & 0 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ type buildOptions struct {
control.ControlOptions

invokeConfig *invokeConfig

timeout time.Duration
}

func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error) {
Expand Down
6 changes: 5 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"time"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/driver"
Expand All @@ -27,6 +28,7 @@ type createOptions struct {
buildkitdFlags string
buildkitdConfigFile string
bootstrap bool
timeout time.Duration
// upgrade bool // perform upgrade of the driver
}

Expand Down Expand Up @@ -61,6 +63,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
Use: in.use,
Endpoint: ep,
Append: in.actionAppend,
Timeout: in.timeout,
})
if err != nil {
return err
Expand All @@ -80,7 +83,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
return nil
}

func createCmd(dockerCli command.Cli) *cobra.Command {
func createCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options createOptions

var drivers bytes.Buffer
Expand All @@ -96,6 +99,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
Short: "Create a new builder instance",
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.timeout = rootOpts.timeout
return runCreate(cmd.Context(), dockerCli, options, args)
},
ValidArgsFunction: completion.Disable,
Expand Down
4 changes: 3 additions & 1 deletion commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type inspectOptions struct {
bootstrap bool
builder string
timeout time.Duration
}

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

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
defer cancel()

nodes, err := b.LoadNodes(timeoutCtx, builder.WithData())
Expand Down Expand Up @@ -147,6 +148,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
if len(args) > 0 {
options.builder = args[0]
}
options.timeout = rootOpts.timeout
return runInspect(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.BuilderNames(dockerCli),
Expand Down
8 changes: 5 additions & 3 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const (
)

type lsOptions struct {
format string
format string
timeout time.Duration
}

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

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
defer cancel()

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

func lsCmd(dockerCli command.Cli) *cobra.Command {
func lsCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options lsOptions

cmd := &cobra.Command{
Use: "ls",
Short: "List builder instances",
Args: cli.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
options.timeout = rootOpts.timeout
return runLs(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.Disable,
Expand Down
4 changes: 3 additions & 1 deletion commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type rmOptions struct {
keepDaemon bool
allInactive bool
force bool
timeout time.Duration
}

const (
Expand Down Expand Up @@ -109,6 +110,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
options.builders = args
}
options.timeout = rootOpts.timeout
return runRm(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.BuilderNames(dockerCli),
Expand Down Expand Up @@ -150,7 +152,7 @@ func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, i
return err
}

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, in.timeout)
defer cancel()

eg, _ := errgroup.WithContext(timeoutCtx)
Expand Down
18 changes: 16 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"os"
"time"

debugcmd "github.com/docker/buildx/commands/debug"
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
Expand All @@ -20,6 +21,8 @@ import (
"github.com/spf13/pflag"
)

const defaultTimeoutCli = 20 * time.Second

func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Short: "Docker Buildx",
Expand Down Expand Up @@ -74,6 +77,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman

type rootOptions struct {
builder string
timeout time.Duration
}

func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
Expand All @@ -83,10 +87,10 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
cmd.AddCommand(
buildCmd(dockerCli, opts, nil),
bakeCmd(dockerCli, opts),
createCmd(dockerCli),
createCmd(dockerCli, opts),
dialStdioCmd(dockerCli, opts),
rmCmd(dockerCli, opts),
lsCmd(dockerCli),
lsCmd(dockerCli, opts),
useCmd(dockerCli, opts),
inspectCmd(dockerCli, opts),
stopCmd(dockerCli, opts),
Expand All @@ -112,4 +116,14 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {

func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")

var timeoutDuration = defaultTimeoutCli
if value, ok := os.LookupEnv("BUILDX_TIMEOUT"); ok {
var err error
timeoutDuration, err = time.ParseDuration(value)
if err != nil {
timeoutDuration = defaultTimeoutCli
}
}
flags.DurationVar(&options.timeout, "timeout", timeoutDuration, "Override the default global timeout (20 seconds)")
}
2 changes: 2 additions & 0 deletions controller/control/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package control
import (
"context"
"io"
"time"

controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/util/progress"
Expand All @@ -29,4 +30,5 @@ type ControlOptions struct {
ServerConfig string
Root string
Detach bool
Timeout time.Duration
}
5 changes: 2 additions & 3 deletions controller/remote/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"path/filepath"
"strconv"
"syscall"
"time"

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

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

// wait for buildx server to be ready
ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
ctx2, cancel = context.WithTimeout(ctx, opts.Timeout)
c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
cancel()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (d *Driver) wait(ctx context.Context, l progress.SubLogger) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Duration(try*120) * time.Millisecond):
case <-time.After(d.Timeout / time.Microsecond):
try++
continue
}
Expand Down
10 changes: 6 additions & 4 deletions driver/kubernetes/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type Driver struct {
configMapClient clientcorev1.ConfigMapInterface
podChooser podchooser.PodChooser
defaultLoad bool
timeout time.Duration
}

func (d *Driver) IsMobyDriver() bool {
Expand Down Expand Up @@ -90,7 +89,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
}
}
return sub.Wrap(
fmt.Sprintf("waiting for %d pods to be ready, timeout: %s", d.minReplicas, units.HumanDuration(d.timeout)),
fmt.Sprintf("waiting for %d pods to be ready, timeout: %s", d.minReplicas, units.HumanDuration(d.Timeout)),
func() error {
return d.wait(ctx)
})
Expand All @@ -104,8 +103,11 @@ func (d *Driver) wait(ctx context.Context) error {
depl *appsv1.Deployment
)

timeoutChan := time.After(d.timeout)
ticker := time.NewTicker(100 * time.Millisecond)
// 120 * 1000 * 1000 * 1000 =
// 100 * 1000 * 1000 =

timeoutChan := time.After(d.Timeout)
ticker := time.NewTicker(d.Timeout / time.Microsecond)
defer ticker.Stop()

for {
Expand Down
5 changes: 2 additions & 3 deletions driver/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const (
prioritySupported = 40
priorityUnsupported = 80
defaultTimeout = 120 * time.Second
)

func init() {
Expand Down Expand Up @@ -78,7 +77,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
}

d.defaultLoad = defaultLoad
d.timeout = timeout
d.Timeout = timeout

d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt)
if err != nil {
Expand Down Expand Up @@ -119,7 +118,7 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
}

defaultLoad := false
timeout := defaultTimeout
timeout := cfg.Timeout

deploymentOpt.Qemu.Image = bkimage.QemuImage

Expand Down
5 changes: 5 additions & 0 deletions driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"sync"
"time"

dockerclient "github.com/docker/docker/client"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -60,8 +61,11 @@ type InitConfig struct {
Platforms []specs.Platform
ContextPathHash string // can be used for determining pods in the driver instance
DialMeta map[string][]string
Timeout time.Duration
}

const defaultTimeoutDriver = 120 * time.Second

var drivers map[string]Factory

func Register(f Factory) {
Expand Down Expand Up @@ -117,6 +121,7 @@ func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string,
ContextPathHash: contextPathHash,
DialMeta: dialMeta,
Files: files,
Timeout: defaultTimeoutDriver,
}
if f == nil {
var err error
Expand Down
3 changes: 1 addition & 2 deletions driver/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/docker/buildx/driver"
util "github.com/docker/buildx/driver/remote/util"
Expand Down Expand Up @@ -47,7 +46,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
return err
}
return progress.Wrap("[internal] waiting for connection", l, func(_ progress.SubLogger) error {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
ctx, cancel := context.WithTimeout(ctx, d.Timeout)
defer cancel()
return c.Wait(ctx)
})
Expand Down
7 changes: 7 additions & 0 deletions driver/remote/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

// import connhelpers for special url schemes
_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
Expand Down Expand Up @@ -87,6 +88,12 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
return nil, err
}
d.defaultLoad = parsed
case "timeout":
parsed, err := time.ParseDuration(v)
if err != nil {
return nil, err
}
d.Timeout = parsed
default:
return nil, errors.Errorf("invalid driver option %s for remote driver", k)
}
Expand Down

0 comments on commit 9822961

Please sign in to comment.