From 3dc83e5dd8c4c0061c804266fb34342ea60966c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Thu, 17 Mar 2022 18:36:35 +0300 Subject: [PATCH] feat: env driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Furkan Türkal Signed-off-by: Batuhan Apaydın --- cmd/buildx/main.go | 1 + commands/util.go | 13 ++++++-- driver/env/driver.go | 70 +++++++++++++++++++++++++++++++++++++++++++ driver/env/factory.go | 52 ++++++++++++++++++++++++++++++++ driver/manager.go | 9 +++++- 5 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 driver/env/driver.go create mode 100644 driver/env/factory.go diff --git a/cmd/buildx/main.go b/cmd/buildx/main.go index 28476c8c..0a6295bc 100644 --- a/cmd/buildx/main.go +++ b/cmd/buildx/main.go @@ -23,6 +23,7 @@ import ( _ "github.com/docker/buildx/driver/docker" _ "github.com/docker/buildx/driver/docker-container" + _ "github.com/docker/buildx/driver/env" _ "github.com/docker/buildx/driver/kubernetes" ) diff --git a/commands/util.go b/commands/util.go index 1c74d8c1..1500ab73 100644 --- a/commands/util.go +++ b/commands/util.go @@ -6,6 +6,8 @@ import ( "os" "strings" + buildkitclient "github.com/moby/buildkit/client" + "github.com/docker/buildx/build" "github.com/docker/buildx/driver" ctxkube "github.com/docker/buildx/driver/kubernetes/context" @@ -80,6 +82,13 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N defer func() { dis[i] = di }() + + buildkitAPI, err := buildkitclient.New(ctx, n.Endpoint) + if err != nil { + di.Err = err + return nil + } + dockerapi, err := clientForEndpoint(dockerCli, n.Endpoint) if err != nil { di.Err = err @@ -118,7 +127,7 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N } } - d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, imageopt.Auth, kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, contextPathHash) + d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, n.Endpoint, buildkitAPI, imageopt.Auth, kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, contextPathHash, ng.Driver) if err != nil { di.Err = err return nil @@ -259,7 +268,7 @@ func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly b return nil, err } - d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), imageopt.Auth, nil, nil, nil, nil, nil, contextPathHash) + d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), "", nil, imageopt.Auth, nil, nil, nil, nil, nil, contextPathHash, "") if err != nil { return nil, err } diff --git a/driver/env/driver.go b/driver/env/driver.go new file mode 100644 index 00000000..acc1216d --- /dev/null +++ b/driver/env/driver.go @@ -0,0 +1,70 @@ +package env + +import ( + "context" + "fmt" + + "github.com/docker/buildx/driver" + "github.com/docker/buildx/util/progress" + "github.com/moby/buildkit/client" + buildkitclient "github.com/moby/buildkit/client" + "github.com/pkg/errors" +) + +type Driver struct { + factory driver.Factory + driver.InitConfig + BuldkitdAddr string + BuildkitAPI *buildkitclient.Client +} + +func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error { + return nil +} + +func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { + if d.BuldkitdAddr == "" && d.Driver == "env" { + return nil, errors.Errorf("buldkitd addr must not be empty") + } + + c, err := client.New(ctx, d.BuldkitdAddr) + if err != nil { + return nil, errors.Wrapf(driver.ErrNotConnecting, err.Error()) + } + + if _, err := c.ListWorkers(ctx); err != nil { + return nil, errors.Wrapf(driver.ErrNotConnecting, err.Error()) + } + + return &driver.Info{ + Status: driver.Running, + }, nil +} + +func (d *Driver) Stop(ctx context.Context, force bool) error { + return fmt.Errorf("stop command is not implemented for this driver") +} + +func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error { + return fmt.Errorf("rm command is not implemented for this driver") +} + +func (d *Driver) Client(ctx context.Context) (*client.Client, error) { + return client.New(ctx, d.BuldkitdAddr, client.WithSessionDialer(d.BuildkitAPI.Dialer())) +} + +func (d *Driver) Features() map[driver.Feature]bool { + return map[driver.Feature]bool{} +} + +func (d *Driver) Factory() driver.Factory { + return d.factory +} + +func (d *Driver) IsMobyDriver() bool { + return false +} + +func (d *Driver) Config() driver.InitConfig { + return d.InitConfig +} diff --git a/driver/env/factory.go b/driver/env/factory.go new file mode 100644 index 00000000..309686dc --- /dev/null +++ b/driver/env/factory.go @@ -0,0 +1,52 @@ +package env + +import ( + "context" + + "github.com/docker/buildx/driver" + dockerclient "github.com/docker/docker/client" + "github.com/pkg/errors" +) + +const prioritySupported = 50 +const priorityUnsupported = 90 + +func init() { + driver.Register(&factory{}) +} + +type factory struct { +} + +func (*factory) Name() string { + return "env" +} + +func (*factory) Usage() string { + return "env" +} + +func (*factory) Priority(ctx context.Context, api dockerclient.APIClient) int { + if api == nil { + return priorityUnsupported + } + + return prioritySupported +} + +func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver, error) { + if len(cfg.Files) > 0 { + return nil, errors.Errorf("setting config file is not supported for remote driver") + } + + return &Driver{ + factory: f, + InitConfig: cfg, + BuldkitdAddr: cfg.BuldkitdAddr, + BuildkitAPI: cfg.BuildkitAPI, + }, nil +} + +func (f *factory) AllowsInstances() bool { + return true +} diff --git a/driver/manager.go b/driver/manager.go index 38be00f1..83854ca7 100644 --- a/driver/manager.go +++ b/driver/manager.go @@ -11,6 +11,7 @@ import ( dockerclient "github.com/docker/docker/client" "github.com/moby/buildkit/client" + buildkitclient "github.com/moby/buildkit/client" specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" ) @@ -52,8 +53,11 @@ type InitConfig struct { Name string DockerAPI dockerclient.APIClient KubeClientConfig KubeClientConfig + BuldkitdAddr string + BuildkitAPI *buildkitclient.Client BuildkitFlags []string Files map[string][]byte + Driver string DriverOpts map[string]string Auth Auth Platforms []specs.Platform @@ -103,13 +107,16 @@ func GetFactory(name string, instanceRequired bool) Factory { return nil } -func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, auth Auth, kcc KubeClientConfig, flags []string, files map[string][]byte, do map[string]string, platforms []specs.Platform, contextPathHash string) (Driver, error) { +func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, buldkitdAddr string, buildkitAPI *buildkitclient.Client, auth Auth, kcc KubeClientConfig, flags []string, files map[string][]byte, do map[string]string, platforms []specs.Platform, contextPathHash string, driver string) (Driver, error) { ic := InitConfig{ DockerAPI: api, KubeClientConfig: kcc, Name: name, + BuldkitdAddr: buldkitdAddr, + BuildkitAPI: buildkitAPI, BuildkitFlags: flags, DriverOpts: do, + Driver: driver, Auth: auth, Platforms: platforms, ContextPathHash: contextPathHash,