-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildkit.go
424 lines (365 loc) · 11.1 KB
/
buildkit.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package buildkit
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/docker/cli/cli/config"
"github.com/go-logr/logr"
"github.com/google/go-containerregistry/pkg/authn"
bkclient "github.com/moby/buildkit/client"
"github.com/moby/buildkit/cmd/buildctl/build"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/session/secrets/secretsprovider"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/tonistiigi/fsutil"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/dominodatalab/hephaestus/pkg/buildkit/archive"
hephconfig "github.com/dominodatalab/hephaestus/pkg/config"
)
var clientCheckBackoff = wait.Backoff{ // retries after 500ms 1s 2s 4s 8s 16s 32s 64s with jitter
Duration: 500 * time.Millisecond,
Factor: 2.0,
Steps: 8,
Jitter: 0.1,
}
type ClientBuilder struct {
addr string
dockerConfigDir string
log logr.Logger
bkOpts []bkclient.ClientOpt
}
func NewClientBuilder(addr string) *ClientBuilder {
return &ClientBuilder{addr: addr, log: logr.Discard()}
}
func (b *ClientBuilder) WithDockerConfigDir(configDir string) *ClientBuilder {
b.dockerConfigDir = configDir
return b
}
func (b *ClientBuilder) WithMTLSAuth(caPath, certPath, keyPath string) *ClientBuilder {
u, err := url.Parse(b.addr)
if err != nil {
b.log.Error(err, "Cannot parse hostname, skipping mTLS auth", "addr", b.addr)
} else {
b.bkOpts = append(b.bkOpts,
bkclient.WithServerConfig(u.Hostname(), caPath),
bkclient.WithCredentials(certPath, keyPath),
)
}
return b
}
func (b *ClientBuilder) WithLogger(log logr.Logger) *ClientBuilder {
b.log = log
return b
}
func (b *ClientBuilder) Build(ctx context.Context) (*Client, error) {
bk, err := bkclient.New(ctx, b.addr, b.bkOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create buildkit client: %w", err)
}
var lastErr error
b.log.Info("Confirming buildkitd connectivity")
err = wait.ExponentialBackoffWithContext(ctx, clientCheckBackoff, func(ctx context.Context) (done bool, err error) {
if _, lastErr = bk.ListWorkers(ctx); lastErr != nil {
b.log.V(1).Info("Buildkitd is not ready")
//nolint:nilerr // returning and err here will stop the loop immediately
return false, nil
}
return true, nil
})
if err != nil {
return nil, fmt.Errorf("failed to contact buildkitd after %d attempts: %w", clientCheckBackoff.Steps, lastErr)
}
b.log.Info("Buildkitd connectivity established")
return &Client{
bk: bk,
log: b.log,
dockerConfigDir: b.dockerConfigDir,
}, nil
}
type BuildOptions struct {
Context string
ContextDir string
DockerfileContents string
Images []string
BuildArgs []string
NoCache bool
ImportCache []string
DisableInlineCacheExport bool
Secrets map[string]string
SecretsData map[string][]byte
FetchAndExtractTimeout time.Duration
}
type Buildkit interface {
Build(ctx context.Context, opts BuildOptions) error
Cache(ctx context.Context, image string) error
}
type Client struct {
bk *bkclient.Client
log logr.Logger
dockerConfigDir string
}
func validateCompression(compression string, name string) map[string]string {
attrs := make(map[string]string)
attrs["name"] = name
const truth = "true"
switch compression {
case "estargz":
attrs["push"] = truth
attrs["compression"] = "estargz"
attrs["force-compression"] = truth
attrs["oci-mediatypes"] = truth
case "zstd":
attrs["compression"] = "zstd"
attrs["force-compression"] = truth
attrs["push"] = truth
// default is gzip
default:
attrs["push"] = truth
}
return attrs
}
func (c *Client) Build(ctx context.Context, opts BuildOptions) (string, error) {
// setup build directory
buildDir, err := os.MkdirTemp("", "hephaestus-build-")
if err != nil {
return "", fmt.Errorf("failed to create build dir: %w", err)
}
defer func(path string) {
if err := os.RemoveAll(path); err != nil {
c.log.Error(err, "Failed to delete build context")
}
}(buildDir)
dockerConfig, err := config.Load(c.dockerConfigDir)
if err != nil {
c.log.Error(err, "Error loading config file")
}
// process build context
var contentsDir string
fi, err := os.Stat(opts.ContextDir)
switch {
case err == nil && fi.IsDir():
c.log.Info("Using context dir", "dir", opts.ContextDir)
contentsDir = opts.ContextDir
case strings.TrimSpace(opts.Context) != "":
c.log.Info("Fetching remote context", "url", opts.Context)
extract, extractErr := archive.FetchAndExtract(ctx, c.log, opts.Context, buildDir, opts.FetchAndExtractTimeout)
if extractErr != nil {
return "", fmt.Errorf("cannot fetch remote context: %w", err)
}
contentsDir = extract.ContentsDir
case strings.TrimSpace(opts.DockerfileContents) != "":
c.log.Info("Creating context from DockerfileContents")
contentsDir, err = os.MkdirTemp(buildDir, "dockerfile-contents-")
if err != nil {
return "", fmt.Errorf("cannot create temp directory for dockerfileContents: %w", err)
}
err = os.WriteFile(path.Join(contentsDir, "Dockerfile"), []byte(opts.DockerfileContents), os.FileMode(0644))
if err != nil {
return "", fmt.Errorf("cannot write temporary file for dockerfileContents: %w", err)
}
default:
return "", errors.New("no valid docker context provided")
}
c.log.V(1).Info("Context extracted", "dir", contentsDir)
// verify manifest is present
dockerfile := filepath.Join(contentsDir, "Dockerfile")
if _, err := os.Stat(dockerfile); errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("build requires a Dockerfile inside context dir: %w", err)
}
if l := c.log.V(1); l.Enabled() {
bs, err := os.ReadFile(dockerfile)
if err != nil {
return "", fmt.Errorf("cannot read Dockerfile: %w", err)
}
l.Info("Dockerfile contents:\n" + string(bs))
}
// Do not cache these as the file contents can change
// over time (e.g. when mounted from a configmap)
secrets := make(map[string][]byte)
for name, path := range opts.Secrets {
contents, err := os.ReadFile(path)
if err != nil {
return "", err
}
secrets[name] = contents
}
// merge in preloaded data
for name, contents := range opts.SecretsData {
secrets[name] = contents
}
contentsFS, err := fsutil.NewFS(contentsDir)
if err != nil {
return "", fmt.Errorf("unable to create context dir: %w", err)
}
// build solve options
solveOpt := bkclient.SolveOpt{
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{},
LocalMounts: map[string]fsutil.FS{
"context": contentsFS,
"dockerfile": contentsFS,
},
Session: []session.Attachable{
authprovider.NewDockerAuthProvider(dockerConfig, nil),
secretsprovider.FromMap(secrets),
},
CacheExports: []bkclient.CacheOptionsEntry{
{
Type: "inline",
},
},
}
if opts.NoCache {
solveOpt.FrontendAttrs["no-cache"] = ""
}
if opts.DisableInlineCacheExport {
solveOpt.CacheExports = nil
}
for _, ref := range opts.ImportCache {
solveOpt.CacheImports = []bkclient.CacheOptionsEntry{
{
Type: "registry",
Attrs: map[string]string{
"ref": ref,
},
},
}
}
if len(opts.BuildArgs) != 0 {
var args []string
for _, arg := range opts.BuildArgs {
args = append(args, fmt.Sprintf("build-arg:%s", arg))
}
attrs, err := build.ParseOpt(args)
if err != nil {
return "", fmt.Errorf("cannot parse build args: %w", err)
}
for k, v := range attrs {
solveOpt.FrontendAttrs[k] = v
}
}
for _, name := range opts.Images {
bkclientattrs := validateCompression(hephconfig.CompressionMethod, name)
solveOpt.Exports = append(solveOpt.Exports, bkclient.ExportEntry{
Type: bkclient.ExporterImage,
Attrs: bkclientattrs,
})
}
// build/push images
return c.runSolve(ctx, solveOpt)
}
func (c *Client) Cache(ctx context.Context, image string) error {
return c.solveWith(ctx, func(buildDir string, solveOpt *bkclient.SolveOpt) error {
dockerfile := filepath.Join(buildDir, "Dockerfile")
contents := []byte(fmt.Sprintf("FROM %s\nRUN echo extract\n", image))
if err := os.WriteFile(dockerfile, contents, 0644); err != nil {
return fmt.Errorf("failed to create dockerfile: %w", err)
}
buildFS, err := fsutil.NewFS(buildDir)
if err != nil {
return fmt.Errorf("failed to create build dir: %w", err)
}
solveOpt.LocalMounts = map[string]fsutil.FS{
"context": buildFS,
"dockerfile": buildFS,
}
solveOpt.Exports = []bkclient.ExportEntry{
{
Type: bkclient.ExporterOCI,
Output: func(_ map[string]string) (io.WriteCloser, error) {
return DiscardCloser{io.Discard}, nil
},
},
}
return nil
})
}
func (c *Client) Prune() error {
c.log.Info("Prune not implemented")
return nil
}
func (c *Client) solveWith(ctx context.Context, modify func(buildDir string, solveOpt *bkclient.SolveOpt) error) error {
buildDir, err := os.MkdirTemp("", "hephaestus-build-")
if err != nil {
return fmt.Errorf("failed to create build dir: %w", err)
}
defer func(path string) {
if err := os.RemoveAll(path); err != nil {
c.log.Error(err, "Failed to delete build context")
}
}(buildDir)
dockerConfig, err := config.Load(c.dockerConfigDir)
if err != nil {
c.log.Error(err, "Error loading config file")
}
solveOpt := bkclient.SolveOpt{
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{},
Session: []session.Attachable{
authprovider.NewDockerAuthProvider(dockerConfig, nil),
},
}
if err = modify(buildDir, &solveOpt); err != nil {
return err
}
_, err = c.runSolve(ctx, solveOpt)
return err
}
func (c *Client) ResolveAuth(registryHostname string) (authn.Authenticator, error) {
cf, err := config.Load(c.dockerConfigDir)
if err != nil {
return nil, err
}
cfg, err := cf.GetAuthConfig(registryHostname)
if err != nil {
return nil, err
}
return authn.FromConfig(authn.AuthConfig{
Username: cfg.Username,
Password: cfg.Password,
Auth: cfg.Auth,
IdentityToken: cfg.IdentityToken,
RegistryToken: cfg.RegistryToken,
}), nil
}
func (c *Client) runSolve(ctx context.Context, so bkclient.SolveOpt) (string, error) {
lw := &LogWriter{Logger: c.log}
ch := make(chan *bkclient.SolveStatus)
eg, ctx := errgroup.WithContext(ctx)
d, err := progressui.NewDisplay(lw, progressui.PlainMode)
if err != nil {
return "", fmt.Errorf("unable to setup buildkit logging: %w", err)
}
//nolint:contextcheck
eg.Go(func() error {
// this operation should return cleanly when solve returns (either by itself or when cancelled) so there's no
// need to cancel it explicitly. see https://github.com/moby/buildkit/pull/1721 for details.
_, err = d.UpdateFrom(context.Background(), ch)
return err
})
var imageName string
eg.Go(func() error {
res, err := c.bk.Solve(ctx, nil, so, ch)
if err != nil {
return err
}
c.log.Info("Solve complete")
imageName = res.ExporterResponse["image.name"]
return nil
})
if err := eg.Wait(); err != nil {
c.log.Info(fmt.Sprintf("Build failed: %s", err.Error()))
return "", fmt.Errorf("buildkit solve issue: %w", err)
}
c.log.Info(fmt.Sprintf("Final image name: %s", imageName))
return imageName, nil
}