2017-12-13 23:46:52 +00:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
2017-12-14 01:29:03 +00:00
|
|
|
"os"
|
|
|
|
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/frontend/gateway/client"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-12-14 01:29:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-12-13 23:46:52 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func llbBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string) (*bridgeClient, error) {
|
|
|
|
return &bridgeClient{opts: opts, FrontendLLBBridge: llbBridge, sid: session.FromContext(ctx)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type bridgeClient struct {
|
|
|
|
frontend.FrontendLLBBridge
|
|
|
|
opts map[string]string
|
2017-12-14 01:29:03 +00:00
|
|
|
final *ref
|
2017-12-13 23:46:52 +00:00
|
|
|
sid string
|
|
|
|
exporterAttr map[string][]byte
|
|
|
|
refs []*ref
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bridgeClient) Solve(ctx context.Context, def *pb.Definition, f string, exporterAttr map[string][]byte, final bool) (client.Reference, error) {
|
|
|
|
r, exporterAttrRes, err := c.FrontendLLBBridge.Solve(ctx, frontend.SolveRequest{
|
|
|
|
Definition: def,
|
|
|
|
Frontend: f,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rr := &ref{r}
|
|
|
|
c.refs = append(c.refs, rr)
|
|
|
|
if final {
|
|
|
|
c.final = rr
|
|
|
|
if exporterAttr == nil {
|
|
|
|
exporterAttr = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
for k, v := range exporterAttrRes {
|
|
|
|
exporterAttr[k] = v
|
|
|
|
}
|
|
|
|
c.exporterAttr = exporterAttr
|
|
|
|
}
|
|
|
|
return rr, nil
|
|
|
|
}
|
|
|
|
func (c *bridgeClient) Opts() map[string]string {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
func (c *bridgeClient) SessionID() string {
|
|
|
|
return c.sid
|
|
|
|
}
|
|
|
|
|
|
|
|
type ref struct {
|
|
|
|
cache.ImmutableRef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ref) ReadFile(ctx context.Context, fp string) ([]byte, error) {
|
2017-12-14 01:29:03 +00:00
|
|
|
if r.ImmutableRef == nil {
|
|
|
|
return nil, errors.Wrapf(os.ErrNotExist, "%s no found", fp)
|
|
|
|
}
|
2017-12-13 23:46:52 +00:00
|
|
|
return cache.ReadFile(ctx, r.ImmutableRef, fp)
|
|
|
|
}
|