File: factory.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (118 lines) | stat: -rw-r--r-- 2,747 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package remote

import (
	"context"
	"net/url"
	"path/filepath"
	"strings"

	// import connhelpers for special url schemes
	_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
	_ "github.com/moby/buildkit/client/connhelper/kubepod"
	_ "github.com/moby/buildkit/client/connhelper/ssh"

	"github.com/docker/buildx/driver"
	util "github.com/docker/buildx/driver/remote/util"
	dockerclient "github.com/docker/docker/client"
	"github.com/pkg/errors"
)

const prioritySupported = 20
const priorityUnsupported = 90

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, dialMeta map[string][]string) int {
	if util.IsValidEndpoint(endpoint) != 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")
	}
	if len(cfg.BuildkitdFlags) > 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 != "" && tls.key == "" {
			missing = append(missing, "key")
		}
		if tls.key != "" && tls.cert == "" {
			missing = append(missing, "cert")
		}
		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
}