2017-11-21 08:08:36 +00:00
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-12-28 07:07:13 +00:00
|
|
|
"time"
|
2017-11-21 08:08:36 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd"
|
2018-06-22 02:06:12 +00:00
|
|
|
introspection "github.com/containerd/containerd/api/services/introspection/v1"
|
2017-12-28 06:29:50 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
2017-12-28 07:07:13 +00:00
|
|
|
"github.com/containerd/containerd/snapshots"
|
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 06:29:50 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
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-03-18 05:07:19 +00:00
|
|
|
"github.com/moby/buildkit/util/throttle"
|
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"
|
2018-03-18 05:07:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-11-21 08:08:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2018-09-05 06:09:27 +00:00
|
|
|
opts = append(opts, containerd.WithDefaultNamespace(ns))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata.db"))
|
|
|
|
if err != nil {
|
2017-12-15 08:06:54 +00:00
|
|
|
return base.WorkerOpt{}, err
|
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
|
|
|
|
2018-03-18 05:07:19 +00:00
|
|
|
throttledGC := throttle.Throttle(time.Second, func() {
|
2017-12-28 07:07:13 +00:00
|
|
|
// TODO: how to avoid this?
|
2018-03-18 05:07:19 +00:00
|
|
|
ctx := context.TODO()
|
2017-12-28 06:29:50 +00:00
|
|
|
snapshotter := client.SnapshotService(snapshotterName)
|
2018-09-05 06:09:27 +00:00
|
|
|
ctx = namespaces.WithNamespace(ctx, ns)
|
2017-12-28 06:29:50 +00:00
|
|
|
key := identity.NewID()
|
2017-12-28 07:07:13 +00:00
|
|
|
if _, err := snapshotter.Prepare(ctx, key, "", snapshots.WithLabels(map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
})); err != nil {
|
2018-03-18 05:07:19 +00:00
|
|
|
logrus.Errorf("GC error: %+v", err)
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
if err := snapshotter.Remove(ctx, key); err != nil {
|
2018-03-18 05:07:19 +00:00
|
|
|
logrus.Errorf("GC error: %+v", err)
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
2018-03-18 05:07:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
gc := func(ctx context.Context) error {
|
|
|
|
throttledGC()
|
2017-12-28 06:29:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 06:09:27 +00:00
|
|
|
cs := containerdsnapshot.NewContentStore(client.ContentStore(), ns, gc)
|
2017-12-28 07:07:13 +00:00
|
|
|
|
2018-06-22 02:06:12 +00:00
|
|
|
resp, err := client.IntrospectionService().Plugins(context.TODO(), &introspection.PluginsRequest{Filters: []string{"type==io.containerd.runtime.v1"}})
|
|
|
|
if err != nil {
|
|
|
|
return base.WorkerOpt{}, errors.Wrap(err, "failed to list runtime plugin")
|
|
|
|
}
|
|
|
|
if len(resp.Plugins) == 0 {
|
|
|
|
return base.WorkerOpt{}, errors.Wrap(err, "failed to get runtime plugin")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
opt := base.WorkerOpt{
|
2017-12-28 07:07:13 +00:00
|
|
|
ID: id,
|
|
|
|
Labels: xlabels,
|
|
|
|
MetadataStore: md,
|
2019-07-10 21:42:30 +00:00
|
|
|
Executor: containerdexecutor.New(client, root, "", np, dns),
|
2019-03-28 09:26:34 +00:00
|
|
|
Snapshotter: containerdsnapshot.NewSnapshotter(snapshotterName, client.SnapshotService(snapshotterName), cs, md, ns, gc, nil),
|
2017-12-28 07:07:13 +00:00
|
|
|
ContentStore: cs,
|
2018-07-15 00:07:39 +00:00
|
|
|
Applier: winlayers.NewFileSystemApplierWithWindows(cs, df),
|
|
|
|
Differ: winlayers.NewWalkingDiffWithWindows(cs, df),
|
2017-12-28 07:07:13 +00:00
|
|
|
ImageStore: client.ImageService(),
|
2018-06-22 02:06:12 +00:00
|
|
|
Platforms: platforms,
|
2019-05-21 18:32:21 +00:00
|
|
|
LeaseManager: leaseutil.WithNamespace(client.LeasesService(), ns),
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
return opt, nil
|
|
|
|
}
|