2017-10-01 00:58:07 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-10-01 00:58:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
2017-10-03 06:33:25 +00:00
|
|
|
"strings"
|
2018-06-20 18:17:05 +00:00
|
|
|
"sync"
|
2017-10-01 00:58:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
2018-06-30 01:35:39 +00:00
|
|
|
apitypes "github.com/moby/buildkit/api/types"
|
2017-10-01 00:58:07 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-07-26 20:51:28 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2017-10-01 00:58:07 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/executor"
|
2018-07-13 01:02:36 +00:00
|
|
|
"github.com/moby/buildkit/exporter/containerimage/exptypes"
|
2017-10-01 00:58:07 +00:00
|
|
|
"github.com/moby/buildkit/frontend"
|
2018-07-25 06:18:21 +00:00
|
|
|
gw "github.com/moby/buildkit/frontend/gateway/client"
|
2017-10-01 00:58:07 +00:00
|
|
|
pb "github.com/moby/buildkit/frontend/gateway/pb"
|
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
|
"github.com/moby/buildkit/session"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2018-06-30 01:35:39 +00:00
|
|
|
opspb "github.com/moby/buildkit/solver/pb"
|
2018-07-09 22:02:52 +00:00
|
|
|
"github.com/moby/buildkit/util/apicaps"
|
2018-01-07 01:17:45 +00:00
|
|
|
"github.com/moby/buildkit/util/tracing"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
2018-06-24 05:52:19 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-10-01 00:58:07 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/http2"
|
2018-07-11 23:51:41 +00:00
|
|
|
spb "google.golang.org/genproto/googleapis/rpc/status"
|
2017-10-01 00:58:07 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
2018-07-11 23:51:41 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2017-10-01 00:58:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-07-13 01:02:36 +00:00
|
|
|
keySource = "source"
|
|
|
|
keyDevel = "gateway-devel"
|
2017-10-01 00:58:07 +00:00
|
|
|
)
|
|
|
|
|
2018-06-30 01:35:39 +00:00
|
|
|
func NewGatewayFrontend(w frontend.WorkerInfos) frontend.Frontend {
|
|
|
|
return &gatewayFrontend{
|
|
|
|
workers: w,
|
|
|
|
}
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type gatewayFrontend struct {
|
2018-06-30 01:35:39 +00:00
|
|
|
workers frontend.WorkerInfos
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 06:33:25 +00:00
|
|
|
func filterPrefix(opts map[string]string, pfx string) map[string]string {
|
|
|
|
m := map[string]string{}
|
|
|
|
for k, v := range opts {
|
|
|
|
if strings.HasPrefix(k, pfx) {
|
|
|
|
m[strings.TrimPrefix(k, pfx)] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
func (gf *gatewayFrontend) Solve(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string) (ret *frontend.Result, retErr error) {
|
2017-10-01 00:58:07 +00:00
|
|
|
source, ok := opts[keySource]
|
|
|
|
if !ok {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Errorf("no source specified for gateway")
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sid := session.FromContext(ctx)
|
|
|
|
|
|
|
|
_, isDevel := opts[keyDevel]
|
2018-06-24 05:52:19 +00:00
|
|
|
var img specs.Image
|
2017-10-01 00:58:07 +00:00
|
|
|
var rootFS cache.ImmutableRef
|
2018-03-26 13:51:08 +00:00
|
|
|
var readonly bool // TODO: try to switch to read-only by default.
|
2017-10-01 00:58:07 +00:00
|
|
|
|
|
|
|
if isDevel {
|
2018-07-11 23:51:41 +00:00
|
|
|
devRes, err := llbBridge.Solve(session.NewContext(ctx, "gateway:"+sid),
|
2017-10-05 05:31:18 +00:00
|
|
|
frontend.SolveRequest{
|
|
|
|
Frontend: source,
|
|
|
|
FrontendOpt: filterPrefix(opts, "gateway-"),
|
|
|
|
})
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
defer func() {
|
2018-07-11 23:51:41 +00:00
|
|
|
devRes.EachRef(func(ref solver.CachedResult) error {
|
|
|
|
return ref.Release(context.TODO())
|
|
|
|
})
|
2018-07-05 23:32:01 +00:00
|
|
|
}()
|
2018-07-11 23:51:41 +00:00
|
|
|
if devRes.Ref == nil {
|
|
|
|
return nil, errors.Errorf("development gateway didn't return default result")
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
workerRef, ok := devRes.Ref.Sys().(*worker.WorkerRef)
|
2018-04-13 21:13:48 +00:00
|
|
|
if !ok {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Errorf("invalid ref: %T", devRes.Ref.Sys())
|
2018-04-13 21:13:48 +00:00
|
|
|
}
|
|
|
|
rootFS = workerRef.ImmutableRef
|
2018-07-13 01:02:36 +00:00
|
|
|
config, ok := devRes.Metadata[exptypes.ExporterImageConfigKey]
|
2017-10-01 00:58:07 +00:00
|
|
|
if ok {
|
2017-10-03 06:33:25 +00:00
|
|
|
if err := json.Unmarshal(config, &img); err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-03 06:33:25 +00:00
|
|
|
}
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sourceRef, err := reference.ParseNormalizedNamed(source)
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 06:18:21 +00:00
|
|
|
dgst, config, err := llbBridge.ResolveImageConfig(ctx, reference.TagNameOnly(sourceRef).String(), gw.ResolveImageConfigOpt{})
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(config, &img); err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:09:32 +00:00
|
|
|
if dgst != "" {
|
|
|
|
sourceRef, err = reference.WithDigest(sourceRef, dgst)
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2018-06-20 18:09:32 +00:00
|
|
|
}
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 20:51:28 +00:00
|
|
|
src := llb.Image(sourceRef.String(), &markTypeFrontend{})
|
2017-10-01 00:58:07 +00:00
|
|
|
|
|
|
|
def, err := src.Marshal()
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
res, err := llbBridge.Solve(ctx, frontend.SolveRequest{
|
2017-10-05 05:31:18 +00:00
|
|
|
Definition: def.ToPB(),
|
|
|
|
})
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
defer func() {
|
2018-07-11 23:51:41 +00:00
|
|
|
res.EachRef(func(ref solver.CachedResult) error {
|
|
|
|
return ref.Release(context.TODO())
|
|
|
|
})
|
2018-07-05 23:32:01 +00:00
|
|
|
}()
|
2018-07-11 23:51:41 +00:00
|
|
|
if res.Ref == nil {
|
|
|
|
return nil, errors.Errorf("gateway source didn't return default result")
|
|
|
|
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
workerRef, ok := res.Ref.Sys().(*worker.WorkerRef)
|
2018-04-13 21:13:48 +00:00
|
|
|
if !ok {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Errorf("invalid ref: %T", res.Ref.Sys())
|
2018-04-13 21:13:48 +00:00
|
|
|
}
|
|
|
|
rootFS = workerRef.ImmutableRef
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:35:39 +00:00
|
|
|
lbf, err := newLLBBridgeForwarder(ctx, llbBridge, gf.workers)
|
2017-10-01 00:58:07 +00:00
|
|
|
defer lbf.conn.Close()
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"/run"}
|
|
|
|
env := []string{}
|
|
|
|
cwd := "/"
|
|
|
|
if img.Config.Env != nil {
|
|
|
|
env = img.Config.Env
|
|
|
|
}
|
|
|
|
if img.Config.Entrypoint != nil {
|
|
|
|
args = img.Config.Entrypoint
|
|
|
|
}
|
|
|
|
if img.Config.WorkingDir != "" {
|
|
|
|
cwd = img.Config.WorkingDir
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for k, v := range opts {
|
|
|
|
env = append(env, fmt.Sprintf("BUILDKIT_FRONTEND_OPT_%d", i)+"="+k+"="+v)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
env = append(env, "BUILDKIT_SESSION_ID="+sid)
|
|
|
|
|
2018-07-04 02:47:40 +00:00
|
|
|
dt, err := json.Marshal(gf.workers.WorkerInfos())
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to marshal workers array")
|
2018-07-04 02:47:40 +00:00
|
|
|
}
|
|
|
|
env = append(env, "BUILDKIT_WORKERS="+string(dt))
|
|
|
|
|
2017-12-13 23:47:05 +00:00
|
|
|
defer func() {
|
|
|
|
for _, r := range lbf.refs {
|
2018-07-11 23:51:41 +00:00
|
|
|
if retErr == nil && lbf.result != nil {
|
|
|
|
keep := false
|
|
|
|
lbf.result.EachRef(func(r2 solver.CachedResult) error {
|
|
|
|
if r == r2 {
|
|
|
|
keep = true
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if keep {
|
|
|
|
continue
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
r.Release(context.TODO())
|
2017-12-13 23:47:05 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-07-12 23:04:58 +00:00
|
|
|
env = append(env, "BUILDKIT_EXPORTEDPRODUCT="+apicaps.ExportedProduct)
|
2018-07-09 22:02:52 +00:00
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
err = llbBridge.Exec(ctx, executor.Meta{
|
2018-03-26 13:51:08 +00:00
|
|
|
Env: env,
|
|
|
|
Args: args,
|
|
|
|
Cwd: cwd,
|
|
|
|
ReadonlyRootFS: readonly,
|
2017-10-01 00:58:07 +00:00
|
|
|
}, rootFS, lbf.Stdin, lbf.Stdout, os.Stderr)
|
|
|
|
|
2018-07-30 13:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
// An existing error (set via Return rpc) takes
|
|
|
|
// precedence over this error, which in turn takes
|
|
|
|
// precedence over a success reported via Return.
|
|
|
|
lbf.mu.Lock()
|
|
|
|
if lbf.err == nil {
|
|
|
|
lbf.result = nil
|
|
|
|
lbf.err = err
|
|
|
|
}
|
|
|
|
lbf.mu.Unlock()
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 13:55:26 +00:00
|
|
|
return lbf.Result()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lbf *llbBridgeForwarder) setResult(r *frontend.Result, err error) (*pb.ReturnResponse, error) {
|
|
|
|
lbf.mu.Lock()
|
|
|
|
defer lbf.mu.Unlock()
|
|
|
|
|
|
|
|
if (r == nil) == (err == nil) {
|
|
|
|
return nil, errors.New("gateway return must be either result or err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lbf.result != nil || lbf.err != nil {
|
|
|
|
return nil, errors.New("gateway result is already set")
|
|
|
|
}
|
|
|
|
|
|
|
|
lbf.result = r
|
|
|
|
lbf.err = err
|
|
|
|
return &pb.ReturnResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lbf *llbBridgeForwarder) Result() (*frontend.Result, error) {
|
|
|
|
lbf.mu.Lock()
|
|
|
|
defer lbf.mu.Unlock()
|
|
|
|
|
|
|
|
if lbf.result == nil && lbf.err == nil {
|
|
|
|
return nil, errors.New("no result for incomplete build")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lbf.err != nil {
|
|
|
|
return nil, lbf.err
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
return lbf.result, nil
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:35:39 +00:00
|
|
|
func newLLBBridgeForwarder(ctx context.Context, llbBridge frontend.FrontendLLBBridge, workers frontend.WorkerInfos) (*llbBridgeForwarder, error) {
|
2018-03-08 18:08:32 +00:00
|
|
|
lbf := &llbBridgeForwarder{
|
2018-01-07 01:17:45 +00:00
|
|
|
callCtx: ctx,
|
2017-10-01 00:58:07 +00:00
|
|
|
llbBridge: llbBridge,
|
2018-07-05 23:32:01 +00:00
|
|
|
refs: map[string]solver.CachedResult{},
|
2017-10-01 00:58:07 +00:00
|
|
|
pipe: newPipe(),
|
2018-06-30 01:35:39 +00:00
|
|
|
workers: workers,
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server := grpc.NewServer()
|
|
|
|
grpc_health_v1.RegisterHealthServer(server, health.NewServer())
|
|
|
|
pb.RegisterLLBBridgeServer(server, lbf)
|
|
|
|
|
|
|
|
go serve(ctx, server, lbf.conn)
|
|
|
|
|
|
|
|
return lbf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipe struct {
|
|
|
|
Stdin io.ReadCloser
|
|
|
|
Stdout io.WriteCloser
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPipe() *pipe {
|
|
|
|
pr1, pw1, _ := os.Pipe()
|
|
|
|
pr2, pw2, _ := os.Pipe()
|
|
|
|
return &pipe{
|
|
|
|
Stdin: pr1,
|
|
|
|
Stdout: pw2,
|
|
|
|
conn: &conn{
|
|
|
|
Reader: pr2,
|
|
|
|
Writer: pw1,
|
|
|
|
Closer: pw2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type conn struct {
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *conn) LocalAddr() net.Addr {
|
|
|
|
return dummyAddr{}
|
|
|
|
}
|
|
|
|
func (s *conn) RemoteAddr() net.Addr {
|
|
|
|
return dummyAddr{}
|
|
|
|
}
|
|
|
|
func (s *conn) SetDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyAddr struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyAddr) Network() string {
|
|
|
|
return "pipe"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyAddr) String() string {
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:08:32 +00:00
|
|
|
type llbBridgeForwarder struct {
|
2018-07-11 23:51:41 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
callCtx context.Context
|
|
|
|
llbBridge frontend.FrontendLLBBridge
|
|
|
|
refs map[string]solver.CachedResult
|
|
|
|
// lastRef solver.CachedResult
|
|
|
|
// lastRefs map[string]solver.CachedResult
|
|
|
|
// err error
|
|
|
|
result *frontend.Result
|
2018-07-09 21:26:31 +00:00
|
|
|
err error
|
2017-10-03 06:33:25 +00:00
|
|
|
exporterAttr map[string][]byte
|
2018-06-30 01:35:39 +00:00
|
|
|
workers frontend.WorkerInfos
|
2017-10-01 00:58:07 +00:00
|
|
|
*pipe
|
|
|
|
}
|
|
|
|
|
2018-05-01 01:10:54 +00:00
|
|
|
func (lbf *llbBridgeForwarder) ResolveImageConfig(ctx context.Context, req *pb.ResolveImageConfigRequest) (*pb.ResolveImageConfigResponse, error) {
|
2018-01-07 01:17:45 +00:00
|
|
|
ctx = tracing.ContextWithSpanFromContext(ctx, lbf.callCtx)
|
2018-06-24 05:52:19 +00:00
|
|
|
var platform *specs.Platform
|
|
|
|
if p := req.Platform; p != nil {
|
|
|
|
platform = &specs.Platform{
|
|
|
|
OS: p.OS,
|
|
|
|
Architecture: p.Architecture,
|
|
|
|
Variant: p.Variant,
|
|
|
|
OSVersion: p.OSVersion,
|
|
|
|
OSFeatures: p.OSFeatures,
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 06:18:21 +00:00
|
|
|
dgst, dt, err := lbf.llbBridge.ResolveImageConfig(ctx, req.Ref, gw.ResolveImageConfigOpt{
|
|
|
|
Platform: platform,
|
|
|
|
ResolveMode: req.ResolveMode,
|
2018-07-28 00:27:41 +00:00
|
|
|
LogName: req.LogName,
|
2018-07-25 06:18:21 +00:00
|
|
|
})
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pb.ResolveImageConfigResponse{
|
|
|
|
Digest: dgst,
|
|
|
|
Config: dt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-01 01:10:54 +00:00
|
|
|
func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest) (*pb.SolveResponse, error) {
|
2018-01-07 01:17:45 +00:00
|
|
|
ctx = tracing.ContextWithSpanFromContext(ctx, lbf.callCtx)
|
2018-07-11 23:51:41 +00:00
|
|
|
res, err := lbf.llbBridge.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-10-05 05:31:18 +00:00
|
|
|
})
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
if len(res.Refs) > 0 && !req.AllowResultReturn {
|
|
|
|
// this should never happen because old client shouldn't make a map request
|
|
|
|
return nil, errors.Errorf("solve did not return default result")
|
|
|
|
}
|
|
|
|
|
|
|
|
pbRes := &pb.Result{}
|
|
|
|
var defaultID string
|
2017-10-01 00:58:07 +00:00
|
|
|
|
2018-07-05 23:32:01 +00:00
|
|
|
lbf.mu.Lock()
|
2018-07-11 23:51:41 +00:00
|
|
|
if res.Refs != nil {
|
|
|
|
ids := make(map[string]string, len(res.Refs))
|
|
|
|
for k, ref := range res.Refs {
|
|
|
|
id := identity.NewID()
|
|
|
|
if ref == nil {
|
|
|
|
id = ""
|
|
|
|
} else {
|
|
|
|
lbf.refs[id] = ref
|
|
|
|
}
|
|
|
|
ids[k] = id
|
|
|
|
}
|
|
|
|
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: ids}}
|
|
|
|
} else {
|
2018-07-05 23:32:01 +00:00
|
|
|
id := identity.NewID()
|
2018-07-11 23:51:41 +00:00
|
|
|
if res.Ref == nil {
|
2018-07-05 23:32:01 +00:00
|
|
|
id = ""
|
2018-07-11 23:51:41 +00:00
|
|
|
} else {
|
|
|
|
lbf.refs[id] = res.Ref
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
defaultID = id
|
|
|
|
pbRes.Result = &pb.Result_Ref{Ref: id}
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-06-20 18:17:05 +00:00
|
|
|
lbf.mu.Unlock()
|
2018-07-05 23:32:01 +00:00
|
|
|
|
|
|
|
// compatibility mode for older clients
|
2017-10-01 00:58:07 +00:00
|
|
|
if req.Final {
|
2018-07-11 23:51:41 +00:00
|
|
|
exp := map[string][]byte{}
|
2018-07-05 23:32:01 +00:00
|
|
|
if err := json.Unmarshal(req.ExporterAttr, &exp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
for k, v := range res.Metadata {
|
|
|
|
exp[k] = v
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
lbf.result = &frontend.Result{
|
|
|
|
Ref: lbf.refs[defaultID],
|
|
|
|
Metadata: exp,
|
|
|
|
}
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
resp := &pb.SolveResponse{
|
|
|
|
Result: pbRes,
|
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
if !req.AllowResultReturn {
|
|
|
|
resp.Ref = defaultID
|
2017-12-14 01:29:03 +00:00
|
|
|
}
|
2018-07-05 23:32:01 +00:00
|
|
|
|
|
|
|
return resp, nil
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
2018-05-01 01:10:54 +00:00
|
|
|
func (lbf *llbBridgeForwarder) ReadFile(ctx context.Context, req *pb.ReadFileRequest) (*pb.ReadFileResponse, error) {
|
2018-01-07 01:17:45 +00:00
|
|
|
ctx = tracing.ContextWithSpanFromContext(ctx, lbf.callCtx)
|
2018-06-20 18:17:05 +00:00
|
|
|
lbf.mu.Lock()
|
2017-10-01 00:58:07 +00:00
|
|
|
ref, ok := lbf.refs[req.Ref]
|
2018-06-20 18:17:05 +00:00
|
|
|
lbf.mu.Unlock()
|
2017-10-01 00:58:07 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("no such ref: %v", req.Ref)
|
|
|
|
}
|
2017-12-14 01:29:03 +00:00
|
|
|
if ref == nil {
|
|
|
|
return nil, errors.Wrapf(os.ErrNotExist, "%s no found", req.FilePath)
|
|
|
|
}
|
2018-04-13 21:13:48 +00:00
|
|
|
workerRef, ok := ref.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid ref: %T", ref.Sys())
|
|
|
|
}
|
2018-05-29 21:15:13 +00:00
|
|
|
|
|
|
|
newReq := cache.ReadRequest{
|
|
|
|
Filename: req.FilePath,
|
|
|
|
}
|
|
|
|
if r := req.Range; r != nil {
|
|
|
|
newReq.Range = &cache.FileRange{
|
|
|
|
Offset: int(r.Offset),
|
|
|
|
Length: int(r.Length),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := cache.ReadFile(ctx, workerRef.ImmutableRef, newReq)
|
2017-10-01 00:58:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ReadFileResponse{Data: dt}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-01 01:10:54 +00:00
|
|
|
func (lbf *llbBridgeForwarder) Ping(context.Context, *pb.PingRequest) (*pb.PongResponse, error) {
|
2018-06-30 01:35:39 +00:00
|
|
|
|
|
|
|
workers := lbf.workers.WorkerInfos()
|
|
|
|
pbWorkers := make([]*apitypes.WorkerRecord, 0, len(workers))
|
|
|
|
for _, w := range workers {
|
|
|
|
pbWorkers = append(pbWorkers, &apitypes.WorkerRecord{
|
|
|
|
ID: w.ID,
|
|
|
|
Labels: w.Labels,
|
|
|
|
Platforms: opspb.PlatformsFromSpec(w.Platforms),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-25 23:49:06 +00:00
|
|
|
return &pb.PongResponse{
|
|
|
|
FrontendAPICaps: pb.Caps.All(),
|
2018-06-30 01:35:39 +00:00
|
|
|
Workers: pbWorkers,
|
2018-07-18 22:32:37 +00:00
|
|
|
LLBCaps: opspb.Caps.All(),
|
2018-06-25 23:49:06 +00:00
|
|
|
}, nil
|
2017-10-01 00:58:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:15:14 +00:00
|
|
|
func (lbf *llbBridgeForwarder) Return(ctx context.Context, in *pb.ReturnRequest) (*pb.ReturnResponse, error) {
|
2018-07-09 21:26:31 +00:00
|
|
|
if in.Error != nil {
|
2018-07-30 13:55:26 +00:00
|
|
|
return lbf.setResult(nil, status.ErrorProto(&spb.Status{
|
2018-07-11 23:51:41 +00:00
|
|
|
Code: in.Error.Code,
|
|
|
|
Message: in.Error.Message,
|
|
|
|
// Details: in.Error.Details,
|
2018-07-30 13:55:26 +00:00
|
|
|
}))
|
2018-07-09 21:26:31 +00:00
|
|
|
} else {
|
2018-07-30 13:55:26 +00:00
|
|
|
r := &frontend.Result{
|
2018-07-11 23:51:41 +00:00
|
|
|
Metadata: in.Result.Metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch res := in.Result.Result.(type) {
|
|
|
|
case *pb.Result_Ref:
|
|
|
|
ref, err := lbf.convertRef(res.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-30 13:55:26 +00:00
|
|
|
r.Ref = ref
|
2018-07-11 23:51:41 +00:00
|
|
|
case *pb.Result_Refs:
|
|
|
|
m := map[string]solver.CachedResult{}
|
|
|
|
for k, v := range res.Refs.Refs {
|
|
|
|
ref, err := lbf.convertRef(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m[k] = ref
|
|
|
|
}
|
2018-07-30 13:55:26 +00:00
|
|
|
r.Refs = m
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
2018-07-30 13:55:26 +00:00
|
|
|
return lbf.setResult(r, nil)
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
func (lbf *llbBridgeForwarder) convertRef(id string) (solver.CachedResult, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
r, ok := lbf.refs[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("return reference %s not found", id)
|
2018-07-09 21:26:31 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
return r, nil
|
2018-07-05 21:15:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 00:58:07 +00:00
|
|
|
func serve(ctx context.Context, grpcServer *grpc.Server, conn net.Conn) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
logrus.Debugf("serving grpc connection")
|
|
|
|
(&http2.Server{}).ServeConn(conn, &http2.ServeConnOpts{Handler: grpcServer})
|
|
|
|
}
|
2018-07-26 20:51:28 +00:00
|
|
|
|
|
|
|
type markTypeFrontend struct{}
|
|
|
|
|
|
|
|
func (*markTypeFrontend) SetImageOption(ii *llb.ImageInfo) {
|
|
|
|
ii.RecordType = string(client.UsageRecordTypeFrontend)
|
|
|
|
}
|