feat: env driver

Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
This commit is contained in:
Batuhan Apaydın 2022-03-17 18:36:35 +03:00 committed by Justin Chadwell
parent 29f97f6762
commit 3dc83e5dd8
5 changed files with 142 additions and 3 deletions

View File

@ -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"
)

View File

@ -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
}

70
driver/env/driver.go vendored Normal file
View File

@ -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
}

52
driver/env/factory.go vendored Normal file
View File

@ -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
}

View File

@ -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,