2017-11-21 08:08:36 +00:00
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd"
|
2019-09-18 00:18:32 +00:00
|
|
|
"github.com/containerd/containerd/gc"
|
|
|
|
"github.com/containerd/containerd/leases"
|
2019-09-30 23:38:02 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/executor/containerdexecutor"
|
2019-06-06 01:46:52 +00:00
|
|
|
"github.com/moby/buildkit/executor/oci"
|
2017-12-28 07:07:13 +00:00
|
|
|
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
|
2019-05-21 18:32:21 +00:00
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2019-08-06 05:23:23 +00:00
|
|
|
"github.com/moby/buildkit/util/network/netproviders"
|
2018-07-15 00:07:39 +00:00
|
|
|
"github.com/moby/buildkit/util/winlayers"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/worker/base"
|
2018-06-22 02:06:12 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewWorkerOpt creates a WorkerOpt.
|
2019-08-06 05:23:23 +00:00
|
|
|
func NewWorkerOpt(root string, address, snapshotterName, ns string, labels map[string]string, dns *oci.DNSConfig, nopt netproviders.Opt, opts ...containerd.ClientOpt) (base.WorkerOpt, error) {
|
2019-08-02 15:48:51 +00:00
|
|
|
opts = append(opts,
|
|
|
|
containerd.WithDefaultNamespace(ns),
|
|
|
|
containerd.WithDefaultRuntime("io.containerd.runtime.v1.linux"),
|
|
|
|
)
|
2017-11-21 08:08:36 +00:00
|
|
|
client, err := containerd.New(address, opts...)
|
|
|
|
if err != nil {
|
2017-12-15 08:06:54 +00:00
|
|
|
return base.WorkerOpt{}, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", address)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
2019-07-10 21:42:30 +00:00
|
|
|
return newContainerd(root, client, snapshotterName, ns, labels, dns, nopt)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 05:23:23 +00:00
|
|
|
func newContainerd(root string, client *containerd.Client, snapshotterName, ns string, labels map[string]string, dns *oci.DNSConfig, nopt netproviders.Opt) (base.WorkerOpt, error) {
|
2017-11-21 08:08:36 +00:00
|
|
|
if strings.Contains(snapshotterName, "/") {
|
2017-12-15 08:06:54 +00:00
|
|
|
return base.WorkerOpt{}, errors.Errorf("bad snapshotter name: %q", snapshotterName)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
name := "containerd-" + snapshotterName
|
|
|
|
root = filepath.Join(root, name)
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
2017-12-15 08:06:54 +00:00
|
|
|
return base.WorkerOpt{}, errors.Wrapf(err, "failed to create %s", root)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
df := client.DiffService()
|
2017-12-19 09:34:34 +00:00
|
|
|
// TODO: should use containerd daemon instance ID (containerd/containerd#1862)?
|
|
|
|
id, err := base.ID(root)
|
|
|
|
if err != nil {
|
|
|
|
return base.WorkerOpt{}, err
|
|
|
|
}
|
|
|
|
xlabels := base.Labels("containerd", snapshotterName)
|
|
|
|
for k, v := range labels {
|
|
|
|
xlabels[k] = v
|
|
|
|
}
|
2017-12-28 06:29:50 +00:00
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
lm := leaseutil.WithNamespace(client.LeasesService(), ns)
|
2018-03-18 05:07:19 +00:00
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
gc := func(ctx context.Context) (gc.Stats, error) {
|
|
|
|
l, err := lm.Create(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, lm.Delete(ctx, leases.Lease{ID: l.ID}, leases.SynchronousDelete)
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
cs := containerdsnapshot.NewContentStore(client.ContentStore(), ns)
|
2017-12-28 07:07:13 +00:00
|
|
|
|
2020-02-25 02:06:30 +00:00
|
|
|
resp, err := client.IntrospectionService().Plugins(context.TODO(), []string{"type==io.containerd.runtime.v1", "type==io.containerd.runtime.v2"})
|
2018-06-22 02:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return base.WorkerOpt{}, errors.Wrap(err, "failed to list runtime plugin")
|
|
|
|
}
|
|
|
|
if len(resp.Plugins) == 0 {
|
2020-01-05 13:42:14 +00:00
|
|
|
return base.WorkerOpt{}, errors.New("failed to find any runtime plugins")
|
2018-06-22 02:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var platforms []specs.Platform
|
|
|
|
for _, plugin := range resp.Plugins {
|
|
|
|
for _, p := range plugin.Platforms {
|
|
|
|
platforms = append(platforms, specs.Platform{
|
|
|
|
OS: p.OS,
|
|
|
|
Architecture: p.Architecture,
|
|
|
|
Variant: p.Variant,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 05:23:23 +00:00
|
|
|
np, err := netproviders.Providers(nopt)
|
2019-07-10 21:42:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return base.WorkerOpt{}, err
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:38:02 +00:00
|
|
|
snap := containerdsnapshot.NewSnapshotter(snapshotterName, client.SnapshotService(snapshotterName), ns, nil)
|
|
|
|
|
|
|
|
if err := cache.MigrateV2(context.TODO(), filepath.Join(root, "metadata.db"), filepath.Join(root, "metadata_v2.db"), cs, snap, lm); err != nil {
|
|
|
|
return base.WorkerOpt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata_v2.db"))
|
|
|
|
if err != nil {
|
|
|
|
return base.WorkerOpt{}, err
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
opt := base.WorkerOpt{
|
2019-09-18 00:18:32 +00:00
|
|
|
ID: id,
|
|
|
|
Labels: xlabels,
|
|
|
|
MetadataStore: md,
|
|
|
|
Executor: containerdexecutor.New(client, root, "", np, dns),
|
2019-09-30 23:38:02 +00:00
|
|
|
Snapshotter: snap,
|
2019-09-18 00:18:32 +00:00
|
|
|
ContentStore: cs,
|
|
|
|
Applier: winlayers.NewFileSystemApplierWithWindows(cs, df),
|
|
|
|
Differ: winlayers.NewWalkingDiffWithWindows(cs, df),
|
|
|
|
ImageStore: client.ImageService(),
|
|
|
|
Platforms: platforms,
|
|
|
|
LeaseManager: lm,
|
|
|
|
GarbageCollect: gc,
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
return opt, nil
|
|
|
|
}
|