forked from jessfraz/buildkit
/
forward.go
66 lines (59 loc) · 1.62 KB
/
forward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dockerfile
import (
"context"
"os"
"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"
"github.com/pkg/errors"
)
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
final *ref
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) {
if r.ImmutableRef == nil {
return nil, errors.Wrapf(os.ErrNotExist, "%s no found", fp)
}
return cache.ReadFile(ctx, r.ImmutableRef, fp)
}