2018-04-13 21:05:29 +00:00
|
|
|
package llbsolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-27 22:59:11 +00:00
|
|
|
"fmt"
|
2018-04-13 21:05:29 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-01-10 16:43:27 +00:00
|
|
|
"time"
|
2018-04-13 21:05:29 +00:00
|
|
|
|
2018-09-17 20:08:58 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2020-02-05 23:17:33 +00:00
|
|
|
"github.com/mitchellh/hashstructure"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-05-07 21:24:28 +00:00
|
|
|
"github.com/moby/buildkit/cache/remotecache"
|
2019-12-09 20:50:21 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/moby/buildkit/executor"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
2018-07-25 06:18:21 +00:00
|
|
|
gw "github.com/moby/buildkit/frontend/gateway/client"
|
2019-02-23 12:56:04 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2020-05-01 18:39:45 +00:00
|
|
|
"github.com/moby/buildkit/solver/errdefs"
|
2020-02-04 23:00:44 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/moby/buildkit/util/tracing"
|
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
2018-09-11 08:02:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-04-13 21:05:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type llbBridge struct {
|
2018-09-11 08:02:46 +00:00
|
|
|
builder solver.Builder
|
|
|
|
frontends map[string]frontend.Frontend
|
|
|
|
resolveWorker func() (worker.Worker, error)
|
2019-07-25 21:13:13 +00:00
|
|
|
eachWorker func(func(worker.Worker) error) error
|
2018-09-11 08:02:46 +00:00
|
|
|
resolveCacheImporterFuncs map[string]remotecache.ResolveCacheImporterFunc
|
|
|
|
cms map[string]solver.CacheManager
|
|
|
|
cmsMu sync.Mutex
|
2019-02-23 12:56:04 +00:00
|
|
|
sm *session.Manager
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 23:17:33 +00:00
|
|
|
func (b *llbBridge) loadResult(ctx context.Context, def *pb.Definition, cacheImports []gw.CacheOptionsEntry) (solver.CachedResult, error) {
|
2018-07-03 09:59:33 +00:00
|
|
|
w, err := b.resolveWorker()
|
|
|
|
if err != nil {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, err
|
2018-07-03 09:59:33 +00:00
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
ent, err := loadEntitlements(b.builder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 17:49:15 +00:00
|
|
|
var cms []solver.CacheManager
|
2020-02-05 23:17:33 +00:00
|
|
|
for _, im := range cacheImports {
|
|
|
|
cmID, err := cmKey(im)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-13 21:05:29 +00:00
|
|
|
b.cmsMu.Lock()
|
2018-04-25 17:49:15 +00:00
|
|
|
var cm solver.CacheManager
|
2020-02-05 23:17:33 +00:00
|
|
|
if prevCm, ok := b.cms[cmID]; !ok {
|
|
|
|
func(cmID string, im gw.CacheOptionsEntry) {
|
|
|
|
cm = newLazyCacheManager(cmID, func() (solver.CacheManager, error) {
|
2018-04-25 22:22:49 +00:00
|
|
|
var cmNew solver.CacheManager
|
2020-06-30 01:06:02 +00:00
|
|
|
if err := inBuilderContext(context.TODO(), b.builder, "importing cache manifest from "+cmID, "", func(ctx context.Context, g session.Group) error {
|
2018-09-11 08:02:46 +00:00
|
|
|
resolveCI, ok := b.resolveCacheImporterFuncs[im.Type]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("unknown cache importer: %s", im.Type)
|
2018-07-03 09:59:33 +00:00
|
|
|
}
|
2020-06-30 01:06:02 +00:00
|
|
|
ci, desc, err := resolveCI(ctx, g, im.Attrs)
|
2018-07-03 09:59:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
cmNew, err = ci.Resolve(ctx, desc, cmID, w)
|
2018-04-25 22:22:49 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
2020-02-05 23:17:33 +00:00
|
|
|
logrus.Debugf("error while importing cache manifest from cmId=%s: %v", cmID, err)
|
2018-04-25 22:22:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cmNew, nil
|
|
|
|
})
|
2020-02-05 23:17:33 +00:00
|
|
|
}(cmID, im)
|
|
|
|
b.cms[cmID] = cm
|
2018-04-13 21:05:29 +00:00
|
|
|
} else {
|
|
|
|
cm = prevCm
|
|
|
|
}
|
2018-04-25 17:49:15 +00:00
|
|
|
cms = append(cms, cm)
|
2018-04-13 21:05:29 +00:00
|
|
|
b.cmsMu.Unlock()
|
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
dpc := &detectPrunedCacheID{}
|
2018-04-13 21:05:29 +00:00
|
|
|
|
2020-06-03 02:05:32 +00:00
|
|
|
edge, err := Load(def, dpc.Load, ValidateEntitlements(ent), WithCacheSources(cms), NormalizeRuntimePlatforms(), WithValidateCaps())
|
2020-02-05 23:17:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load LLB")
|
2018-08-30 12:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 23:17:33 +00:00
|
|
|
if len(dpc.ids) > 0 {
|
|
|
|
ids := make([]string, 0, len(dpc.ids))
|
|
|
|
for id := range dpc.ids {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
if err := b.eachWorker(func(w worker.Worker) error {
|
|
|
|
return w.PruneCacheMounts(ctx, ids)
|
|
|
|
}); err != nil {
|
2018-08-04 19:42:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
}
|
2018-08-04 19:42:01 +00:00
|
|
|
|
2020-02-05 23:17:33 +00:00
|
|
|
res, err := b.builder.Build(ctx, edge)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wr, ok := res.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid reference for exporting: %T", res.Sys())
|
|
|
|
}
|
|
|
|
if wr.ImmutableRef != nil {
|
|
|
|
if err := wr.ImmutableRef.Finalize(ctx, false); err != nil {
|
|
|
|
return nil, err
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
2019-07-25 21:13:13 +00:00
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (b *llbBridge) Solve(ctx context.Context, req frontend.SolveRequest, sid string) (res *frontend.Result, err error) {
|
2020-02-05 23:17:33 +00:00
|
|
|
if req.Definition != nil && req.Definition.Def != nil && req.Frontend != "" {
|
|
|
|
return nil, errors.New("cannot solve with both Definition and Frontend specified")
|
|
|
|
}
|
2019-07-25 21:13:13 +00:00
|
|
|
|
2020-02-05 23:17:33 +00:00
|
|
|
if req.Definition != nil && req.Definition.Def != nil {
|
|
|
|
res = &frontend.Result{Ref: newResultProxy(b, req)}
|
2018-08-15 15:20:02 +00:00
|
|
|
} else if req.Frontend != "" {
|
2018-04-13 21:05:29 +00:00
|
|
|
f, ok := b.frontends[req.Frontend]
|
|
|
|
if !ok {
|
2018-07-11 23:51:41 +00:00
|
|
|
return nil, errors.Errorf("invalid frontend: %s", req.Frontend)
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
2020-06-30 01:06:02 +00:00
|
|
|
res, err = f.Solve(ctx, b, req.FrontendOpt, req.FrontendInputs, sid)
|
2018-04-13 21:05:29 +00:00
|
|
|
if err != nil {
|
2019-06-02 00:12:49 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to solve with frontend %s", req.Frontend)
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-30 13:01:27 +00:00
|
|
|
return &frontend.Result{}, nil
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 23:00:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type resultProxy struct {
|
|
|
|
cb func(context.Context) (solver.CachedResult, error)
|
|
|
|
def *pb.Definition
|
|
|
|
g flightcontrol.Group
|
|
|
|
mu sync.Mutex
|
|
|
|
released bool
|
|
|
|
v solver.CachedResult
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-02-05 23:17:33 +00:00
|
|
|
func newResultProxy(b *llbBridge, req frontend.SolveRequest) *resultProxy {
|
|
|
|
return &resultProxy{
|
|
|
|
def: req.Definition,
|
|
|
|
cb: func(ctx context.Context) (solver.CachedResult, error) {
|
|
|
|
return b.loadResult(ctx, req.Definition, req.CacheImports)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:00:44 +00:00
|
|
|
func (rp *resultProxy) Definition() *pb.Definition {
|
|
|
|
return rp.def
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *resultProxy) Release(ctx context.Context) error {
|
|
|
|
rp.mu.Lock()
|
|
|
|
defer rp.mu.Unlock()
|
|
|
|
if rp.v != nil {
|
2020-02-05 23:17:33 +00:00
|
|
|
if rp.released {
|
|
|
|
logrus.Warnf("release of already released result")
|
|
|
|
}
|
2020-02-04 23:00:44 +00:00
|
|
|
if err := rp.v.Release(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rp.released = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:39:45 +00:00
|
|
|
func (rp *resultProxy) wrapError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var ve *errdefs.VertexError
|
|
|
|
if errors.As(err, &ve) {
|
2020-05-20 23:16:38 +00:00
|
|
|
if rp.def.Source != nil {
|
|
|
|
locs, ok := rp.def.Source.Locations[string(ve.Digest)]
|
2020-05-01 18:39:45 +00:00
|
|
|
if ok {
|
2020-05-20 23:16:38 +00:00
|
|
|
for _, loc := range locs.Locations {
|
|
|
|
err = errdefs.WithSource(err, errdefs.Source{
|
|
|
|
Info: rp.def.Source.Infos[loc.SourceIndex],
|
|
|
|
Ranges: loc.Ranges,
|
|
|
|
})
|
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *resultProxy) Result(ctx context.Context) (res solver.CachedResult, err error) {
|
|
|
|
defer func() {
|
|
|
|
err = rp.wrapError(err)
|
|
|
|
}()
|
2020-02-04 23:00:44 +00:00
|
|
|
r, err := rp.g.Do(ctx, "result", func(ctx context.Context) (interface{}, error) {
|
|
|
|
rp.mu.Lock()
|
|
|
|
if rp.released {
|
|
|
|
rp.mu.Unlock()
|
|
|
|
return nil, errors.Errorf("accessing released result")
|
|
|
|
}
|
|
|
|
if rp.v != nil || rp.err != nil {
|
|
|
|
rp.mu.Unlock()
|
|
|
|
return rp.v, rp.err
|
2018-07-11 23:51:41 +00:00
|
|
|
}
|
2020-02-04 23:00:44 +00:00
|
|
|
rp.mu.Unlock()
|
|
|
|
v, err := rp.cb(ctx)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
if strings.Contains(err.Error(), context.Canceled.Error()) {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
default:
|
2018-05-22 22:58:22 +00:00
|
|
|
}
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
2020-02-04 23:00:44 +00:00
|
|
|
rp.mu.Lock()
|
|
|
|
if rp.released {
|
|
|
|
if v != nil {
|
|
|
|
v.Release(context.TODO())
|
|
|
|
}
|
|
|
|
rp.mu.Unlock()
|
|
|
|
return nil, errors.Errorf("evaluating released result")
|
|
|
|
}
|
|
|
|
rp.v = v
|
|
|
|
rp.err = err
|
|
|
|
rp.mu.Unlock()
|
|
|
|
return v, err
|
|
|
|
})
|
|
|
|
if r != nil {
|
|
|
|
return r.(solver.CachedResult), nil
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
2020-02-04 23:00:44 +00:00
|
|
|
return nil, err
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 21:48:42 +00:00
|
|
|
func (s *llbBridge) Run(ctx context.Context, id string, root cache.Mountable, mounts []executor.Mount, process executor.ProcessInfo, started chan<- struct{}) (err error) {
|
2018-04-13 21:05:29 +00:00
|
|
|
w, err := s.resolveWorker()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-16 21:48:42 +00:00
|
|
|
span, ctx := tracing.StartSpan(ctx, strings.Join(process.Meta.Args, " "))
|
2020-07-17 00:54:15 +00:00
|
|
|
err = w.Executor().Run(ctx, id, root, mounts, process, started)
|
2020-07-16 21:48:42 +00:00
|
|
|
tracing.FinishWithError(span, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *llbBridge) Exec(ctx context.Context, id string, process executor.ProcessInfo) (err error) {
|
|
|
|
w, err := s.resolveWorker()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
span, ctx := tracing.StartSpan(ctx, strings.Join(process.Meta.Args, " "))
|
2020-07-17 00:54:15 +00:00
|
|
|
err = w.Executor().Exec(ctx, id, process)
|
2018-04-13 21:05:29 +00:00
|
|
|
tracing.FinishWithError(span, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-09 20:50:21 +00:00
|
|
|
func (s *llbBridge) ResolveImageConfig(ctx context.Context, ref string, opt llb.ResolveImageConfigOpt) (dgst digest.Digest, config []byte, err error) {
|
2018-04-13 21:05:29 +00:00
|
|
|
w, err := s.resolveWorker()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2018-07-28 00:27:41 +00:00
|
|
|
if opt.LogName == "" {
|
|
|
|
opt.LogName = fmt.Sprintf("resolve image config for %s", ref)
|
|
|
|
}
|
2018-09-17 20:08:58 +00:00
|
|
|
id := ref // make a deterministic ID for avoiding duplicates
|
|
|
|
if platform := opt.Platform; platform == nil {
|
|
|
|
id += platforms.Format(platforms.DefaultSpec())
|
|
|
|
} else {
|
|
|
|
id += platforms.Format(*platform)
|
|
|
|
}
|
2020-06-30 01:06:02 +00:00
|
|
|
err = inBuilderContext(ctx, s.builder, opt.LogName, id, func(ctx context.Context, g session.Group) error {
|
|
|
|
dgst, config, err = w.ResolveImageConfig(ctx, ref, opt, s.sm, g)
|
2018-07-27 22:59:11 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return dgst, config, err
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lazyCacheManager struct {
|
|
|
|
id string
|
|
|
|
main solver.CacheManager
|
|
|
|
|
|
|
|
waitCh chan struct{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lcm *lazyCacheManager) ID() string {
|
|
|
|
return lcm.id
|
|
|
|
}
|
|
|
|
func (lcm *lazyCacheManager) Query(inp []solver.CacheKeyWithSelector, inputIndex solver.Index, dgst digest.Digest, outputIndex solver.Index) ([]*solver.CacheKey, error) {
|
2019-04-22 23:07:58 +00:00
|
|
|
lcm.wait()
|
|
|
|
if lcm.main == nil {
|
|
|
|
return nil, nil
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
return lcm.main.Query(inp, inputIndex, dgst, outputIndex)
|
|
|
|
}
|
|
|
|
func (lcm *lazyCacheManager) Records(ck *solver.CacheKey) ([]*solver.CacheRecord, error) {
|
2019-04-22 23:07:58 +00:00
|
|
|
lcm.wait()
|
|
|
|
if lcm.main == nil {
|
|
|
|
return nil, nil
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
return lcm.main.Records(ck)
|
|
|
|
}
|
|
|
|
func (lcm *lazyCacheManager) Load(ctx context.Context, rec *solver.CacheRecord) (solver.Result, error) {
|
|
|
|
if err := lcm.wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lcm.main.Load(ctx, rec)
|
|
|
|
}
|
2019-01-10 16:43:27 +00:00
|
|
|
func (lcm *lazyCacheManager) Save(key *solver.CacheKey, s solver.Result, createdAt time.Time) (*solver.ExportableCacheKey, error) {
|
2018-04-13 21:05:29 +00:00
|
|
|
if err := lcm.wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-10 16:43:27 +00:00
|
|
|
return lcm.main.Save(key, s, createdAt)
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lcm *lazyCacheManager) wait() error {
|
|
|
|
<-lcm.waitCh
|
|
|
|
return lcm.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLazyCacheManager(id string, fn func() (solver.CacheManager, error)) solver.CacheManager {
|
|
|
|
lcm := &lazyCacheManager{id: id, waitCh: make(chan struct{})}
|
|
|
|
go func() {
|
|
|
|
defer close(lcm.waitCh)
|
|
|
|
cm, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
lcm.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lcm.main = cm
|
|
|
|
}()
|
|
|
|
return lcm
|
|
|
|
}
|
2020-02-05 23:17:33 +00:00
|
|
|
|
|
|
|
func cmKey(im gw.CacheOptionsEntry) (string, error) {
|
|
|
|
if im.Type == "registry" && im.Attrs["ref"] != "" {
|
|
|
|
return im.Attrs["ref"], nil
|
|
|
|
}
|
|
|
|
i, err := hashstructure.Hash(im, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", im.Type, i), nil
|
|
|
|
}
|