2021-05-26 01:30:49 +02:00
|
|
|
package client
|
2020-12-30 03:45:16 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-17 05:13:51 +01:00
|
|
|
"fmt"
|
2020-12-30 03:45:16 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2021-08-20 14:07:04 +02:00
|
|
|
"sync"
|
2020-12-30 03:45:16 +01:00
|
|
|
|
2022-04-05 17:28:53 +02:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2022-03-23 23:02:17 +01:00
|
|
|
"github.com/google/uuid"
|
2021-11-25 01:05:53 +01:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-12-30 03:45:16 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-01-14 02:38:16 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-04-05 17:28:53 +02:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
|
2020-12-30 03:45:16 +01:00
|
|
|
// Cue
|
|
|
|
|
|
|
|
// buildkit
|
|
|
|
bk "github.com/moby/buildkit/client"
|
2022-04-15 21:27:22 +02:00
|
|
|
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the docker connection driver
|
|
|
|
_ "github.com/moby/buildkit/client/connhelper/kubepod" // import the kubernetes connection driver
|
|
|
|
_ "github.com/moby/buildkit/client/connhelper/podmancontainer" // import the podman connection driver
|
|
|
|
|
2021-02-02 20:03:12 +01:00
|
|
|
bkgw "github.com/moby/buildkit/frontend/gateway/client"
|
2021-04-27 03:00:03 +02:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-12-30 03:45:16 +01:00
|
|
|
|
|
|
|
// docker output
|
2021-11-17 01:13:45 +01:00
|
|
|
"go.dagger.io/dagger/plancontext"
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/util/buildkitd"
|
|
|
|
"go.dagger.io/dagger/util/progressui"
|
2021-02-07 08:36:21 +01:00
|
|
|
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/solver"
|
2020-12-30 03:45:16 +01:00
|
|
|
)
|
|
|
|
|
2021-07-30 08:02:03 +02:00
|
|
|
// Client is a dagger client
|
2020-12-30 03:45:16 +01:00
|
|
|
type Client struct {
|
2021-07-23 16:05:49 +02:00
|
|
|
c *bk.Client
|
|
|
|
cfg Config
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 16:05:49 +02:00
|
|
|
type Config struct {
|
|
|
|
NoCache bool
|
|
|
|
|
|
|
|
CacheExports []bk.CacheOptionsEntry
|
|
|
|
CacheImports []bk.CacheOptionsEntry
|
2022-04-05 23:17:33 +02:00
|
|
|
|
|
|
|
TargetPlatform *specs.Platform
|
2021-07-23 16:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx context.Context, host string, cfg Config) (*Client, error) {
|
2021-01-30 02:16:22 +01:00
|
|
|
if host == "" {
|
|
|
|
host = os.Getenv("BUILDKIT_HOST")
|
|
|
|
}
|
|
|
|
if host == "" {
|
2021-03-03 01:14:53 +01:00
|
|
|
h, err := buildkitd.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
host = h
|
2021-01-30 02:16:22 +01:00
|
|
|
}
|
2021-03-04 03:09:25 +01:00
|
|
|
opts := []bk.ClientOpt{}
|
2021-07-02 14:26:35 +02:00
|
|
|
|
2021-11-25 01:05:53 +01:00
|
|
|
if span := trace.SpanFromContext(ctx); span != nil {
|
|
|
|
opts = append(opts, bk.WithTracerProvider(span.TracerProvider()))
|
|
|
|
}
|
2021-07-02 14:26:35 +02:00
|
|
|
|
2021-03-04 03:09:25 +01:00
|
|
|
c, err := bk.New(ctx, host, opts...)
|
2021-01-30 02:16:22 +01:00
|
|
|
if err != nil {
|
2021-02-17 05:13:51 +01:00
|
|
|
return nil, fmt.Errorf("buildkit client: %w", err)
|
2021-01-30 02:16:22 +01:00
|
|
|
}
|
|
|
|
return &Client{
|
2021-07-23 16:05:49 +02:00
|
|
|
c: c,
|
|
|
|
cfg: cfg,
|
2021-01-30 02:16:22 +01:00
|
|
|
}, nil
|
2021-01-15 00:19:39 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
type DoFunc func(context.Context, *solver.Solver) error
|
2021-04-01 01:32:15 +02:00
|
|
|
|
2021-03-27 00:44:13 +01:00
|
|
|
// FIXME: return completed *Route, instead of *compiler.Value
|
2021-11-30 21:48:09 +01:00
|
|
|
func (c *Client) Do(ctx context.Context, pctx *plancontext.Context, fn DoFunc) error {
|
2021-01-28 23:58:13 +01:00
|
|
|
lg := log.Ctx(ctx)
|
2021-02-02 20:32:35 +01:00
|
|
|
eg, gctx := errgroup.WithContext(ctx)
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2022-04-05 23:17:33 +02:00
|
|
|
if c.cfg.TargetPlatform != nil {
|
|
|
|
pctx.Platform.Set(*c.cfg.TargetPlatform)
|
|
|
|
} else {
|
2022-04-05 17:28:53 +02:00
|
|
|
p, err := c.detectPlatform(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pctx.Platform.Set(*p)
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:10:47 +01:00
|
|
|
// Spawn print function
|
2021-02-24 00:39:06 +01:00
|
|
|
events := make(chan *bk.SolveStatus)
|
|
|
|
eg.Go(func() error {
|
|
|
|
// Create a background context so that logging will not be cancelled
|
|
|
|
// with the main context.
|
|
|
|
dispCtx := lg.WithContext(context.Background())
|
2021-11-17 01:13:45 +01:00
|
|
|
return c.logSolveStatus(dispCtx, pctx, events)
|
2021-02-24 00:39:06 +01:00
|
|
|
})
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2021-02-02 20:32:35 +01:00
|
|
|
// Spawn build function
|
|
|
|
eg.Go(func() error {
|
2021-11-30 21:48:09 +01:00
|
|
|
return c.buildfn(gctx, pctx, fn, events)
|
2021-02-02 20:32:35 +01:00
|
|
|
})
|
|
|
|
|
2021-07-13 16:15:34 +02:00
|
|
|
return eg.Wait()
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 19:29:36 +02:00
|
|
|
// detectPlatform tries using Buildkit's target platform;
|
|
|
|
// if not possible, default platform will be used.
|
2022-04-05 17:28:53 +02:00
|
|
|
func (c *Client) detectPlatform(ctx context.Context) (*specs.Platform, error) {
|
|
|
|
w, err := c.c.ListWorkers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error detecting platform %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
if len(w) > 0 && len(w[0].Platforms) > 0 {
|
|
|
|
dPlatform := w[0].Platforms[0]
|
|
|
|
lg.Debug().
|
|
|
|
Str("platform", fmt.Sprintf("%s", dPlatform)).
|
|
|
|
Msg("platform detected automatically")
|
|
|
|
return &dPlatform, nil
|
|
|
|
}
|
|
|
|
defaultPlatform := platforms.DefaultSpec()
|
|
|
|
return &defaultPlatform, nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
func convertCacheOptionEntries(ims []bk.CacheOptionsEntry) []bkgw.CacheOptionsEntry {
|
|
|
|
convertIms := []bkgw.CacheOptionsEntry{}
|
|
|
|
|
|
|
|
for _, im := range ims {
|
|
|
|
convertIm := bkgw.CacheOptionsEntry{
|
|
|
|
Type: im.Type,
|
|
|
|
Attrs: im.Attrs,
|
|
|
|
}
|
|
|
|
convertIms = append(convertIms, convertIm)
|
|
|
|
}
|
|
|
|
return convertIms
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
func (c *Client) buildfn(ctx context.Context, pctx *plancontext.Context, fn DoFunc, ch chan *bk.SolveStatus) error {
|
2021-08-20 18:39:12 +02:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
2021-08-20 14:07:04 +02:00
|
|
|
// Close output channel
|
2021-08-20 18:39:12 +02:00
|
|
|
defer func() {
|
|
|
|
// Wait until all the events are caught
|
|
|
|
wg.Wait()
|
|
|
|
close(ch)
|
|
|
|
}()
|
2021-08-20 14:07:04 +02:00
|
|
|
|
2021-01-14 02:38:16 +01:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
2021-04-27 03:00:03 +02:00
|
|
|
// buildkit auth provider (registry)
|
2021-05-26 01:30:49 +02:00
|
|
|
auth := solver.NewRegistryAuthProvider()
|
2021-04-27 03:00:03 +02:00
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
localdirs, err := pctx.LocalDirs.Paths()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
// Setup solve options
|
|
|
|
opts := bk.SolveOpt{
|
2021-02-02 20:03:12 +01:00
|
|
|
LocalDirs: localdirs,
|
2021-06-05 00:56:59 +02:00
|
|
|
Session: []session.Attachable{
|
|
|
|
auth,
|
2021-11-17 01:13:45 +01:00
|
|
|
solver.NewSecretsStoreProvider(pctx),
|
|
|
|
solver.NewDockerSocketProvider(pctx),
|
2021-06-05 00:56:59 +02:00
|
|
|
},
|
2021-07-23 16:05:49 +02:00
|
|
|
CacheExports: c.cfg.CacheExports,
|
|
|
|
CacheImports: c.cfg.CacheImports,
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
// Call buildkit solver
|
2021-01-22 01:39:29 +01:00
|
|
|
lg.Debug().
|
|
|
|
Interface("localdirs", opts.LocalDirs).
|
|
|
|
Interface("attrs", opts.FrontendAttrs).
|
|
|
|
Msg("spawning buildkit job")
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-08-20 14:07:04 +02:00
|
|
|
// Catch output from events
|
|
|
|
catchOutput := func(inCh chan *bk.SolveStatus) {
|
|
|
|
for e := range inCh {
|
2021-08-19 20:10:33 +02:00
|
|
|
ch <- e
|
|
|
|
}
|
2021-08-20 14:07:04 +02:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch build events
|
|
|
|
// Closed by buildkit
|
|
|
|
buildCh := make(chan *bk.SolveStatus)
|
|
|
|
wg.Add(1)
|
|
|
|
go catchOutput(buildCh)
|
2021-08-19 20:10:33 +02:00
|
|
|
|
2021-03-12 01:41:19 +01:00
|
|
|
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
|
2022-04-08 02:23:54 +02:00
|
|
|
// Catch solver's events
|
|
|
|
// Closed by solver.Stop
|
|
|
|
eventsCh := make(chan *bk.SolveStatus)
|
|
|
|
wg.Add(1)
|
|
|
|
go catchOutput(eventsCh)
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
s := solver.New(solver.Opts{
|
2022-03-23 23:02:17 +01:00
|
|
|
Control: c.c,
|
|
|
|
Gateway: gw,
|
|
|
|
Events: eventsCh,
|
|
|
|
Auth: auth,
|
|
|
|
NoCache: c.cfg.NoCache,
|
|
|
|
CacheImports: convertCacheOptionEntries(opts.CacheImports),
|
2021-05-26 01:30:49 +02:00
|
|
|
})
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-08-23 16:57:19 +02:00
|
|
|
// Close events channel
|
|
|
|
defer s.Stop()
|
|
|
|
|
2021-02-02 20:03:12 +01:00
|
|
|
// Compute output overlay
|
2022-03-23 23:02:17 +01:00
|
|
|
res := bkgw.NewResult()
|
2021-04-01 01:32:15 +02:00
|
|
|
if fn != nil {
|
2022-03-23 23:02:17 +01:00
|
|
|
err := fn(ctx, s)
|
|
|
|
if err != nil {
|
2021-04-01 01:32:15 +02:00
|
|
|
return nil, compiler.Err(err)
|
|
|
|
}
|
2021-02-24 01:37:45 +01:00
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
refs := s.References()
|
|
|
|
// Add functions layers
|
|
|
|
for _, ref := range refs {
|
|
|
|
res.AddRef(uuid.New().String(), ref)
|
|
|
|
}
|
2021-02-02 20:03:12 +01:00
|
|
|
}
|
2021-03-12 22:00:11 +01:00
|
|
|
return res, nil
|
2021-08-20 14:07:04 +02:00
|
|
|
}, buildCh)
|
2021-01-11 20:51:15 +01:00
|
|
|
if err != nil {
|
2021-05-26 03:56:16 +02:00
|
|
|
return solver.CleanError(err)
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
|
|
|
for k, v := range resp.ExporterResponse {
|
|
|
|
// FIXME consume exporter response
|
2021-01-14 02:38:16 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Str("key", k).
|
|
|
|
Str("value", v).
|
|
|
|
Msg("exporter response")
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
2021-08-20 14:07:04 +02:00
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:13:45 +01:00
|
|
|
func (c *Client) logSolveStatus(ctx context.Context, pctx *plancontext.Context, ch chan *bk.SolveStatus) error {
|
2021-02-24 00:39:06 +01:00
|
|
|
parseName := func(v *bk.Vertex) (string, string) {
|
|
|
|
// Pattern: `@name@ message`. Minimal length is len("@X@ ")
|
|
|
|
if len(v.Name) < 2 || !strings.HasPrefix(v.Name, "@") {
|
|
|
|
return "", v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixEndPos := strings.Index(v.Name[1:], "@")
|
|
|
|
if prefixEndPos == -1 {
|
|
|
|
return "", v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
component := v.Name[1 : prefixEndPos+1]
|
|
|
|
return component, v.Name[prefixEndPos+3 : len(v.Name)]
|
|
|
|
}
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
// Just like sprintf, but redacts secrets automatically
|
|
|
|
secureSprintf := func(format string, a ...interface{}) string {
|
2022-01-07 20:57:48 +01:00
|
|
|
// Load a fresh copy of secrets (since they can be dynamically added).
|
|
|
|
secrets := pctx.Secrets.List()
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
s := fmt.Sprintf(format, a...)
|
2021-11-17 01:13:45 +01:00
|
|
|
for _, secret := range secrets {
|
2021-12-01 02:51:28 +01:00
|
|
|
s = strings.ReplaceAll(s, secret.PlainText(), "***")
|
2021-05-29 11:18:01 +02:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-02-24 00:39:06 +01:00
|
|
|
return progressui.PrintSolveStatus(ctx, ch,
|
|
|
|
func(v *bk.Vertex, index int) {
|
|
|
|
component, name := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
2021-11-25 01:58:24 +01:00
|
|
|
Str("task", component).
|
2021-02-24 00:39:06 +01:00
|
|
|
Logger()
|
|
|
|
|
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(secureSprintf("#%d %s\n", index, name))
|
2021-02-24 00:39:06 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(secureSprintf("#%d %s\n", index, v.Digest))
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
func(v *bk.Vertex, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
2021-11-25 01:58:24 +01:00
|
|
|
Str("task", component).
|
2021-02-24 00:39:06 +01:00
|
|
|
Logger()
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
msg := secureSprintf(format, a...)
|
2021-02-24 00:39:06 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(msg)
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
func(v *bk.Vertex, stream int, partial bool, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
2021-11-25 01:58:24 +01:00
|
|
|
Str("task", component).
|
2021-02-24 00:39:06 +01:00
|
|
|
Logger()
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
msg := secureSprintf(format, a...)
|
2021-06-22 12:48:42 +02:00
|
|
|
lg.
|
|
|
|
Info().
|
|
|
|
Msg(msg)
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
)
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|