diff --git a/commands/create.go b/commands/create.go index bada4cac633f..d61c5da95ca2 100644 --- a/commands/create.go +++ b/commands/create.go @@ -28,6 +28,7 @@ type createOptions struct { flags string configFile string driverOpts []string + driverEnvs []string // upgrade bool // perform upgrade of the driver } @@ -146,7 +147,11 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { if err != nil { return err } - if err := ng.Update(in.nodeName, ep, in.platform, len(args) > 0, in.actionAppend, flags, in.configFile, m); err != nil { + e, err := csvToMap(in.driverEnvs) + if err != nil { + return err + } + if err := ng.Update(in.nodeName, ep, in.platform, len(args) > 0, in.actionAppend, flags, in.configFile, m, e); err != nil { return err } } @@ -195,6 +200,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command { flags.StringVar(&options.configFile, "config", "", "BuildKit config file") flags.StringArrayVar(&options.platform, "platform", []string{}, "Fixed platforms for current node") flags.StringArrayVar(&options.driverOpts, "driver-opt", []string{}, "Options for the driver") + flags.StringArrayVar(&options.driverEnvs, "driver-env", []string{}, "Environment variables for the driver") flags.BoolVar(&options.actionAppend, "append", false, "Append a node to builder instead of changing it") flags.BoolVar(&options.actionLeave, "leave", false, "Remove a node from builder instead of changing it") @@ -215,7 +221,7 @@ func csvToMap(in []string) (map[string]string, error) { } for _, v := range fields { p := strings.SplitN(v, "=", 2) - if len(p) != 2 { + if len(p) < 2 { return nil, errors.Errorf("invalid value %q, expecting k=v", v) } m[p[0]] = p[1] diff --git a/commands/util.go b/commands/util.go index be38bbce510c..1123b2ca25c1 100644 --- a/commands/util.go +++ b/commands/util.go @@ -174,7 +174,7 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N // TODO: replace the following line with dockerclient.WithAPIVersionNegotiation option in clientForEndpoint dockerapi.NegotiateAPIVersion(ctx) - d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, n.Flags, n.ConfigFile, n.DriverOpts) + d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, n.Flags, n.ConfigFile, n.DriverOpts, n.DriverEnvs) if err != nil { di.Err = err return nil @@ -251,7 +251,7 @@ func getDefaultDrivers(ctx context.Context, dockerCli command.Cli) ([]build.Driv return driversForNodeGroup(ctx, dockerCli, ng) } - d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, "", nil) + d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, "", nil, nil) if err != nil { return nil, err } diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index 38be74a4566c..b68b00b09e8b 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -29,6 +29,7 @@ type Driver struct { factory driver.Factory netMode string image string + env []string } func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error { @@ -57,6 +58,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error { if d.image != "" { imageName = d.image } + env := d.env if err := l.Wrap("pulling image "+imageName, func() error { rc, err := d.DockerAPI.ImageCreate(ctx, imageName, types.ImageCreateOptions{}) if err != nil { @@ -70,6 +72,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error { cfg := &container.Config{ Image: imageName, + Env: env, } if d.InitConfig.BuildkitFlags != nil { cfg.Cmd = d.InitConfig.BuildkitFlags diff --git a/driver/docker-container/factory.go b/driver/docker-container/factory.go index 0b6c79687aec..445513b03056 100644 --- a/driver/docker-container/factory.go +++ b/driver/docker-container/factory.go @@ -2,6 +2,7 @@ package docker import ( "context" + "fmt" "github.com/docker/buildx/driver" dockerclient "github.com/docker/docker/client" @@ -51,6 +52,9 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver return nil, errors.Errorf("invalid driver option %s for docker-container driver", k) } } + for k, v := range cfg.DriverEnvs { + d.env = append(d.env, fmt.Sprintf("%s=%s", k, v)) + } return d, nil } diff --git a/driver/manager.go b/driver/manager.go index 8ddf78218c33..586f2ebf0769 100644 --- a/driver/manager.go +++ b/driver/manager.go @@ -28,6 +28,7 @@ type InitConfig struct { BuildkitFlags []string ConfigFile string DriverOpts map[string]string + DriverEnvs map[string]string } var drivers map[string]Factory @@ -72,13 +73,14 @@ func GetFactory(name string, instanceRequired bool) Factory { return nil } -func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, flags []string, config string, do map[string]string) (Driver, error) { +func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, flags []string, config string, do map[string]string, de map[string]string) (Driver, error) { ic := InitConfig{ DockerAPI: api, Name: name, BuildkitFlags: flags, ConfigFile: config, DriverOpts: do, + DriverEnvs: de, } if f == nil { var err error diff --git a/hack/binaries b/hack/binaries index c71b1f7c10c7..e8cb73ac22a8 100755 --- a/hack/binaries +++ b/hack/binaries @@ -20,7 +20,7 @@ binariesDocker() { platformFlag="--build-arg=TARGETPLATFORM=$TARGETPLATFORM" fi - docker build $platformFlag --target=binaries --iidfile $iidfile --force-rm . + docker build --build-arg http_proxy --build-arg https_proxy --build-arg no_proxy $platformFlag --target=binaries --iidfile $iidfile --force-rm . iid=$(cat $iidfile) containerID=$(docker create $iid copy) docker cp $containerID:/ bin/tmp diff --git a/store/nodegroup.go b/store/nodegroup.go index 04f8be82742a..406bbf921dfb 100644 --- a/store/nodegroup.go +++ b/store/nodegroup.go @@ -22,6 +22,7 @@ type Node struct { Flags []string ConfigFile string DriverOpts map[string]string + DriverEnvs map[string]string } func (ng *NodeGroup) Leave(name string) error { @@ -36,7 +37,7 @@ func (ng *NodeGroup) Leave(name string) error { return nil } -func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpointsSet bool, actionAppend bool, flags []string, configFile string, do map[string]string) error { +func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpointsSet bool, actionAppend bool, flags []string, configFile string, do map[string]string, de map[string]string) error { i := ng.findNode(name) if i == -1 && !actionAppend { if len(ng.Nodes) > 0 { @@ -84,6 +85,7 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints ConfigFile: configFile, Flags: flags, DriverOpts: do, + DriverEnvs: de, } ng.Nodes = append(ng.Nodes, n) diff --git a/store/nodegroup_test.go b/store/nodegroup_test.go index cdcfc77d32c4..93738bb15f3d 100644 --- a/store/nodegroup_test.go +++ b/store/nodegroup_test.go @@ -11,16 +11,16 @@ func TestNodeGroupUpdate(t *testing.T) { t.Parallel() ng := &NodeGroup{} - err := ng.Update("foo", "foo0", []string{"linux/amd64"}, true, false, []string{"--debug"}, "", nil) + err := ng.Update("foo", "foo0", []string{"linux/amd64"}, true, false, []string{"--debug"}, "", nil, nil) require.NoError(t, err) - err = ng.Update("foo1", "foo1", []string{"linux/arm64", "linux/arm/v7"}, true, true, nil, "", nil) + err = ng.Update("foo1", "foo1", []string{"linux/arm64", "linux/arm/v7"}, true, true, nil, "", nil, nil) require.NoError(t, err) require.Equal(t, len(ng.Nodes), 2) // update - err = ng.Update("foo", "foo2", []string{"linux/amd64", "linux/arm"}, true, false, nil, "", nil) + err = ng.Update("foo", "foo2", []string{"linux/amd64", "linux/arm"}, true, false, nil, "", nil, nil) require.NoError(t, err) require.Equal(t, len(ng.Nodes), 2) @@ -32,7 +32,7 @@ func TestNodeGroupUpdate(t *testing.T) { require.Equal(t, []string(nil), ng.Nodes[1].Flags) // duplicate endpoint - err = ng.Update("foo1", "foo2", nil, true, false, nil, "", nil) + err = ng.Update("foo1", "foo2", nil, true, false, nil, "", nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "duplicate endpoint")