2018-04-17 18:50:17 -04:00
|
|
|
package buildkit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-19 17:43:21 -05:00
|
|
|
"fmt"
|
2018-04-17 18:50:17 -04:00
|
|
|
"io"
|
2018-08-10 11:48:40 -04:00
|
|
|
"net"
|
2019-02-28 01:35:26 -05:00
|
|
|
"strconv"
|
2018-05-14 14:05:49 -04:00
|
|
|
"strings"
|
2018-04-17 18:50:17 -04:00
|
|
|
"sync"
|
2018-05-18 20:50:52 -04:00
|
|
|
"time"
|
2018-04-17 18:50:17 -04:00
|
|
|
|
2018-06-26 19:17:28 -04:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2020-04-13 23:31:26 -04:00
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
2018-05-15 13:02:16 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
|
|
|
"github.com/docker/docker/builder"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/docker/docker/daemon/images"
|
2019-06-10 19:28:01 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2018-06-11 14:48:42 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2018-07-02 22:31:05 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2018-08-02 17:24:34 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2018-04-17 18:50:17 -04:00
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/moby/buildkit/client"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
|
"github.com/moby/buildkit/session"
|
2018-08-09 17:19:50 -04:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2018-05-18 01:47:34 -04:00
|
|
|
"github.com/moby/buildkit/util/tracing"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-04-02 00:08:51 -04:00
|
|
|
"google.golang.org/grpc"
|
2018-04-17 18:50:17 -04:00
|
|
|
grpcmetadata "google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
2018-11-19 17:43:21 -05:00
|
|
|
type errMultipleFilterValues struct{}
|
|
|
|
|
|
|
|
func (errMultipleFilterValues) Error() string { return "filters expect only one value" }
|
|
|
|
|
|
|
|
func (errMultipleFilterValues) InvalidParameter() {}
|
|
|
|
|
|
|
|
type errConflictFilter struct {
|
|
|
|
a, b string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e errConflictFilter) Error() string {
|
|
|
|
return fmt.Sprintf("conflicting filters: %q and %q", e.a, e.b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (errConflictFilter) InvalidParameter() {}
|
2018-08-15 17:24:37 -04:00
|
|
|
|
|
|
|
var cacheFields = map[string]bool{
|
|
|
|
"id": true,
|
|
|
|
"parent": true,
|
|
|
|
"type": true,
|
|
|
|
"description": true,
|
|
|
|
"inuse": true,
|
|
|
|
"shared": true,
|
|
|
|
"private": true,
|
|
|
|
// fields from buildkit that are not exposed
|
|
|
|
"mutable": false,
|
|
|
|
"immutable": false,
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Opt is option struct required for creating the builder
|
2018-04-17 18:50:17 -04:00
|
|
|
type Opt struct {
|
2018-08-14 21:40:28 -04:00
|
|
|
SessionManager *session.Manager
|
|
|
|
Root string
|
|
|
|
Dist images.DistributionServices
|
|
|
|
NetworkController libnetwork.NetworkController
|
|
|
|
DefaultCgroupParent string
|
2020-04-13 23:31:26 -04:00
|
|
|
RegistryHosts docker.RegistryHosts
|
2018-09-04 22:12:44 -04:00
|
|
|
BuilderConfig config.BuilderConfig
|
2019-02-28 03:12:55 -05:00
|
|
|
Rootless bool
|
2019-06-10 19:28:01 -04:00
|
|
|
IdentityMapping *idtools.IdentityMapping
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig config.DNSConfig
|
2020-10-09 13:20:48 -04:00
|
|
|
ApparmorProfile string
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Builder can build using BuildKit backend
|
2018-04-17 18:50:17 -04:00
|
|
|
type Builder struct {
|
2018-05-18 01:47:34 -04:00
|
|
|
controller *control.Controller
|
|
|
|
reqBodyHandler *reqBodyHandler
|
2018-04-19 14:08:33 -04:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2018-05-18 20:50:52 -04:00
|
|
|
jobs map[string]*buildJob
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// New creates a new builder
|
2018-04-17 18:50:17 -04:00
|
|
|
func New(opt Opt) (*Builder, error) {
|
2018-05-18 01:47:34 -04:00
|
|
|
reqHandler := newReqBodyHandler(tracing.DefaultTransport)
|
|
|
|
|
2019-07-01 14:26:27 -04:00
|
|
|
if opt.IdentityMapping != nil && opt.IdentityMapping.Empty() {
|
|
|
|
opt.IdentityMapping = nil
|
|
|
|
}
|
|
|
|
|
2018-05-18 01:47:34 -04:00
|
|
|
c, err := newController(reqHandler, opt)
|
2018-04-17 18:50:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := &Builder{
|
2018-05-18 01:47:34 -04:00
|
|
|
controller: c,
|
|
|
|
reqBodyHandler: reqHandler,
|
2018-05-18 20:50:52 -04:00
|
|
|
jobs: map[string]*buildJob{},
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 00:08:51 -04:00
|
|
|
// RegisterGRPC registers controller to the grpc server.
|
|
|
|
func (b *Builder) RegisterGRPC(s *grpc.Server) {
|
|
|
|
b.controller.Register(s)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Cancel cancels a build using ID
|
2018-04-19 14:08:33 -04:00
|
|
|
func (b *Builder) Cancel(ctx context.Context, id string) error {
|
|
|
|
b.mu.Lock()
|
2018-05-18 20:50:52 -04:00
|
|
|
if j, ok := b.jobs[id]; ok && j.cancel != nil {
|
|
|
|
j.cancel()
|
2018-04-19 14:08:33 -04:00
|
|
|
}
|
|
|
|
b.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// DiskUsage returns a report about space used by build cache
|
2018-05-15 13:02:16 -04: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{
|
2018-08-15 17:24:37 -04:00
|
|
|
ID: r.ID,
|
|
|
|
Parent: r.Parent,
|
|
|
|
Type: r.RecordType,
|
|
|
|
Description: r.Description,
|
|
|
|
InUse: r.InUse,
|
|
|
|
Shared: r.Shared,
|
|
|
|
Size: r.Size_,
|
2018-05-15 13:02:16 -04:00
|
|
|
CreatedAt: r.CreatedAt,
|
|
|
|
LastUsedAt: r.LastUsedAt,
|
|
|
|
UsageCount: int(r.UsageCount),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Prune clears all reclaimable build cache
|
2018-08-15 17:24:37 -04:00
|
|
|
func (b *Builder) Prune(ctx context.Context, opts types.BuildCachePruneOptions) (int64, []string, error) {
|
2018-05-15 18:12:03 -04:00
|
|
|
ch := make(chan *controlapi.UsageRecord)
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2018-08-15 17:24:37 -04:00
|
|
|
validFilters := make(map[string]bool, 1+len(cacheFields))
|
|
|
|
validFilters["unused-for"] = true
|
2018-11-19 17:43:21 -05:00
|
|
|
validFilters["until"] = true
|
2018-11-20 18:31:50 -05:00
|
|
|
validFilters["label"] = true // TODO(tiborvass): handle label
|
|
|
|
validFilters["label!"] = true // TODO(tiborvass): handle label!
|
2018-08-15 17:24:37 -04:00
|
|
|
for k, v := range cacheFields {
|
|
|
|
validFilters[k] = v
|
|
|
|
}
|
|
|
|
if err := opts.Filters.Validate(validFilters); err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:12:44 -04:00
|
|
|
pi, err := toBuildkitPruneInfo(opts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2018-08-15 17:24:37 -04:00
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
eg.Go(func() error {
|
|
|
|
defer close(ch)
|
2018-08-15 17:24:37 -04:00
|
|
|
return b.controller.Prune(&controlapi.PruneRequest{
|
2018-09-04 22:12:44 -04:00
|
|
|
All: pi.All,
|
|
|
|
KeepDuration: int64(pi.KeepDuration),
|
|
|
|
KeepBytes: pi.KeepBytes,
|
|
|
|
Filter: pi.Filter,
|
2018-08-15 17:24:37 -04:00
|
|
|
}, &pruneProxy{
|
2018-05-15 18:12:03 -04:00
|
|
|
streamProxy: streamProxy{ctx: ctx},
|
|
|
|
ch: ch,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var size int64
|
2018-08-15 17:24:37 -04:00
|
|
|
var cacheIDs []string
|
2018-05-15 18:12:03 -04:00
|
|
|
eg.Go(func() error {
|
|
|
|
for r := range ch {
|
|
|
|
size += r.Size_
|
2018-08-15 17:24:37 -04:00
|
|
|
cacheIDs = append(cacheIDs, r.ID)
|
2018-05-15 18:12:03 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
2018-08-15 17:24:37 -04:00
|
|
|
return 0, nil, err
|
2018-05-15 18:12:03 -04:00
|
|
|
}
|
|
|
|
|
2018-08-15 17:24:37 -04:00
|
|
|
return size, cacheIDs, nil
|
2018-05-15 18:12:03 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Build executes a build request
|
2018-04-17 18:50:17 -04:00
|
|
|
func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.Result, error) {
|
2018-05-18 20:50:52 -04:00
|
|
|
var rc = opt.Source
|
|
|
|
|
2018-05-15 19:06:06 -04:00
|
|
|
if buildID := opt.Options.BuildID; buildID != "" {
|
|
|
|
b.mu.Lock()
|
2018-05-18 20:50:52 -04: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]
|
2018-05-24 22:37:11 -04:00
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2018-05-18 20:50:52 -04:00
|
|
|
j.cancel = cancel
|
2018-05-15 19:06:06 -04:00
|
|
|
b.mu.Unlock()
|
2018-05-18 20:50:52 -04: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 19:06:06 -04:00
|
|
|
defer func() {
|
2020-04-03 18:33:15 -04:00
|
|
|
b.mu.Lock()
|
2018-05-15 19:06:06 -04:00
|
|
|
delete(b.jobs, buildID)
|
2020-04-03 18:33:15 -04:00
|
|
|
b.mu.Unlock()
|
2018-05-15 19:06:06 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
var out builder.Result
|
2018-04-19 14:08:33 -04:00
|
|
|
|
2018-04-17 18:50:17 -04: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 01:47:34 -04:00
|
|
|
if opt.Options.RemoteContext != "client-session" {
|
|
|
|
frontendAttrs["context"] = opt.Options.RemoteContext
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-18 20:50:52 -04:00
|
|
|
url, cancel := b.reqBodyHandler.newRequest(rc)
|
2018-05-18 01:47:34 -04:00
|
|
|
defer cancel()
|
|
|
|
frontendAttrs["context"] = url
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
cacheFrom := append([]string{}, opt.Options.CacheFrom...)
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
frontendAttrs["cache-from"] = strings.Join(cacheFrom, ",")
|
2018-04-17 18:50:17 -04:00
|
|
|
|
|
|
|
for k, v := range opt.Options.BuildArgs {
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
frontendAttrs["build-arg:"+k] = *v
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
for k, v := range opt.Options.Labels {
|
|
|
|
frontendAttrs["label:"+k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Options.NoCache {
|
|
|
|
frontendAttrs["no-cache"] = ""
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:53:19 -04:00
|
|
|
if opt.Options.PullParent {
|
|
|
|
frontendAttrs["image-resolve-mode"] = "pull"
|
|
|
|
} else {
|
|
|
|
frontendAttrs["image-resolve-mode"] = "default"
|
|
|
|
}
|
|
|
|
|
2018-07-02 22:31:05 -04:00
|
|
|
if opt.Options.Platform != "" {
|
|
|
|
// same as in newBuilder in builder/dockerfile.builder.go
|
|
|
|
// TODO: remove once opt.Options.Platform is of type specs.Platform
|
|
|
|
sp, err := platforms.Parse(opt.Options.Platform)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := system.ValidatePlatform(sp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
frontendAttrs["platform"] = opt.Options.Platform
|
2018-06-26 19:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:19:50 -04:00
|
|
|
switch opt.Options.NetworkMode {
|
|
|
|
case "host", "none":
|
|
|
|
frontendAttrs["force-network-mode"] = opt.Options.NetworkMode
|
|
|
|
case "", "default":
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("network mode %q not supported by buildkit", opt.Options.NetworkMode)
|
|
|
|
}
|
|
|
|
|
2018-08-10 11:48:40 -04:00
|
|
|
extraHosts, err := toBuildkitExtraHosts(opt.Options.ExtraHosts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
frontendAttrs["add-hosts"] = extraHosts
|
|
|
|
|
2019-03-17 12:54:52 -04:00
|
|
|
exporterName := ""
|
2018-05-24 20:40:13 -04:00
|
|
|
exporterAttrs := map[string]string{}
|
|
|
|
|
2019-03-17 12:54:52 -04:00
|
|
|
if len(opt.Options.Outputs) > 1 {
|
|
|
|
return nil, errors.Errorf("multiple outputs not supported")
|
|
|
|
} else if len(opt.Options.Outputs) == 0 {
|
|
|
|
exporterName = "moby"
|
|
|
|
} else {
|
|
|
|
// cacheonly is a special type for triggering skipping all exporters
|
|
|
|
if opt.Options.Outputs[0].Type != "cacheonly" {
|
|
|
|
exporterName = opt.Options.Outputs[0].Type
|
|
|
|
exporterAttrs = opt.Options.Outputs[0].Attrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exporterName == "moby" {
|
|
|
|
if len(opt.Options.Tags) > 0 {
|
|
|
|
exporterAttrs["name"] = strings.Join(opt.Options.Tags, ",")
|
|
|
|
}
|
2018-05-24 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
2019-02-28 01:35:26 -05:00
|
|
|
cache := controlapi.CacheOptions{}
|
|
|
|
|
|
|
|
if inlineCache := opt.Options.BuildArgs["BUILDKIT_INLINE_CACHE"]; inlineCache != nil {
|
|
|
|
if b, err := strconv.ParseBool(*inlineCache); err == nil && b {
|
|
|
|
cache.Exports = append(cache.Exports, &controlapi.CacheOptionsEntry{
|
|
|
|
Type: "inline",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:50:17 -04:00
|
|
|
req := &controlapi.SolveRequest{
|
|
|
|
Ref: id,
|
2019-03-17 12:54:52 -04:00
|
|
|
Exporter: exporterName,
|
2018-05-24 20:40:13 -04:00
|
|
|
ExporterAttrs: exporterAttrs,
|
2018-04-17 18:50:17 -04:00
|
|
|
Frontend: "dockerfile.v0",
|
|
|
|
FrontendAttrs: frontendAttrs,
|
|
|
|
Session: opt.Options.SessionID,
|
2019-02-28 01:35:26 -05:00
|
|
|
Cache: cache,
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:19:50 -04:00
|
|
|
if opt.Options.NetworkMode == "host" {
|
|
|
|
req.Entitlements = append(req.Entitlements, entitlements.EntitlementNetworkHost)
|
|
|
|
}
|
|
|
|
|
2018-07-11 19:52:03 -04:00
|
|
|
aux := streamformatter.AuxFormatter{Writer: opt.ProgressWriter.Output}
|
2018-06-11 14:48:42 -04:00
|
|
|
|
2018-04-17 18:50:17 -04:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
2018-05-14 14:05:49 -04:00
|
|
|
resp, err := b.controller.Solve(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-17 12:54:52 -04:00
|
|
|
if exporterName != "moby" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-14 14:05:49 -04:00
|
|
|
id, ok := resp.ExporterResponse["containerimage.digest"]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("missing image id")
|
|
|
|
}
|
|
|
|
out.ImageID = id
|
2018-06-11 14:48:42 -04:00
|
|
|
return aux.Emit("moby.image.id", types.BuildResult{ID: id})
|
2018-04-17 18:50:17 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
ch := make(chan *controlapi.StatusResponse)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer close(ch)
|
2018-08-06 17:39:56 -04:00
|
|
|
// streamProxy.ctx is not set to ctx because when request is cancelled,
|
|
|
|
// only the build request has to be cancelled, not the status request.
|
|
|
|
stream := &statusProxy{streamProxy: streamProxy{ctx: context.TODO()}, ch: ch}
|
|
|
|
return b.controller.Status(&controlapi.StatusRequest{Ref: id}, stream)
|
2018-04-17 18:50:17 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
for sr := range ch {
|
|
|
|
dt, err := sr.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-11 14:48:42 -04:00
|
|
|
if err := aux.Emit("moby.buildkit.trace", dt); err != nil {
|
2018-04-17 18:50:17 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
return &out, nil
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
type streamProxy struct {
|
2018-04-17 18:50:17 -04:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
func (sp *streamProxy) SetHeader(_ grpcmetadata.MD) error {
|
2018-04-17 18:50:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
func (sp *streamProxy) SendHeader(_ grpcmetadata.MD) error {
|
2018-04-17 18:50:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
func (sp *streamProxy) SetTrailer(_ grpcmetadata.MD) {
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
func (sp *streamProxy) Context() context.Context {
|
|
|
|
return sp.ctx
|
|
|
|
}
|
|
|
|
func (sp *streamProxy) RecvMsg(m interface{}) error {
|
|
|
|
return io.EOF
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-15 18:12:03 -04:00
|
|
|
type statusProxy struct {
|
|
|
|
streamProxy
|
|
|
|
ch chan *controlapi.StatusResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *statusProxy) Send(resp *controlapi.StatusResponse) error {
|
|
|
|
return sp.SendMsg(resp)
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
func (sp *statusProxy) SendMsg(m interface{}) error {
|
|
|
|
if sr, ok := m.(*controlapi.StatusResponse); ok {
|
|
|
|
sp.ch <- sr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-15 18:12:03 -04: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 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-18 20:50:52 -04: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)
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 11:48:40 -04:00
|
|
|
|
|
|
|
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
|
|
|
|
func toBuildkitExtraHosts(inp []string) (string, error) {
|
|
|
|
if len(inp) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
hosts := make([]string, 0, len(inp))
|
|
|
|
for _, h := range inp {
|
|
|
|
parts := strings.Split(h, ":")
|
2018-11-19 17:43:21 -05:00
|
|
|
|
2018-08-10 11:48:40 -04:00
|
|
|
if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
|
|
|
|
return "", errors.Errorf("invalid host %s", h)
|
|
|
|
}
|
|
|
|
hosts = append(hosts, parts[0]+"="+parts[1])
|
|
|
|
}
|
|
|
|
return strings.Join(hosts, ","), nil
|
|
|
|
}
|
2018-09-04 22:12:44 -04:00
|
|
|
|
|
|
|
func toBuildkitPruneInfo(opts types.BuildCachePruneOptions) (client.PruneInfo, error) {
|
2018-11-19 17:43:21 -05:00
|
|
|
var until time.Duration
|
|
|
|
untilValues := opts.Filters.Get("until") // canonical
|
|
|
|
unusedForValues := opts.Filters.Get("unused-for") // deprecated synonym for "until" filter
|
2018-09-04 22:12:44 -04:00
|
|
|
|
2018-11-19 17:43:21 -05:00
|
|
|
if len(untilValues) > 0 && len(unusedForValues) > 0 {
|
|
|
|
return client.PruneInfo{}, errConflictFilter{"until", "unused-for"}
|
|
|
|
}
|
|
|
|
filterKey := "until"
|
|
|
|
if len(unusedForValues) > 0 {
|
|
|
|
filterKey = "unused-for"
|
|
|
|
}
|
|
|
|
untilValues = append(untilValues, unusedForValues...)
|
2018-09-04 22:12:44 -04:00
|
|
|
|
2018-11-19 17:43:21 -05:00
|
|
|
switch len(untilValues) {
|
|
|
|
case 0:
|
|
|
|
// nothing to do
|
2018-09-04 22:12:44 -04:00
|
|
|
case 1:
|
|
|
|
var err error
|
2018-11-19 17:43:21 -05:00
|
|
|
until, err = time.ParseDuration(untilValues[0])
|
2018-09-04 22:12:44 -04:00
|
|
|
if err != nil {
|
2018-11-19 17:43:21 -05:00
|
|
|
return client.PruneInfo{}, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", filterKey)
|
2018-09-04 22:12:44 -04:00
|
|
|
}
|
|
|
|
default:
|
2018-11-19 17:43:21 -05:00
|
|
|
return client.PruneInfo{}, errMultipleFilterValues{}
|
2018-09-04 22:12:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bkFilter := make([]string, 0, opts.Filters.Len())
|
|
|
|
for cacheField := range cacheFields {
|
2019-04-08 09:46:45 -04:00
|
|
|
if opts.Filters.Contains(cacheField) {
|
2018-10-16 14:51:25 -04:00
|
|
|
values := opts.Filters.Get(cacheField)
|
|
|
|
switch len(values) {
|
|
|
|
case 0:
|
|
|
|
bkFilter = append(bkFilter, cacheField)
|
|
|
|
case 1:
|
|
|
|
if cacheField == "id" {
|
|
|
|
bkFilter = append(bkFilter, cacheField+"~="+values[0])
|
|
|
|
} else {
|
|
|
|
bkFilter = append(bkFilter, cacheField+"=="+values[0])
|
|
|
|
}
|
|
|
|
default:
|
2018-11-19 17:43:21 -05:00
|
|
|
return client.PruneInfo{}, errMultipleFilterValues{}
|
2018-10-16 14:51:25 -04:00
|
|
|
}
|
2018-09-04 22:12:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.PruneInfo{
|
|
|
|
All: opts.All,
|
2018-11-19 17:43:21 -05:00
|
|
|
KeepDuration: until,
|
2018-09-04 22:12:44 -04:00
|
|
|
KeepBytes: opts.KeepStorage,
|
2018-10-16 14:51:25 -04:00
|
|
|
Filter: []string{strings.Join(bkFilter, ",")},
|
2018-09-04 22:12:44 -04:00
|
|
|
}, nil
|
|
|
|
}
|