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
|
|
|
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-07-04 02:47:40 +00:00
|
|
|
clienttypes "github.com/moby/buildkit/client"
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/frontend/gateway/client"
|
2018-09-21 22:19:10 +00:00
|
|
|
gwpb "github.com/moby/buildkit/frontend/gateway/pb"
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
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"
|
2017-12-14 01:29:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-12-13 23:46:52 +00:00
|
|
|
)
|
|
|
|
|
2018-07-04 02:47:40 +00:00
|
|
|
func llbBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string, workerInfos []clienttypes.WorkerInfo) (*bridgeClient, error) {
|
2018-07-05 23:32:01 +00:00
|
|
|
return &bridgeClient{
|
|
|
|
opts: opts,
|
|
|
|
FrontendLLBBridge: llbBridge,
|
|
|
|
sid: session.FromContext(ctx),
|
|
|
|
workerInfos: workerInfos,
|
|
|
|
final: map[*ref]struct{}{},
|
|
|
|
}, nil
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bridgeClient struct {
|
|
|
|
frontend.FrontendLLBBridge
|
2018-07-11 02:31:33 +00:00
|
|
|
mu sync.Mutex
|
2017-12-13 23:46:52 +00:00
|
|
|
opts map[string]string
|
2018-07-05 23:32:01 +00:00
|
|
|
final map[*ref]struct{}
|
2017-12-13 23:46:52 +00:00
|
|
|
sid string
|
|
|
|
exporterAttr map[string][]byte
|
|
|
|
refs []*ref
|
2018-07-04 02:47:40 +00:00
|
|
|
workerInfos []clienttypes.WorkerInfo
|
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{
|
2018-04-27 22:47:32 +00:00
|
|
|
Definition: req.Definition,
|
|
|
|
Frontend: req.Frontend,
|
|
|
|
FrontendOpt: req.FrontendOpt,
|
|
|
|
ImportCacheRefs: req.ImportCacheRefs,
|
2017-12-13 23:46:52 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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 {
|
2018-07-05 23:32:01 +00:00
|
|
|
rr := &ref{r}
|
|
|
|
c.refs = append(c.refs, rr)
|
2018-07-11 23:51:41 +00:00
|
|
|
cRes.AddRef(k, rr)
|
|
|
|
}
|
|
|
|
if r := res.Ref; r != nil {
|
|
|
|
rr := &ref{r}
|
|
|
|
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 {
|
|
|
|
workers := make([]client.WorkerInfo, 0, len(c.workerInfos))
|
2018-07-04 02:47:40 +00:00
|
|
|
for _, w := range c.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
|
|
|
|
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 {
|
|
|
|
res.Refs = make(map[string]solver.CachedResult, len(r.Refs))
|
|
|
|
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{}{}
|
|
|
|
res.Refs[k] = rr.CachedResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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{}{}
|
2018-07-11 23:51:41 +00:00
|
|
|
res.Ref = rr.CachedResult
|
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) {
|
|
|
|
for _, r := range c.refs {
|
|
|
|
if r != nil {
|
|
|
|
if _, ok := c.final[r]; !ok || err != nil {
|
|
|
|
r.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:46:52 +00:00
|
|
|
type ref struct {
|
2018-04-13 21:13:48 +00:00
|
|
|
solver.CachedResult
|
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) {
|
2018-04-13 21:13:48 +00:00
|
|
|
ref, err := r.getImmutableRef()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-29 21:15:13 +00:00
|
|
|
newReq := cache.ReadRequest{
|
|
|
|
Filename: req.Filename,
|
|
|
|
}
|
|
|
|
if r := req.Range; r != nil {
|
|
|
|
newReq.Range = &cache.FileRange{
|
|
|
|
Offset: r.Offset,
|
|
|
|
Length: r.Length,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cache.ReadFile(ctx, ref, newReq)
|
2018-04-13 21:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ref) getImmutableRef() (cache.ImmutableRef, error) {
|
|
|
|
ref, ok := r.CachedResult.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid ref: %T", r.CachedResult.Sys())
|
2017-12-14 01:29:03 +00:00
|
|
|
}
|
2018-04-13 21:13:48 +00:00
|
|
|
return ref.ImmutableRef, nil
|
2017-12-13 23:46:52 +00:00
|
|
|
}
|