Complete remote driver
This patch completes the work started in creating a remote driver: - Renames the env driver to the remote driver (an alternative suggestion that should be more user-friendly) - Adds support for TLS to encrypt connections with buildkitd - Fixes outstanding review comments - Reworks the buildx create command endpoint construction to be clearer and include better support for this new driver. Signed-off-by: Justin Chadwell <me@jedevc.com>pull/1078/head
parent
3dc83e5dd8
commit
d7e4affe98
@ -1,52 +0,0 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/buildx/driver"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const prioritySupported = 20
|
||||
const priorityUnsupported = 90
|
||||
|
||||
var schemeRegexp = regexp.MustCompile("^(tcp|unix)://")
|
||||
|
||||
func init() {
|
||||
driver.Register(&factory{})
|
||||
}
|
||||
|
||||
type factory struct {
|
||||
}
|
||||
|
||||
func (*factory) Name() string {
|
||||
return "remote"
|
||||
}
|
||||
|
||||
func (*factory) Usage() string {
|
||||
return "remote"
|
||||
}
|
||||
|
||||
func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int {
|
||||
if schemeRegexp.MatchString(endpoint) {
|
||||
return prioritySupported
|
||||
}
|
||||
return priorityUnsupported
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
if len(cfg.BuildkitFlags) > 0 {
|
||||
return nil, errors.Errorf("setting buildkit flags is not supported for remote driver")
|
||||
}
|
||||
|
||||
d := &Driver{
|
||||
factory: f,
|
||||
InitConfig: cfg,
|
||||
}
|
||||
|
||||
tls := &tlsOpts{}
|
||||
tlsEnabled := false
|
||||
for k, v := range cfg.DriverOpts {
|
||||
switch k {
|
||||
case "servername":
|
||||
tls.serverName = v
|
||||
tlsEnabled = true
|
||||
case "cacert":
|
||||
if !filepath.IsAbs(v) {
|
||||
return nil, errors.Errorf("non-absolute path '%s' provided for %s", v, k)
|
||||
}
|
||||
tls.caCert = v
|
||||
tlsEnabled = true
|
||||
case "cert":
|
||||
if !filepath.IsAbs(v) {
|
||||
return nil, errors.Errorf("non-absolute path '%s' provided for %s", v, k)
|
||||
}
|
||||
tls.cert = v
|
||||
tlsEnabled = true
|
||||
case "key":
|
||||
if !filepath.IsAbs(v) {
|
||||
return nil, errors.Errorf("non-absolute path '%s' provided for %s", v, k)
|
||||
}
|
||||
tls.key = v
|
||||
tlsEnabled = true
|
||||
default:
|
||||
return nil, errors.Errorf("invalid driver option %s for remote driver", k)
|
||||
}
|
||||
}
|
||||
|
||||
if tlsEnabled {
|
||||
if tls.serverName == "" {
|
||||
// guess servername as hostname of target address
|
||||
uri, err := url.Parse(cfg.EndpointAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tls.serverName = uri.Hostname()
|
||||
}
|
||||
missing := []string{}
|
||||
if tls.caCert == "" {
|
||||
missing = append(missing, "cacert")
|
||||
}
|
||||
if tls.cert == "" {
|
||||
missing = append(missing, "cert")
|
||||
}
|
||||
if tls.key == "" {
|
||||
missing = append(missing, "key")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return nil, errors.Errorf("tls enabled, but missing keys %s", strings.Join(missing, ", "))
|
||||
}
|
||||
d.tlsOpts = tls
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (f *factory) AllowsInstances() bool {
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue