2018-07-05 21:31:50 +00:00
|
|
|
package forwarder
|
2017-12-13 23:46:52 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-07-11 02:31:33 +00:00
|
|
|
"sync"
|
2017-12-14 01:29:03 +00:00
|
|
|
|
2018-10-02 11:17:57 +00:00
|
|
|
cacheutil "github.com/moby/buildkit/cache/util"
|
2019-12-09 20:50:21 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/frontend"
|
2020-08-05 01:11:32 +00:00
|
|
|
"github.com/moby/buildkit/frontend/gateway"
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/frontend/gateway/client"
|
2018-09-21 22:19:10 +00:00
|
|
|
gwpb "github.com/moby/buildkit/frontend/gateway/pb"
|
2020-08-05 01:11:32 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2020-09-15 06:40:05 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2020-10-28 21:07:44 +00:00
|
|
|
"github.com/moby/buildkit/solver/errdefs"
|
|
|
|
llberrdefs "github.com/moby/buildkit/solver/llbsolver/errdefs"
|
2018-09-21 22:19:10 +00:00
|
|
|
opspb "github.com/moby/buildkit/solver/pb"
|
2018-07-05 21:15:14 +00:00
|
|
|
"github.com/moby/buildkit/util/apicaps"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
2021-11-24 07:56:04 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-12-14 01:29:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-09-21 12:48:30 +00:00
|
|
|
fstypes "github.com/tonistiigi/fsutil/types"
|
2020-11-14 06:14:05 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-12-13 23:46:52 +00:00
|
|
|
)
|
|
|
|
|
2020-11-14 05:58:20 +00:00
|
|
|
func llbBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string, inputs map[string]*opspb.Definition, w worker.Infos, sid string, sm *session.Manager) (*bridgeClient, error) {
|
2018-07-05 23:32:01 +00:00
|
|
|
return &bridgeClient{
|
|
|
|
opts: opts,
|
2020-02-07 19:53:18 +00:00
|
|
|
inputs: inputs,
|
2018-07-05 23:32:01 +00:00
|
|
|
FrontendLLBBridge: llbBridge,
|
2020-06-30 01:06:02 +00:00
|
|
|
sid: sid,
|
2020-10-02 01:51:53 +00:00
|
|
|
sm: sm,
|
2020-11-14 05:58:20 +00:00
|
|
|
workers: w,
|
2018-07-05 23:32:01 +00:00
|
|
|
final: map[*ref]struct{}{},
|
2020-10-28 21:07:44 +00:00
|
|
|
workerRefByID: make(map[string]*worker.WorkerRef),
|
2018-07-05 23:32:01 +00:00
|
|
|
}, nil
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bridgeClient struct {
|
|
|
|
frontend.FrontendLLBBridge
|
2020-10-28 21:07:44 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
opts map[string]string
|
|
|
|
inputs map[string]*opspb.Definition
|
|
|
|
final map[*ref]struct{}
|
|
|
|
sid string
|
|
|
|
sm *session.Manager
|
|
|
|
refs []*ref
|
2020-11-14 05:58:20 +00:00
|
|
|
workers worker.Infos
|
2020-10-28 21:07:44 +00:00
|
|
|
workerRefByID map[string]*worker.WorkerRef
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 23:32:01 +00:00
|
|
|
func (c *bridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*client.Result, error) {
|
2018-07-11 23:51:41 +00:00
|
|
|
res, err := c.FrontendLLBBridge.Solve(ctx, frontend.SolveRequest{
|
2020-10-28 21:07:44 +00:00
|
|
|
Evaluate: req.Evaluate,
|
2020-02-07 19:53:18 +00:00
|
|
|
Definition: req.Definition,
|
|
|
|
Frontend: req.Frontend,
|
|
|
|
FrontendOpt: req.FrontendOpt,
|
|
|
|
FrontendInputs: req.FrontendInputs,
|
|
|
|
CacheImports: req.CacheImports,
|
2020-06-30 01:06:02 +00:00
|
|
|
}, c.sid)
|
2017-12-13 23:46:52 +00:00
|
|
|
if err != nil {
|
2020-10-28 21:07:44 +00:00
|
|
|
return nil, c.wrapSolveError(err)
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
cRes := &client.Result{}
|
2018-07-11 02:31:33 +00:00
|
|
|
c.mu.Lock()
|
2018-07-11 23:51:41 +00:00
|
|
|
for k, r := range res.Refs {
|
2020-10-28 21:07:44 +00:00
|
|
|
rr, err := c.newRef(r, session.NewGroup(c.sid))
|
2019-12-09 20:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
c.refs = append(c.refs, rr)
|
2018-07-11 23:51:41 +00:00
|
|
|
cRes.AddRef(k, rr)
|
|
|
|
}
|
|
|
|
if r := res.Ref; r != nil {
|
2020-10-28 21:07:44 +00:00
|
|
|
rr, err := c.newRef(r, session.NewGroup(c.sid))
|
2019-12-09 20:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
c.refs = append(c.refs, rr)
|
|
|
|
cRes.SetRef(rr)
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
2018-07-11 02:31:33 +00:00
|
|
|
c.mu.Unlock()
|
2018-07-11 23:51:41 +00:00
|
|
|
cRes.Metadata = res.Metadata
|
2018-07-05 23:32:01 +00:00
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
return cRes, nil
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
2018-07-05 21:15:14 +00:00
|
|
|
func (c *bridgeClient) BuildOpts() client.BuildOpts {
|
2020-11-14 05:58:20 +00:00
|
|
|
workers := make([]client.WorkerInfo, 0, len(c.workers.WorkerInfos()))
|
|
|
|
for _, w := range c.workers.WorkerInfos() {
|
2018-08-30 21:06:27 +00:00
|
|
|
workers = append(workers, client.WorkerInfo{
|
|
|
|
ID: w.ID,
|
|
|
|
Labels: w.Labels,
|
|
|
|
Platforms: w.Platforms,
|
|
|
|
})
|
2018-07-05 21:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.BuildOpts{
|
|
|
|
Opts: c.opts,
|
|
|
|
SessionID: c.sid,
|
|
|
|
Workers: workers,
|
|
|
|
Product: apicaps.ExportedProduct,
|
2018-09-21 22:19:10 +00:00
|
|
|
Caps: gwpb.Caps.CapSet(gwpb.Caps.All()),
|
|
|
|
LLBCaps: opspb.Caps.CapSet(opspb.Caps.All()),
|
2018-07-04 02:47:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-13 23:46:52 +00:00
|
|
|
|
2020-02-07 19:53:18 +00:00
|
|
|
func (c *bridgeClient) Inputs(ctx context.Context) (map[string]llb.State, error) {
|
2020-02-21 18:19:19 +00:00
|
|
|
inputs := make(map[string]llb.State)
|
|
|
|
for key, def := range c.inputs {
|
|
|
|
defop, err := llb.NewDefinitionOp(def)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inputs[key] = llb.NewState(defop)
|
|
|
|
}
|
|
|
|
return inputs, nil
|
2020-02-07 19:53:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:07:44 +00:00
|
|
|
func (c *bridgeClient) wrapSolveError(solveErr error) error {
|
|
|
|
var (
|
2020-11-14 06:04:26 +00:00
|
|
|
ee *llberrdefs.ExecError
|
|
|
|
fae *llberrdefs.FileActionError
|
|
|
|
sce *solver.SlowCacheError
|
|
|
|
inputIDs []string
|
|
|
|
mountIDs []string
|
|
|
|
subject errdefs.IsSolve_Subject
|
2020-10-28 21:07:44 +00:00
|
|
|
)
|
|
|
|
if errors.As(solveErr, &ee) {
|
|
|
|
var err error
|
|
|
|
inputIDs, err = c.registerResultIDs(ee.Inputs...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-14 07:20:41 +00:00
|
|
|
mountIDs, err = c.registerResultIDs(ee.Mounts...)
|
2020-10-28 21:07:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errors.As(solveErr, &fae) {
|
2020-10-28 21:15:32 +00:00
|
|
|
subject = fae.ToSubject()
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
|
|
|
if errors.As(solveErr, &sce) {
|
|
|
|
var err error
|
|
|
|
inputIDs, err = c.registerResultIDs(sce.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-28 21:15:32 +00:00
|
|
|
subject = sce.ToSubject()
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-11-14 06:04:26 +00:00
|
|
|
return errdefs.WithSolveError(solveErr, subject, inputIDs, mountIDs)
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bridgeClient) registerResultIDs(results ...solver.Result) (ids []string, err error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
ids = make([]string, len(results))
|
|
|
|
for i, res := range results {
|
2020-10-29 01:16:44 +00:00
|
|
|
if res == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-28 21:07:44 +00:00
|
|
|
workerRef, ok := res.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return ids, errors.Errorf("unexpected type for result, got %T", res.Sys())
|
|
|
|
}
|
|
|
|
ids[i] = workerRef.ID()
|
|
|
|
c.workerRefByID[workerRef.ID()] = workerRef
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
func (c *bridgeClient) toFrontendResult(r *client.Result) (*frontend.Result, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &frontend.Result{}
|
|
|
|
|
|
|
|
if r.Refs != nil {
|
2020-02-04 23:00:44 +00:00
|
|
|
res.Refs = make(map[string]solver.ResultProxy, len(r.Refs))
|
2018-07-11 23:51:41 +00:00
|
|
|
for k, r := range r.Refs {
|
|
|
|
rr, ok := r.(*ref)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid reference type for forward %T", r)
|
|
|
|
}
|
|
|
|
c.final[rr] = struct{}{}
|
2020-02-04 23:00:44 +00:00
|
|
|
res.Refs[k] = rr.ResultProxy
|
2018-07-11 23:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if r := r.Ref; r != nil {
|
2018-07-05 23:32:01 +00:00
|
|
|
rr, ok := r.(*ref)
|
|
|
|
if !ok {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Errorf("invalid reference type for forward %T", r)
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
|
|
|
c.final[rr] = struct{}{}
|
2020-02-04 23:00:44 +00:00
|
|
|
res.Ref = rr.ResultProxy
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
res.Metadata = r.Metadata
|
|
|
|
|
|
|
|
return res, nil
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bridgeClient) discard(err error) {
|
2020-10-28 21:07:44 +00:00
|
|
|
for id, workerRef := range c.workerRefByID {
|
|
|
|
workerRef.ImmutableRef.Release(context.TODO())
|
|
|
|
delete(c.workerRefByID, id)
|
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
for _, r := range c.refs {
|
|
|
|
if r != nil {
|
|
|
|
if _, ok := c.final[r]; !ok || err != nil {
|
|
|
|
r.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:20:47 +00:00
|
|
|
func (c *bridgeClient) Warn(ctx context.Context, dgst digest.Digest, msg string, opts client.WarnOpts) error {
|
|
|
|
return c.FrontendLLBBridge.Warn(ctx, dgst, msg, opts)
|
2021-11-24 07:56:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:11:32 +00:00
|
|
|
func (c *bridgeClient) NewContainer(ctx context.Context, req client.NewContainerRequest) (client.Container, error) {
|
|
|
|
ctrReq := gateway.NewContainerRequest{
|
2020-10-02 05:36:33 +00:00
|
|
|
ContainerID: identity.NewID(),
|
|
|
|
NetMode: req.NetMode,
|
2020-11-14 06:14:05 +00:00
|
|
|
Mounts: make([]gateway.Mount, len(req.Mounts)),
|
2020-08-05 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 06:14:05 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2020-10-28 21:07:44 +00:00
|
|
|
|
2020-11-14 06:14:05 +00:00
|
|
|
for i, m := range req.Mounts {
|
|
|
|
i, m := i, m
|
|
|
|
eg.Go(func() error {
|
|
|
|
var workerRef *worker.WorkerRef
|
|
|
|
if m.Ref != nil {
|
|
|
|
refProxy, ok := m.Ref.(*ref)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("unexpected Ref type: %T", m.Ref)
|
|
|
|
}
|
2020-10-28 21:07:44 +00:00
|
|
|
|
2020-11-14 06:14:05 +00:00
|
|
|
res, err := refProxy.Result(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
workerRef, ok = res.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("invalid ref: %T", res.Sys())
|
|
|
|
}
|
|
|
|
} else if m.ResultID != "" {
|
|
|
|
var ok bool
|
|
|
|
workerRef, ok = c.workerRefByID[m.ResultID]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("failed to find ref %s for %q mount", m.ResultID, m.Dest)
|
|
|
|
}
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-11-14 06:14:05 +00:00
|
|
|
ctrReq.Mounts[i] = gateway.Mount{
|
|
|
|
WorkerRef: workerRef,
|
|
|
|
Mount: &opspb.Mount{
|
|
|
|
Dest: m.Dest,
|
|
|
|
Selector: m.Selector,
|
|
|
|
Readonly: m.Readonly,
|
|
|
|
MountType: m.MountType,
|
|
|
|
CacheOpt: m.CacheOpt,
|
|
|
|
SecretOpt: m.SecretOpt,
|
|
|
|
SSHOpt: m.SSHOpt,
|
|
|
|
},
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-11-14 06:14:05 +00:00
|
|
|
return nil
|
2020-08-05 01:11:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-14 06:14:05 +00:00
|
|
|
err := eg.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-03 05:38:11 +00:00
|
|
|
ctrReq.ExtraHosts, err = gateway.ParseExtraHosts(req.ExtraHosts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-14 05:58:20 +00:00
|
|
|
w, err := c.workers.GetDefault()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-15 06:40:05 +00:00
|
|
|
group := session.NewGroup(c.sid)
|
2020-11-14 05:58:20 +00:00
|
|
|
ctr, err := gateway.NewContainer(ctx, w, c.sm, group, ctrReq)
|
2020-08-05 01:11:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ctr, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:46:52 +00:00
|
|
|
type ref struct {
|
2020-02-04 23:00:44 +00:00
|
|
|
solver.ResultProxy
|
2020-10-27 06:13:39 +00:00
|
|
|
session session.Group
|
2020-10-28 21:07:44 +00:00
|
|
|
c *bridgeClient
|
2019-12-09 20:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:07:44 +00:00
|
|
|
func (c *bridgeClient) newRef(r solver.ResultProxy, s session.Group) (*ref, error) {
|
|
|
|
return &ref{ResultProxy: r, session: s, c: c}, nil
|
2020-01-23 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ref) ToState() (st llb.State, err error) {
|
2020-02-04 23:00:44 +00:00
|
|
|
defop, err := llb.NewDefinitionOp(r.Definition())
|
2019-12-09 20:50:21 +00:00
|
|
|
if err != nil {
|
2020-01-23 19:30:45 +00:00
|
|
|
return st, err
|
2019-12-09 20:50:21 +00:00
|
|
|
}
|
2020-01-23 19:30:45 +00:00
|
|
|
return llb.NewState(defop), nil
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 21:15:13 +00:00
|
|
|
func (r *ref) ReadFile(ctx context.Context, req client.ReadRequest) ([]byte, error) {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := r.getMountable(ctx)
|
2018-04-13 21:13:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-02 11:17:57 +00:00
|
|
|
newReq := cacheutil.ReadRequest{
|
2018-05-29 21:15:13 +00:00
|
|
|
Filename: req.Filename,
|
|
|
|
}
|
|
|
|
if r := req.Range; r != nil {
|
2018-10-02 11:17:57 +00:00
|
|
|
newReq.Range = &cacheutil.FileRange{
|
2018-05-29 21:15:13 +00:00
|
|
|
Offset: r.Offset,
|
|
|
|
Length: r.Length,
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
return cacheutil.ReadFile(ctx, m, newReq)
|
2018-04-13 21:13:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 12:48:30 +00:00
|
|
|
func (r *ref) ReadDir(ctx context.Context, req client.ReadDirRequest) ([]*fstypes.Stat, error) {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := r.getMountable(ctx)
|
2018-09-21 12:48:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-02 11:17:57 +00:00
|
|
|
newReq := cacheutil.ReadDirRequest{
|
2018-09-21 12:48:30 +00:00
|
|
|
Path: req.Path,
|
|
|
|
IncludePattern: req.IncludePattern,
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
return cacheutil.ReadDir(ctx, m, newReq)
|
2018-09-21 12:48:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 13:45:39 +00:00
|
|
|
func (r *ref) StatFile(ctx context.Context, req client.StatRequest) (*fstypes.Stat, error) {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := r.getMountable(ctx)
|
2018-10-02 13:45:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
return cacheutil.StatFile(ctx, m, req.Path)
|
2018-10-02 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (r *ref) getMountable(ctx context.Context) (snapshot.Mountable, error) {
|
2020-02-04 23:00:44 +00:00
|
|
|
rr, err := r.ResultProxy.Result(ctx)
|
|
|
|
if err != nil {
|
2020-10-28 21:07:44 +00:00
|
|
|
return nil, r.c.wrapSolveError(err)
|
2020-02-04 23:00:44 +00:00
|
|
|
}
|
|
|
|
ref, ok := rr.Sys().(*worker.WorkerRef)
|
2018-04-13 21:13:48 +00:00
|
|
|
if !ok {
|
2020-02-04 23:00:44 +00:00
|
|
|
return nil, errors.Errorf("invalid ref: %T", rr.Sys())
|
2017-12-14 01:29:03 +00:00
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
return ref.ImmutableRef.Mount(ctx, true, r.session)
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|