Skip to content

Commit

Permalink
Support environment variables in docker-container driver
Browse files Browse the repository at this point in the history
Fixes docker#169

Signed-off-by: Sune Keller <[email protected]>
  • Loading branch information
sirlatrom committed Oct 28, 2019
1 parent 7775953 commit c4d3301
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
10 changes: 8 additions & 2 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type createOptions struct {
flags string
configFile string
driverOpts []string
driverEnvs []string
// upgrade bool // perform upgrade of the driver
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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")
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions driver/docker-container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"context"
"fmt"

"github.com/docker/buildx/driver"
dockerclient "github.com/docker/docker/client"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type InitConfig struct {
BuildkitFlags []string
ConfigFile string
DriverOpts map[string]string
DriverEnvs map[string]string
}

var drivers map[string]Factory
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hack/binaries
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion store/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions store/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")

Expand Down

0 comments on commit c4d3301

Please sign in to comment.