2018-04-13 21:05:29 +00:00
|
|
|
package llbsolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
2018-05-07 21:24:28 +00:00
|
|
|
"github.com/moby/buildkit/cache/remotecache"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/session"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
|
|
|
"github.com/moby/buildkit/worker"
|
2018-06-23 00:31:55 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-04-13 21:05:29 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExporterRequest struct {
|
2018-05-07 21:51:44 +00:00
|
|
|
Exporter exporter.ExporterInstance
|
2018-07-03 09:59:33 +00:00
|
|
|
CacheExporter remotecache.Exporter
|
2018-05-07 21:51:44 +00:00
|
|
|
CacheExportMode solver.CacheExportMode
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveWorkerFunc returns default worker for the temporary default non-distributed use cases
|
|
|
|
type ResolveWorkerFunc func() (worker.Worker, error)
|
|
|
|
|
|
|
|
type Solver struct {
|
2018-07-03 09:59:33 +00:00
|
|
|
solver *solver.Solver
|
|
|
|
resolveWorker ResolveWorkerFunc
|
|
|
|
frontends map[string]frontend.Frontend
|
|
|
|
resolveCacheImporter remotecache.ResolveCacheImporterFunc
|
|
|
|
platforms []specs.Platform
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 23:27:01 +00:00
|
|
|
func New(wc *worker.Controller, f map[string]frontend.Frontend, cache solver.CacheManager, resolveCI remotecache.ResolveCacheImporterFunc) (*Solver, error) {
|
2018-04-13 21:05:29 +00:00
|
|
|
s := &Solver{
|
2018-07-03 09:59:33 +00:00
|
|
|
resolveWorker: defaultResolver(wc),
|
|
|
|
frontends: f,
|
|
|
|
resolveCacheImporter: resolveCI,
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 00:31:55 +00:00
|
|
|
// executing is currently only allowed on default worker
|
|
|
|
w, err := wc.GetDefault()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.platforms = w.Platforms()
|
|
|
|
|
2018-04-13 21:05:29 +00:00
|
|
|
s.solver = solver.NewSolver(solver.SolverOpt{
|
|
|
|
ResolveOpFunc: s.resolver(),
|
|
|
|
DefaultCache: cache,
|
|
|
|
})
|
2018-06-23 00:31:55 +00:00
|
|
|
return s, nil
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Solver) resolver() solver.ResolveOpFunc {
|
|
|
|
return func(v solver.Vertex, b solver.Builder) (solver.Op, error) {
|
|
|
|
w, err := s.resolveWorker()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.ResolveOp(v, s.Bridge(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Solver) Bridge(b solver.Builder) frontend.FrontendLLBBridge {
|
|
|
|
return &llbBridge{
|
2018-07-03 09:59:33 +00:00
|
|
|
builder: b,
|
|
|
|
frontends: s.frontends,
|
|
|
|
resolveWorker: s.resolveWorker,
|
|
|
|
resolveCacheImporter: s.resolveCacheImporter,
|
|
|
|
cms: map[string]solver.CacheManager{},
|
|
|
|
platforms: s.platforms,
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Solver) Solve(ctx context.Context, id string, req frontend.SolveRequest, exp ExporterRequest) (*client.SolveResponse, error) {
|
|
|
|
j, err := s.solver.NewJob(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer j.Discard()
|
|
|
|
|
|
|
|
j.SessionID = session.FromContext(ctx)
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
res, err := s.Bridge(j).Solve(ctx, req)
|
2018-04-13 21:05:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2018-07-11 23:51:41 +00:00
|
|
|
res.EachRef(func(ref solver.CachedResult) error {
|
|
|
|
go ref.Release(context.TODO())
|
|
|
|
return nil
|
|
|
|
})
|
2018-04-13 21:05:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var exporterResponse map[string]string
|
|
|
|
if exp := exp.Exporter; exp != nil {
|
|
|
|
var immutable cache.ImmutableRef
|
2018-07-11 23:51:41 +00:00
|
|
|
if res := res.Ref; res != nil { // FIXME(tonistiigi):
|
|
|
|
workerRef, ok := res.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid reference: %T", res.Sys())
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
2018-07-11 23:51:41 +00:00
|
|
|
immutable = workerRef.ImmutableRef
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := j.Call(ctx, exp.Name(), func(ctx context.Context) error {
|
2018-07-11 23:51:41 +00:00
|
|
|
exporterResponse, err = exp.Export(ctx, immutable, res.Metadata)
|
2018-04-13 21:05:29 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:51:44 +00:00
|
|
|
if e := exp.CacheExporter; e != nil {
|
2018-04-13 21:05:29 +00:00
|
|
|
if err := j.Call(ctx, "exporting cache", func(ctx context.Context) error {
|
|
|
|
prepareDone := oneOffProgress(ctx, "preparing build cache for export")
|
2018-07-11 23:51:41 +00:00
|
|
|
if err := res.EachRef(func(res solver.CachedResult) error {
|
2018-07-23 21:27:35 +00:00
|
|
|
// all keys have same export chain so exporting others is not needed
|
|
|
|
_, err := res.CacheKeys()[0].Exporter.ExportTo(ctx, e, solver.CacheExportOpt{
|
2018-07-05 23:32:01 +00:00
|
|
|
Convert: workerRefConverter,
|
|
|
|
Mode: exp.CacheExportMode,
|
2018-07-11 23:51:41 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return prepareDone(err)
|
2018-04-13 21:05:29 +00:00
|
|
|
}
|
|
|
|
prepareDone(nil)
|
2018-05-07 21:51:44 +00:00
|
|
|
return e.Finalize(ctx)
|
2018-04-13 21:05:29 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client.SolveResponse{
|
|
|
|
ExporterResponse: exporterResponse,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Solver) Status(ctx context.Context, id string, statusChan chan *client.SolveStatus) error {
|
|
|
|
j, err := s.solver.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return j.Status(ctx, statusChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultResolver(wc *worker.Controller) ResolveWorkerFunc {
|
|
|
|
return func() (worker.Worker, error) {
|
|
|
|
return wc.GetDefault()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneOffProgress(ctx context.Context, id string) func(err error) error {
|
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
return func(err error) error {
|
|
|
|
// TODO: set error on status
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
pw.Write(id, st)
|
|
|
|
pw.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|