2018-04-17 22:50:17 +00:00
|
|
|
package buildkit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2018-05-14 18:05:49 +00:00
|
|
|
"strings"
|
2018-04-17 22:50:17 +00:00
|
|
|
"sync"
|
2018-05-19 00:50:52 +00:00
|
|
|
"time"
|
2018-04-17 22:50:17 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2018-05-15 17:02:16 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-04-17 22:50:17 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/daemon/images"
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
|
"github.com/moby/buildkit/session"
|
2018-05-18 05:47:34 +00:00
|
|
|
"github.com/moby/buildkit/util/tracing"
|
2018-04-17 22:50:17 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
grpcmetadata "google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// Opt is option struct required for creating the builder
|
2018-04-17 22:50:17 +00:00
|
|
|
type Opt struct {
|
|
|
|
SessionManager *session.Manager
|
|
|
|
Root string
|
|
|
|
Dist images.DistributionServices
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// Builder can build using BuildKit backend
|
2018-04-17 22:50:17 +00:00
|
|
|
type Builder struct {
|
2018-05-18 05:47:34 +00:00
|
|
|
controller *control.Controller
|
|
|
|
reqBodyHandler *reqBodyHandler
|
2018-04-19 18:08:33 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2018-05-19 00:50:52 +00:00
|
|
|
jobs map[string]*buildJob
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// New creates a new builder
|
2018-04-17 22:50:17 +00:00
|
|
|
func New(opt Opt) (*Builder, error) {
|
2018-05-18 05:47:34 +00:00
|
|
|
reqHandler := newReqBodyHandler(tracing.DefaultTransport)
|
|
|
|
|
|
|
|
c, err := newController(reqHandler, opt)
|
2018-04-17 22:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := &Builder{
|
2018-05-18 05:47:34 +00:00
|
|
|
controller: c,
|
|
|
|
reqBodyHandler: reqHandler,
|
2018-05-19 00:50:52 +00:00
|
|
|
jobs: map[string]*buildJob{},
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// Cancel cancels a build using ID
|
2018-04-19 18:08:33 +00:00
|
|
|
func (b *Builder) Cancel(ctx context.Context, id string) error {
|
|
|
|
b.mu.Lock()
|
2018-05-19 00:50:52 +00:00
|
|
|
if j, ok := b.jobs[id]; ok && j.cancel != nil {
|
|
|
|
j.cancel()
|
2018-04-19 18:08:33 +00:00
|
|
|
}
|
|
|
|
b.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// DiskUsage returns a report about space used by build cache
|
2018-05-15 17:02:16 +00:00
|
|
|
func (b *Builder) DiskUsage(ctx context.Context) ([]*types.BuildCache, error) {
|
|
|
|
duResp, err := b.controller.DiskUsage(ctx, &controlapi.DiskUsageRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var items []*types.BuildCache
|
|
|
|
for _, r := range duResp.Record {
|
|
|
|
items = append(items, &types.BuildCache{
|
|
|
|
ID: r.ID,
|
|
|
|
Mutable: r.Mutable,
|
|
|
|
InUse: r.InUse,
|
|
|
|
Size: r.Size_,
|
|
|
|
|
|
|
|
CreatedAt: r.CreatedAt,
|
|
|
|
LastUsedAt: r.LastUsedAt,
|
|
|
|
UsageCount: int(r.UsageCount),
|
|
|
|
Parent: r.Parent,
|
|
|
|
Description: r.Description,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// Prune clears all reclaimable build cache
|
2018-05-15 22:12:03 +00:00
|
|
|
func (b *Builder) Prune(ctx context.Context) (int64, error) {
|
|
|
|
ch := make(chan *controlapi.UsageRecord)
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer close(ch)
|
|
|
|
return b.controller.Prune(&controlapi.PruneRequest{}, &pruneProxy{
|
|
|
|
streamProxy: streamProxy{ctx: ctx},
|
|
|
|
ch: ch,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var size int64
|
|
|
|
eg.Go(func() error {
|
|
|
|
for r := range ch {
|
|
|
|
size += r.Size_
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
// Build executes a build request
|
2018-04-17 22:50:17 +00:00
|
|
|
func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.Result, error) {
|
2018-05-19 00:50:52 +00:00
|
|
|
var rc = opt.Source
|
|
|
|
|
2018-05-15 23:06:06 +00:00
|
|
|
if buildID := opt.Options.BuildID; buildID != "" {
|
|
|
|
b.mu.Lock()
|
2018-05-19 00:50:52 +00:00
|
|
|
|
|
|
|
upload := false
|
|
|
|
if strings.HasPrefix(buildID, "upload-request:") {
|
|
|
|
upload = true
|
|
|
|
buildID = strings.TrimPrefix(buildID, "upload-request:")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.jobs[buildID]; !ok {
|
|
|
|
b.jobs[buildID] = newBuildJob()
|
|
|
|
}
|
|
|
|
j := b.jobs[buildID]
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
j.cancel = cancel
|
2018-05-15 23:06:06 +00:00
|
|
|
b.mu.Unlock()
|
2018-05-19 00:50:52 +00:00
|
|
|
|
|
|
|
if upload {
|
|
|
|
ctx2, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := j.SetUpload(ctx2, rc)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if remoteContext := opt.Options.RemoteContext; remoteContext == "upload-request" {
|
|
|
|
ctx2, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
var err error
|
|
|
|
rc, err = j.WaitUpload(ctx2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opt.Options.RemoteContext = ""
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
delete(b.jobs, buildID)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:05:49 +00:00
|
|
|
var out builder.Result
|
2018-04-19 18:08:33 +00:00
|
|
|
|
2018-04-17 22:50:17 +00:00
|
|
|
id := identity.NewID()
|
|
|
|
|
|
|
|
frontendAttrs := map[string]string{}
|
|
|
|
|
|
|
|
if opt.Options.Target != "" {
|
|
|
|
frontendAttrs["target"] = opt.Options.Target
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Options.Dockerfile != "" && opt.Options.Dockerfile != "." {
|
|
|
|
frontendAttrs["filename"] = opt.Options.Dockerfile
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Options.RemoteContext != "" {
|
2018-05-18 05:47:34 +00:00
|
|
|
if opt.Options.RemoteContext != "client-session" {
|
|
|
|
frontendAttrs["context"] = opt.Options.RemoteContext
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-19 00:50:52 +00:00
|
|
|
url, cancel := b.reqBodyHandler.newRequest(rc)
|
2018-05-18 05:47:34 +00:00
|
|
|
defer cancel()
|
|
|
|
frontendAttrs["context"] = url
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
cacheFrom := append([]string{}, opt.Options.CacheFrom...)
|
|
|
|
|
2018-05-14 18:05:49 +00:00
|
|
|
frontendAttrs["cache-from"] = strings.Join(cacheFrom, ",")
|
2018-04-17 22:50:17 +00:00
|
|
|
|
|
|
|
for k, v := range opt.Options.BuildArgs {
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
frontendAttrs["build-arg:"+k] = *v
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:05:49 +00:00
|
|
|
for k, v := range opt.Options.Labels {
|
|
|
|
frontendAttrs["label:"+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Options.NoCache {
|
|
|
|
frontendAttrs["no-cache"] = ""
|
|
|
|
}
|
|
|
|
|
2018-04-17 22:50:17 +00:00
|
|
|
req := &controlapi.SolveRequest{
|
|
|
|
Ref: id,
|
2018-05-14 18:05:49 +00:00
|
|
|
Exporter: "moby",
|
2018-04-17 22:50:17 +00:00
|
|
|
Frontend: "dockerfile.v0",
|
|
|
|
FrontendAttrs: frontendAttrs,
|
|
|
|
Session: opt.Options.SessionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
2018-05-14 18:05:49 +00:00
|
|
|
resp, err := b.controller.Solve(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id, ok := resp.ExporterResponse["containerimage.digest"]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("missing image id")
|
|
|
|
}
|
|
|
|
out.ImageID = id
|
|
|
|
return nil
|
2018-04-17 22:50:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ch := make(chan *controlapi.StatusResponse)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer close(ch)
|
|
|
|
return b.controller.Status(&controlapi.StatusRequest{
|
|
|
|
Ref: id,
|
2018-05-15 22:12:03 +00:00
|
|
|
}, &statusProxy{streamProxy: streamProxy{ctx: ctx}, ch: ch})
|
2018-04-17 22:50:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
for sr := range ch {
|
|
|
|
dt, err := sr.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
auxJSONBytes, err := json.Marshal(dt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
auxJSON := new(json.RawMessage)
|
|
|
|
*auxJSON = auxJSONBytes
|
|
|
|
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{ID: "buildkit-trace", Aux: auxJSON})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msgJSON = append(msgJSON, []byte("\r\n")...)
|
|
|
|
n, err := opt.ProgressWriter.Output.Write(msgJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(msgJSON) {
|
|
|
|
return io.ErrShortWrite
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:05:49 +00:00
|
|
|
return &out, nil
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
type streamProxy struct {
|
2018-04-17 22:50:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
func (sp *streamProxy) SetHeader(_ grpcmetadata.MD) error {
|
2018-04-17 22:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
func (sp *streamProxy) SendHeader(_ grpcmetadata.MD) error {
|
2018-04-17 22:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
func (sp *streamProxy) SetTrailer(_ grpcmetadata.MD) {
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
func (sp *streamProxy) Context() context.Context {
|
|
|
|
return sp.ctx
|
|
|
|
}
|
|
|
|
func (sp *streamProxy) RecvMsg(m interface{}) error {
|
|
|
|
return io.EOF
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:03 +00:00
|
|
|
type statusProxy struct {
|
|
|
|
streamProxy
|
|
|
|
ch chan *controlapi.StatusResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *statusProxy) Send(resp *controlapi.StatusResponse) error {
|
|
|
|
return sp.SendMsg(resp)
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
func (sp *statusProxy) SendMsg(m interface{}) error {
|
|
|
|
if sr, ok := m.(*controlapi.StatusResponse); ok {
|
|
|
|
sp.ch <- sr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-15 22:12:03 +00:00
|
|
|
|
|
|
|
type pruneProxy struct {
|
|
|
|
streamProxy
|
|
|
|
ch chan *controlapi.UsageRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *pruneProxy) Send(resp *controlapi.UsageRecord) error {
|
|
|
|
return sp.SendMsg(resp)
|
|
|
|
}
|
|
|
|
func (sp *pruneProxy) SendMsg(m interface{}) error {
|
|
|
|
if sr, ok := m.(*controlapi.UsageRecord); ok {
|
|
|
|
sp.ch <- sr
|
|
|
|
}
|
|
|
|
return nil
|
2018-04-17 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type contentStoreNoLabels struct {
|
|
|
|
content.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *contentStoreNoLabels) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
|
|
|
|
return content.Info{}, nil
|
|
|
|
}
|
2018-05-19 00:50:52 +00:00
|
|
|
|
|
|
|
type wrapRC struct {
|
|
|
|
io.ReadCloser
|
|
|
|
once sync.Once
|
|
|
|
err error
|
|
|
|
waitCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapRC) Read(b []byte) (int, error) {
|
|
|
|
n, err := w.ReadCloser.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
e := err
|
|
|
|
if e == io.EOF {
|
|
|
|
e = nil
|
|
|
|
}
|
|
|
|
w.close(e)
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapRC) Close() error {
|
|
|
|
err := w.ReadCloser.Close()
|
|
|
|
w.close(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapRC) close(err error) {
|
|
|
|
w.once.Do(func() {
|
|
|
|
w.err = err
|
|
|
|
close(w.waitCh)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapRC) wait() error {
|
|
|
|
<-w.waitCh
|
|
|
|
return w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type buildJob struct {
|
|
|
|
cancel func()
|
|
|
|
waitCh chan func(io.ReadCloser) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBuildJob() *buildJob {
|
|
|
|
return &buildJob{waitCh: make(chan func(io.ReadCloser) error)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *buildJob) WaitUpload(ctx context.Context) (io.ReadCloser, error) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
var upload io.ReadCloser
|
|
|
|
fn := func(rc io.ReadCloser) error {
|
|
|
|
w := &wrapRC{ReadCloser: rc, waitCh: make(chan struct{})}
|
|
|
|
upload = w
|
|
|
|
close(done)
|
|
|
|
return w.wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case j.waitCh <- fn:
|
|
|
|
<-done
|
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *buildJob) SetUpload(ctx context.Context, rc io.ReadCloser) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case fn := <-j.waitCh:
|
|
|
|
return fn(rc)
|
|
|
|
}
|
|
|
|
}
|