From f0af89a204686783cd29dd158db7883dd9b68b27 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 8 Jul 2019 15:06:07 -0700 Subject: [PATCH] driver: allow configuring buildkitd flags Signed-off-by: Tonis Tiigi --- commands/create.go | 13 ++++++++++++- commands/inspect.go | 3 +++ commands/util.go | 4 ++-- driver/docker-container/driver.go | 12 +++++++++--- driver/manager.go | 15 ++++++++------- go.mod | 1 + store/nodegroup.go | 7 ++++++- store/nodegroup_test.go | 10 ++++++---- 8 files changed, 47 insertions(+), 18 deletions(-) diff --git a/commands/create.go b/commands/create.go index 31cc361f..280c115c 100644 --- a/commands/create.go +++ b/commands/create.go @@ -8,6 +8,7 @@ import ( "github.com/docker/buildx/store" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/google/shlex" "github.com/moby/buildkit/util/appcontext" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -22,6 +23,7 @@ type createOptions struct { actionAppend bool actionLeave bool use bool + flags string // upgrade bool // perform upgrade of the driver } @@ -107,6 +109,14 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { ng.Driver = driverName } + var flags []string + if in.flags != "" { + flags, err = shlex.Split(in.flags) + if err != nil { + return errors.Wrap(err, "failed to parse buildkit flags") + } + } + var ep string if in.actionLeave { if err := ng.Leave(in.nodeName); err != nil { @@ -128,7 +138,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { return err } } - if err := ng.Update(in.nodeName, ep, in.platform, len(args) > 0, in.actionAppend); err != nil { + if err := ng.Update(in.nodeName, ep, in.platform, len(args) > 0, in.actionAppend, flags); err != nil { return err } } @@ -173,6 +183,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command { flags.StringVar(&options.name, "name", "", "Builder instance name") flags.StringVar(&options.driver, "driver", "", fmt.Sprintf("Driver to use (available: %v)", drivers)) flags.StringVar(&options.nodeName, "node", "", "Create/modify node with given name") + flags.StringVar(&options.flags, "buildkitd-flags", "", "Flags for buildkitd daemon") flags.StringArrayVar(&options.platform, "platform", []string{}, "Fixed platforms for current node") flags.BoolVar(&options.actionAppend, "append", false, "Append a node to builder instead of changing it") diff --git a/commands/inspect.go b/commands/inspect.go index 3d2fe745..ccf53961 100644 --- a/commands/inspect.go +++ b/commands/inspect.go @@ -114,6 +114,9 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error { fmt.Fprintf(w, "Error:\t%s\n", err.Error()) } else { fmt.Fprintf(w, "Status:\t%s\n", ngi.drivers[i].info.Status) + if len(n.Flags) > 0 { + fmt.Fprintf(w, "Flags:\t%s\n", strings.Join(n.Flags, " ")) + } fmt.Fprintf(w, "Platforms:\t%s\n", strings.Join(platformutil.Format(platformutil.Dedupe(append(n.Platforms, ngi.drivers[i].platforms...))), ", ")) } } diff --git a/commands/util.go b/commands/util.go index 7b905a43..7c777b4c 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) + d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, n.Flags) 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()) + d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil) if err != nil { return nil, err } diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index af47d520..50e9dfb1 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -59,10 +59,16 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error { }); err != nil { return err } + + cfg := &container.Config{ + Image: buildkitImage, + } + if d.InitConfig.BuildkitFlags != nil { + cfg.Cmd = d.InitConfig.BuildkitFlags + } + if err := l.Wrap("creating container "+d.Name, func() error { - _, err := d.DockerAPI.ContainerCreate(ctx, &container.Config{ - Image: buildkitImage, - }, &container.HostConfig{ + _, err := d.DockerAPI.ContainerCreate(ctx, cfg, &container.HostConfig{ Privileged: true, }, &network.NetworkingConfig{}, d.Name) if err != nil { diff --git a/driver/manager.go b/driver/manager.go index 82b01959..f2022fe8 100644 --- a/driver/manager.go +++ b/driver/manager.go @@ -23,10 +23,10 @@ type BuildkitConfig struct { type InitConfig struct { // This object needs updates to be generic for different drivers - Name string - DockerAPI dockerclient.APIClient - BuildkitConfig BuildkitConfig - Meta map[string]interface{} + Name string + DockerAPI dockerclient.APIClient + BuildkitFlags []string + Meta map[string]interface{} } var drivers map[string]Factory @@ -71,10 +71,11 @@ func GetFactory(name string, instanceRequired bool) Factory { return nil } -func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient) (Driver, error) { +func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, flags []string) (Driver, error) { ic := InitConfig{ - DockerAPI: api, - Name: name, + DockerAPI: api, + Name: name, + BuildkitFlags: flags, } if f == nil { var err error diff --git a/go.mod b/go.mod index 18e49d0f..f7f888c9 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/gogo/protobuf v1.2.1 // indirect github.com/google/certificate-transparency-go v1.0.21 // indirect github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect + github.com/google/shlex v0.0.0-20150127133951-6f45313302b9 github.com/gorilla/mux v1.7.0 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/hashicorp/go-version v1.1.0 // indirect diff --git a/store/nodegroup.go b/store/nodegroup.go index 3a1604ef..93674730 100644 --- a/store/nodegroup.go +++ b/store/nodegroup.go @@ -19,6 +19,7 @@ type Node struct { Name string Endpoint string Platforms []specs.Platform + Flags []string } func (ng *NodeGroup) Leave(name string) error { @@ -33,7 +34,7 @@ func (ng *NodeGroup) Leave(name string) error { return nil } -func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpointsSet bool, actionAppend bool) error { +func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpointsSet bool, actionAppend bool, flags []string) error { i := ng.findNode(name) if i == -1 && !actionAppend { if len(ng.Nodes) > 0 { @@ -55,6 +56,9 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints if len(platforms) > 0 { n.Platforms = pp } + if flags != nil { + n.Flags = flags + } ng.Nodes[i] = n if err := ng.validateDuplicates(endpoint, i); err != nil { return err @@ -75,6 +79,7 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints Name: name, Endpoint: endpoint, Platforms: pp, + Flags: flags, } ng.Nodes = append(ng.Nodes, n) diff --git a/store/nodegroup_test.go b/store/nodegroup_test.go index f4f7eb35..2f8f4295 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) + err := ng.Update("foo", "foo0", []string{"linux/amd64"}, true, false, []string{"--debug"}) require.NoError(t, err) - err = ng.Update("foo1", "foo1", []string{"linux/arm64", "linux/arm/v7"}, true, true) + err = ng.Update("foo1", "foo1", []string{"linux/arm64", "linux/arm/v7"}, true, true, 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) + err = ng.Update("foo", "foo2", []string{"linux/amd64", "linux/arm"}, true, false, nil) require.NoError(t, err) require.Equal(t, len(ng.Nodes), 2) @@ -28,9 +28,11 @@ func TestNodeGroupUpdate(t *testing.T) { require.Equal(t, []string{"linux/arm64"}, platformutil.Format(ng.Nodes[1].Platforms)) require.Equal(t, "foo2", ng.Nodes[0].Endpoint) + require.Equal(t, []string{"--debug"}, ng.Nodes[0].Flags) + require.Equal(t, []string(nil), ng.Nodes[1].Flags) // duplicate endpoint - err = ng.Update("foo1", "foo2", nil, true, false) + err = ng.Update("foo1", "foo2", nil, true, false, nil) require.Error(t, err) require.Contains(t, err.Error(), "duplicate endpoint")