-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver.go
201 lines (165 loc) · 4.48 KB
/
driver.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package buildxdriver
import (
"context"
"log"
"net"
"os"
"strings"
"time"
"github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/builder"
"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
"github.com/pkg/errors"
)
type Driver struct {
driver.InitConfig
factory driver.Factory
builder *builder.Builder
builderInfo *builder.AcquiredBuilder
*tlsOpts
done chan struct{}
}
type tlsOpts struct {
serverName string
caCert string
cert string
key string
}
func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
err := d.startHealthcheck(context.Background())
if err != nil {
return err
}
builderInfo, err := d.builder.Acquire(l)
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to bootstrap builder"))
}
d.builderInfo = builderInfo
if builderInfo.Cert != "" {
tls := &tlsOpts{}
file, err := os.CreateTemp("", "depot-cert")
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to create temp file"))
}
defer file.Close()
err = os.WriteFile(file.Name(), []byte(builderInfo.Cert), 0600)
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to write cert to temp file"))
}
tls.cert = file.Name()
file, err = os.CreateTemp("", "depot-key")
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to create temp file"))
}
defer file.Close()
err = os.WriteFile(file.Name(), []byte(builderInfo.Key), 0600)
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to write key to temp file"))
}
tls.key = file.Name()
file, err = os.CreateTemp("", "depot-ca-cert")
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to create temp file"))
}
defer file.Close()
err = os.WriteFile(file.Name(), []byte(builderInfo.CACert), 0600)
if err != nil {
return api.NewDepotError(errors.Wrap(err, "failed to write CA cert to temp file"))
}
tls.caCert = file.Name()
d.tlsOpts = tls
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
err = progress.Wrap("[depot] connecting to "+d.builder.Platform+" builder", l, func(sub progress.SubLogger) error {
for i := 0; i < 120; i++ {
info, err := d.Info(ctx)
if err != nil {
return err
}
if info.Status != driver.Inactive {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
default:
time.Sleep(1 * time.Second)
}
}
return errors.New("timed out connecting to builder")
})
if err != nil {
return api.NewDepotError(err)
}
return nil
}
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
if d.builderInfo == nil {
return &driver.Info{Status: driver.Stopped}, nil
}
c, err := d.Client(ctx)
if err != nil {
return &driver.Info{Status: driver.Inactive}, nil
}
if _, err := c.ListWorkers(ctx); err != nil {
return &driver.Info{Status: driver.Inactive}, nil
}
return &driver.Info{Status: driver.Running}, nil
}
func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
opts := []client.ClientOpt{}
if d.tlsOpts != nil {
opts = append(opts, client.WithCredentials(d.tlsOpts.serverName, d.tlsOpts.caCert, d.tlsOpts.cert, d.tlsOpts.key))
}
opts = append(opts, client.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
addr = strings.TrimPrefix(addr, "tcp://")
return net.Dial("tcp", addr)
}))
c, err := client.New(ctx, d.builderInfo.Addr, opts...)
if err != nil {
return nil, api.NewDepotError(err)
}
return c, nil
}
// Boilerplate
func (d *Driver) Config() driver.InitConfig {
return d.InitConfig
}
func (d *Driver) Factory() driver.Factory {
return d.factory
}
func (d *Driver) Features() map[driver.Feature]bool {
return map[driver.Feature]bool{
driver.OCIExporter: true,
driver.DockerExporter: true,
driver.CacheExport: true,
driver.MultiPlatform: true,
}
}
func (d *Driver) IsMobyDriver() bool {
return false
}
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool, rmDaemon bool) error {
return nil
}
func (d *Driver) Stop(ctx context.Context, force bool) error {
go func() {
d.done <- struct{}{}
}()
return nil
}
func (d *Driver) Version(ctx context.Context) (string, error) {
return "", nil
}
func (d *Driver) startHealthcheck(ctx context.Context) error {
go func() {
err := d.builder.ReportHealth(ctx)
if err != nil {
log.Printf("warning: failed to report health for %s builder: %v\n", d.builder.Platform, err)
}
}()
return nil
}