2018-04-17 18:50:17 -04:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-20 14:52:10 -04:00
|
|
|
"fmt"
|
2018-04-17 18:50:17 -04:00
|
|
|
"io"
|
2018-04-20 14:52:10 -04:00
|
|
|
"io/ioutil"
|
2018-05-18 01:47:34 -04:00
|
|
|
nethttp "net/http"
|
2018-04-20 14:52:10 -04:00
|
|
|
"runtime"
|
2018-04-17 18:50:17 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/rootfs"
|
2018-04-20 14:52:10 -04:00
|
|
|
"github.com/docker/docker/distribution"
|
|
|
|
distmetadata "github.com/docker/docker/distribution/metadata"
|
|
|
|
"github.com/docker/docker/distribution/xfer"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
pkgprogress "github.com/docker/docker/pkg/progress"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/moby/buildkit/executor"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"github.com/moby/buildkit/solver/llbsolver/ops"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/source"
|
|
|
|
"github.com/moby/buildkit/source/git"
|
|
|
|
"github.com/moby/buildkit/source/http"
|
|
|
|
"github.com/moby/buildkit/source/local"
|
2018-04-20 14:52:10 -04:00
|
|
|
"github.com/moby/buildkit/util/contentutil"
|
2018-04-17 18:50:17 -04:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Opt defines a structure for creating a worker.
|
|
|
|
type Opt struct {
|
2018-04-20 14:52:10 -04:00
|
|
|
ID string
|
|
|
|
Labels map[string]string
|
|
|
|
SessionManager *session.Manager
|
|
|
|
MetadataStore *metadata.Store
|
|
|
|
Executor executor.Executor
|
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
|
|
|
CacheManager cache.Manager
|
|
|
|
ImageSource source.Source
|
|
|
|
Exporters map[string]exporter.Exporter
|
|
|
|
DownloadManager distribution.RootFSDownloadManager
|
|
|
|
V2MetadataService distmetadata.V2MetadataService
|
2018-05-18 01:47:34 -04:00
|
|
|
Transport nethttp.RoundTripper
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Worker is a local worker instance with dedicated snapshotter, cache, and so on.
|
|
|
|
// TODO: s/Worker/OpWorker/g ?
|
|
|
|
type Worker struct {
|
2018-05-23 18:53:14 -04:00
|
|
|
Opt
|
2018-04-17 18:50:17 -04:00
|
|
|
SourceManager *source.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker instantiates a local worker
|
2018-05-23 18:53:14 -04:00
|
|
|
func NewWorker(opt Opt) (*Worker, error) {
|
2018-04-17 18:50:17 -04:00
|
|
|
sm, err := source.NewManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cm := opt.CacheManager
|
|
|
|
sm.Register(opt.ImageSource)
|
|
|
|
|
|
|
|
gs, err := git.NewSource(git.Opt{
|
|
|
|
CacheAccessor: cm,
|
|
|
|
MetadataStore: opt.MetadataStore,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.Register(gs)
|
|
|
|
|
|
|
|
hs, err := http.NewSource(http.Opt{
|
|
|
|
CacheAccessor: cm,
|
|
|
|
MetadataStore: opt.MetadataStore,
|
2018-05-18 01:47:34 -04:00
|
|
|
Transport: opt.Transport,
|
2018-04-17 18:50:17 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.Register(hs)
|
|
|
|
|
|
|
|
ss, err := local.NewSource(local.Opt{
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
CacheAccessor: cm,
|
|
|
|
MetadataStore: opt.MetadataStore,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sm.Register(ss)
|
|
|
|
|
|
|
|
return &Worker{
|
2018-05-23 18:53:14 -04:00
|
|
|
Opt: opt,
|
2018-04-17 18:50:17 -04:00
|
|
|
SourceManager: sm,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// ID returns worker ID
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) ID() string {
|
2018-05-23 18:53:14 -04:00
|
|
|
return w.Opt.ID
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Labels returns map of all worker labels
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) Labels() map[string]string {
|
2018-05-23 18:53:14 -04:00
|
|
|
return w.Opt.Labels
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// LoadRef loads a reference by ID
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) LoadRef(id string) (cache.ImmutableRef, error) {
|
|
|
|
return w.CacheManager.Get(context.TODO(), id)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// ResolveOp converts a LLB vertex into a LLB operation
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) ResolveOp(v solver.Vertex, s frontend.FrontendLLBBridge) (solver.Op, error) {
|
|
|
|
switch op := v.Sys().(type) {
|
|
|
|
case *pb.Op_Source:
|
|
|
|
return ops.NewSourceOp(v, op, w.SourceManager, w)
|
|
|
|
case *pb.Op_Exec:
|
2018-05-18 01:47:34 -04:00
|
|
|
return ops.NewExecOp(v, op, w.CacheManager, w.MetadataStore, w.Executor, w)
|
2018-04-17 18:50:17 -04:00
|
|
|
case *pb.Op_Build:
|
|
|
|
return ops.NewBuildOp(v, op, s, w)
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("could not resolve %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// ResolveImageConfig returns image config for an image
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error) {
|
|
|
|
// ImageSource is typically source/containerimage
|
|
|
|
resolveImageConfig, ok := w.ImageSource.(resolveImageConfig)
|
|
|
|
if !ok {
|
|
|
|
return "", nil, errors.Errorf("worker %q does not implement ResolveImageConfig", w.ID())
|
|
|
|
}
|
|
|
|
return resolveImageConfig.ResolveImageConfig(ctx, ref)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Exec executes a process directly on a worker
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) Exec(ctx context.Context, meta executor.Meta, rootFS cache.ImmutableRef, stdin io.ReadCloser, stdout, stderr io.WriteCloser) error {
|
|
|
|
active, err := w.CacheManager.New(ctx, rootFS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer active.Release(context.TODO())
|
|
|
|
return w.Executor.Exec(ctx, meta, active, nil, stdin, stdout, stderr)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// DiskUsage returns disk usage report
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) DiskUsage(ctx context.Context, opt client.DiskUsageInfo) ([]*client.UsageInfo, error) {
|
|
|
|
return w.CacheManager.DiskUsage(ctx, opt)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Prune deletes reclaimable build cache
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) Prune(ctx context.Context, ch chan client.UsageInfo) error {
|
|
|
|
return w.CacheManager.Prune(ctx, ch)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// Exporter returns exporter by name
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) Exporter(name string) (exporter.Exporter, error) {
|
|
|
|
exp, ok := w.Exporters[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("exporter %q could not be found", name)
|
|
|
|
}
|
|
|
|
return exp, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// GetRemote returns a remote snapshot reference for a local one
|
2018-05-14 14:05:49 -04:00
|
|
|
func (w *Worker) GetRemote(ctx context.Context, ref cache.ImmutableRef, createIfNeeded bool) (*solver.Remote, error) {
|
2018-04-17 18:50:17 -04:00
|
|
|
return nil, errors.Errorf("getremote not implemented")
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// FromRemote converts a remote snapshot reference to a local one
|
2018-04-17 18:50:17 -04:00
|
|
|
func (w *Worker) FromRemote(ctx context.Context, remote *solver.Remote) (cache.ImmutableRef, error) {
|
2018-04-20 14:52:10 -04:00
|
|
|
rootfs, err := getLayers(ctx, remote.Descriptors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
layers := make([]xfer.DownloadDescriptor, 0, len(rootfs))
|
|
|
|
|
|
|
|
for _, l := range rootfs {
|
|
|
|
// ongoing.add(desc)
|
|
|
|
layers = append(layers, &layerDescriptor{
|
|
|
|
desc: l.Blob,
|
|
|
|
diffID: layer.DiffID(l.Diff.Digest),
|
|
|
|
provider: remote.Provider,
|
|
|
|
w: w,
|
|
|
|
pctx: ctx,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for _, l := range rootfs {
|
|
|
|
w.ContentStore.Delete(context.TODO(), l.Blob.Digest)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
r := image.NewRootFS()
|
|
|
|
rootFS, release, err := w.DownloadManager.Download(ctx, *r, runtime.GOOS, layers, &discardProgress{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
ref, err := w.CacheManager.GetFromSnapshotter(ctx, string(rootFS.ChainID()), cache.WithDescription(fmt.Sprintf("imported %s", remote.Descriptors[len(remote.Descriptors)-1].Digest)))
|
2018-04-20 14:52:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ref, nil
|
2018-04-17 18:50:17 -04:00
|
|
|
}
|
|
|
|
|
2018-04-20 14:52:10 -04:00
|
|
|
type discardProgress struct{}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
func (*discardProgress) WriteProgress(_ pkgprogress.Progress) error {
|
2018-04-20 14:52:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch(ctx context.Context, desc ocispec.Descriptor) (io.ReadCloser, error)
|
|
|
|
type layerDescriptor struct {
|
|
|
|
provider content.Provider
|
|
|
|
desc ocispec.Descriptor
|
|
|
|
diffID layer.DiffID
|
|
|
|
// ref ctdreference.Spec
|
|
|
|
w *Worker
|
|
|
|
pctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *layerDescriptor) Key() string {
|
|
|
|
return "v2:" + ld.desc.Digest.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *layerDescriptor) ID() string {
|
|
|
|
return ld.desc.Digest.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *layerDescriptor) DiffID() (layer.DiffID, error) {
|
|
|
|
return ld.diffID, nil
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:48:30 -04:00
|
|
|
func (ld *layerDescriptor) Download(ctx context.Context, progressOutput pkgprogress.Output) (io.ReadCloser, int64, error) {
|
2018-04-20 14:52:10 -04:00
|
|
|
done := oneOffProgress(ld.pctx, fmt.Sprintf("pulling %s", ld.desc.Digest))
|
|
|
|
if err := contentutil.Copy(ctx, ld.w.ContentStore, ld.provider, ld.desc); err != nil {
|
|
|
|
return nil, 0, done(err)
|
|
|
|
}
|
|
|
|
done(nil)
|
|
|
|
|
2018-06-08 22:05:48 -04:00
|
|
|
ra, err := ld.w.ContentStore.ReaderAt(ctx, ld.desc)
|
2018-04-20 14:52:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *layerDescriptor) Close() {
|
|
|
|
// ld.is.ContentStore.Delete(context.TODO(), ld.desc.Digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *layerDescriptor) Registered(diffID layer.DiffID) {
|
|
|
|
// Cache mapping from this layer's DiffID to the blobsum
|
|
|
|
ld.w.V2MetadataService.Add(diffID, distmetadata.V2Metadata{Digest: ld.desc.Digest})
|
|
|
|
}
|
2018-04-17 18:50:17 -04:00
|
|
|
|
|
|
|
func getLayers(ctx context.Context, descs []ocispec.Descriptor) ([]rootfs.Layer, error) {
|
|
|
|
layers := make([]rootfs.Layer, len(descs))
|
|
|
|
for i, desc := range descs {
|
|
|
|
diffIDStr := desc.Annotations["containerd.io/uncompressed"]
|
|
|
|
if diffIDStr == "" {
|
|
|
|
return nil, errors.Errorf("%s missing uncompressed digest", desc.Digest)
|
|
|
|
}
|
|
|
|
diffID, err := digest.Parse(diffIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers[i].Diff = ocispec.Descriptor{
|
|
|
|
MediaType: ocispec.MediaTypeImageLayer,
|
|
|
|
Digest: diffID,
|
|
|
|
}
|
|
|
|
layers[i].Blob = ocispec.Descriptor{
|
|
|
|
MediaType: desc.MediaType,
|
|
|
|
Digest: desc.Digest,
|
|
|
|
Size: desc.Size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 18:53:14 -04:00
|
|
|
|
|
|
|
type resolveImageConfig interface {
|
|
|
|
ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error)
|
|
|
|
}
|