2017-12-15 08:06:54 +00:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-04-13 21:13:48 +00:00
|
|
|
"fmt"
|
2017-12-15 08:06:54 +00:00
|
|
|
"io"
|
2017-12-19 09:34:34 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2018-04-13 21:13:48 +00:00
|
|
|
"time"
|
2017-12-15 08:06:54 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/diff"
|
|
|
|
"github.com/containerd/containerd/images"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/containerd/containerd/rootfs"
|
|
|
|
cdsnapshot "github.com/containerd/containerd/snapshots"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/cache/blobs"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/moby/buildkit/executor"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
imageexporter "github.com/moby/buildkit/exporter/containerimage"
|
|
|
|
localexporter "github.com/moby/buildkit/exporter/local"
|
|
|
|
ociexporter "github.com/moby/buildkit/exporter/oci"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/frontend"
|
2017-12-19 09:34:34 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-12-28 07:07:13 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/solver-next"
|
|
|
|
"github.com/moby/buildkit/solver-next/llbsolver/ops"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/source"
|
|
|
|
"github.com/moby/buildkit/source/containerimage"
|
|
|
|
"github.com/moby/buildkit/source/git"
|
|
|
|
"github.com/moby/buildkit/source/http"
|
|
|
|
"github.com/moby/buildkit/source/local"
|
2018-04-13 21:13:48 +00:00
|
|
|
"github.com/moby/buildkit/util/contentutil"
|
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-04-13 21:13:48 +00:00
|
|
|
ociidentity "github.com/opencontainers/image-spec/identity"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-04-13 21:13:48 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-12-15 08:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: this file should be removed. containerd defines ContainerdWorker, oci defines OCIWorker. There is no base worker.
|
|
|
|
|
|
|
|
// WorkerOpt is specific to a worker.
|
|
|
|
// See also CommonOpt.
|
|
|
|
type WorkerOpt struct {
|
2017-12-28 07:07:13 +00:00
|
|
|
ID string
|
|
|
|
Labels map[string]string
|
|
|
|
SessionManager *session.Manager
|
|
|
|
MetadataStore *metadata.Store
|
|
|
|
Executor executor.Executor
|
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
2018-02-09 19:39:48 +00:00
|
|
|
Applier diff.Applier
|
|
|
|
Differ diff.Comparer
|
2017-12-28 07:07:13 +00:00
|
|
|
ImageStore images.Store // optional
|
2017-12-15 08:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Worker is a local worker instance with dedicated snapshotter, cache, and so on.
|
|
|
|
// TODO: s/Worker/OpWorker/g ?
|
|
|
|
type Worker struct {
|
|
|
|
WorkerOpt
|
|
|
|
CacheManager cache.Manager
|
|
|
|
SourceManager *source.Manager
|
|
|
|
Exporters map[string]exporter.Exporter
|
|
|
|
ImageSource source.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker instantiates a local worker
|
|
|
|
func NewWorker(opt WorkerOpt) (*Worker, error) {
|
|
|
|
cm, err := cache.NewManager(cache.ManagerOpt{
|
2017-12-28 07:07:13 +00:00
|
|
|
Snapshotter: opt.Snapshotter,
|
2017-12-15 08:06:54 +00:00
|
|
|
MetadataStore: opt.MetadataStore,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := source.NewManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
is, err := containerimage.NewSource(containerimage.SourceOpt{
|
2017-12-28 07:07:13 +00:00
|
|
|
Snapshotter: opt.Snapshotter,
|
2017-12-15 08:06:54 +00:00
|
|
|
ContentStore: opt.ContentStore,
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
Applier: opt.Applier,
|
2018-02-23 14:53:11 +00:00
|
|
|
ImageStore: opt.ImageStore,
|
2017-12-15 08:06:54 +00:00
|
|
|
CacheAccessor: cm,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.Register(is)
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
|
|
|
|
exporters := map[string]exporter.Exporter{}
|
|
|
|
|
|
|
|
iw, err := imageexporter.NewImageWriter(imageexporter.WriterOpt{
|
2017-12-28 07:07:13 +00:00
|
|
|
Snapshotter: opt.Snapshotter,
|
2017-12-15 08:06:54 +00:00
|
|
|
ContentStore: opt.ContentStore,
|
|
|
|
Differ: opt.Differ,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageExporter, err := imageexporter.New(imageexporter.Opt{
|
|
|
|
Images: opt.ImageStore,
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
ImageWriter: iw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exporters[client.ExporterImage] = imageExporter
|
|
|
|
|
|
|
|
localExporter, err := localexporter.New(localexporter.Opt{
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exporters[client.ExporterLocal] = localExporter
|
|
|
|
|
|
|
|
ociExporter, err := ociexporter.New(ociexporter.Opt{
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
ImageWriter: iw,
|
2017-12-18 00:20:19 +00:00
|
|
|
Variant: ociexporter.VariantOCI,
|
2017-12-15 08:06:54 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exporters[client.ExporterOCI] = ociExporter
|
|
|
|
|
2017-12-18 00:20:19 +00:00
|
|
|
dockerExporter, err := ociexporter.New(ociexporter.Opt{
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
ImageWriter: iw,
|
|
|
|
Variant: ociexporter.VariantDocker,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exporters[client.ExporterDocker] = dockerExporter
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
return &Worker{
|
|
|
|
WorkerOpt: opt,
|
|
|
|
CacheManager: cm,
|
|
|
|
SourceManager: sm,
|
|
|
|
Exporters: exporters,
|
|
|
|
ImageSource: is,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
func (w *Worker) ID() string {
|
|
|
|
return w.WorkerOpt.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Labels() map[string]string {
|
|
|
|
return w.WorkerOpt.Labels
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:13:48 +00:00
|
|
|
func (w *Worker) LoadRef(id string) (cache.ImmutableRef, error) {
|
|
|
|
return w.CacheManager.Get(context.TODO(), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) ResolveOp(v solver.Vertex, s frontend.FrontendLLBBridge) (solver.Op, error) {
|
2017-12-15 08:06:54 +00:00
|
|
|
switch op := v.Sys().(type) {
|
|
|
|
case *pb.Op_Source:
|
2018-04-13 21:13:48 +00:00
|
|
|
return ops.NewSourceOp(v, op, w.SourceManager, w)
|
2017-12-15 08:06:54 +00:00
|
|
|
case *pb.Op_Exec:
|
2018-04-13 21:13:48 +00:00
|
|
|
return ops.NewExecOp(v, op, w.CacheManager, w.Executor, w)
|
2017-12-15 08:06:54 +00:00
|
|
|
case *pb.Op_Build:
|
2018-04-13 21:13:48 +00:00
|
|
|
return ops.NewBuildOp(v, op, s, w)
|
2017-12-15 08:06:54 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("could not resolve %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-12-19 09:34:34 +00:00
|
|
|
return "", nil, errors.Errorf("worker %q does not implement ResolveImageConfig", w.ID())
|
2017-12-15 08:06:54 +00:00
|
|
|
}
|
|
|
|
return resolveImageConfig.ResolveImageConfig(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
type resolveImageConfig interface {
|
|
|
|
ResolveImageConfig(ctx context.Context, ref string) (digest.Digest, []byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) DiskUsage(ctx context.Context, opt client.DiskUsageInfo) ([]*client.UsageInfo, error) {
|
|
|
|
return w.CacheManager.DiskUsage(ctx, opt)
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
func (w *Worker) Prune(ctx context.Context, ch chan client.UsageInfo) error {
|
|
|
|
return w.CacheManager.Prune(ctx, ch)
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00: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-04-13 21:13:48 +00:00
|
|
|
func (w *Worker) GetRemote(ctx context.Context, ref cache.ImmutableRef) (*solver.Remote, error) {
|
|
|
|
diffPairs, err := blobs.GetDiffPairs(ctx, w.ContentStore, w.Snapshotter, w.Differ, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed calculaing diff pairs for exported snapshot")
|
|
|
|
}
|
|
|
|
if len(diffPairs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
descs := make([]ocispec.Descriptor, len(diffPairs))
|
|
|
|
|
|
|
|
for i, dp := range diffPairs {
|
|
|
|
info, err := w.ContentStore.Info(ctx, dp.Blobsum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
descs[i] = ocispec.Descriptor{
|
|
|
|
Digest: dp.Blobsum,
|
|
|
|
Size: info.Size,
|
|
|
|
MediaType: schema2.MediaTypeLayer,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"containerd.io/uncompressed": dp.DiffID.String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &solver.Remote{
|
|
|
|
Descriptors: descs,
|
|
|
|
Provider: w.ContentStore,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) FromRemote(ctx context.Context, remote *solver.Remote) (cache.ImmutableRef, error) {
|
|
|
|
eg, gctx := errgroup.WithContext(ctx)
|
|
|
|
for _, desc := range remote.Descriptors {
|
|
|
|
func(desc ocispec.Descriptor) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
done := oneOffProgress(ctx, fmt.Sprintf("pulling %s", desc.Digest))
|
|
|
|
return done(contentutil.Copy(gctx, w.ContentStore, remote.Provider, desc))
|
|
|
|
})
|
|
|
|
}(desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cs, release := snapshot.NewContainerdSnapshotter(w.Snapshotter)
|
|
|
|
defer release()
|
|
|
|
|
|
|
|
unpackProgressDone := oneOffProgress(ctx, "unpacking")
|
|
|
|
chainID, err := w.unpack(ctx, remote.Descriptors, cs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, unpackProgressDone(err)
|
|
|
|
}
|
|
|
|
unpackProgressDone(nil)
|
|
|
|
|
|
|
|
return w.CacheManager.Get(ctx, chainID, cache.WithDescription(fmt.Sprintf("imported %s", remote.Descriptors[len(remote.Descriptors)-1].Digest)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) unpack(ctx context.Context, descs []ocispec.Descriptor, s cdsnapshot.Snapshotter) (string, error) {
|
|
|
|
layers, err := getLayers(ctx, descs)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chain []digest.Digest
|
|
|
|
for _, layer := range layers {
|
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/uncompressed": layer.Diff.Digest.String(),
|
|
|
|
}
|
|
|
|
if _, err := rootfs.ApplyLayer(ctx, layer, chain, s, w.Applier, cdsnapshot.WithLabels(labels)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
chain = append(chain, layer.Diff.Digest)
|
|
|
|
|
|
|
|
chainID := ociidentity.ChainID(chain)
|
|
|
|
if err := w.Snapshotter.SetBlob(ctx, string(chainID), layer.Diff.Digest, layer.Blob.Digest); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(ociidentity.ChainID(chain)), nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
// utility function. could be moved to the constructor logic?
|
|
|
|
func Labels(executor, snapshotter string) map[string]string {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "unknown"
|
|
|
|
}
|
|
|
|
labels := map[string]string{
|
|
|
|
worker.LabelOS: runtime.GOOS,
|
|
|
|
worker.LabelArch: runtime.GOARCH,
|
|
|
|
worker.LabelExecutor: executor,
|
|
|
|
worker.LabelSnapshotter: snapshotter,
|
|
|
|
worker.LabelHostname: hostname,
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID reads the worker id from the `workerid` file.
|
|
|
|
// If not exist, it creates a random one,
|
|
|
|
func ID(root string) (string, error) {
|
|
|
|
f := filepath.Join(root, "workerid")
|
|
|
|
b, err := ioutil.ReadFile(f)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
id := identity.NewID()
|
|
|
|
err := ioutil.WriteFile(f, []byte(id), 0400)
|
|
|
|
return id, err
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
2018-04-13 21:13:48 +00: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
|
|
|
|
}
|
|
|
|
}
|