2017-11-21 08:08:36 +00:00
|
|
|
package runc
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-14 06:26:27 +00:00
|
|
|
"context"
|
2017-06-08 00:54:29 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-18 05:07:19 +00:00
|
|
|
"time"
|
2017-06-08 00:54:29 +00:00
|
|
|
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/containerd/containerd/content/local"
|
2018-02-09 19:39:48 +00:00
|
|
|
"github.com/containerd/containerd/diff/apply"
|
2017-11-06 09:44:23 +00:00
|
|
|
"github.com/containerd/containerd/diff/walking"
|
2017-11-21 08:08:36 +00:00
|
|
|
ctdmetadata "github.com/containerd/containerd/metadata"
|
2018-06-22 02:06:12 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2017-12-05 07:34:25 +00:00
|
|
|
ctdsnapshot "github.com/containerd/containerd/snapshots"
|
2019-03-20 06:40:24 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2019-01-03 10:12:12 +00:00
|
|
|
"github.com/moby/buildkit/executor/oci"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/executor/runcexecutor"
|
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"
|
2018-08-21 15:51:28 +00:00
|
|
|
"github.com/moby/buildkit/util/network"
|
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"
|
2018-03-18 05:07:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-18 18:18:08 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2017-06-08 00:54:29 +00:00
|
|
|
)
|
|
|
|
|
2018-02-26 08:17:33 +00:00
|
|
|
// SnapshotterFactory instantiates a snapshotter
|
|
|
|
type SnapshotterFactory struct {
|
|
|
|
Name string
|
|
|
|
New func(root string) (ctdsnapshot.Snapshotter, error)
|
|
|
|
}
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
// NewWorkerOpt creates a WorkerOpt.
|
2019-07-10 21:42:30 +00:00
|
|
|
func NewWorkerOpt(root string, snFactory SnapshotterFactory, rootless bool, processMode oci.ProcessMode, labels map[string]string, idmap *idtools.IdentityMapping, nopt network.Opt, dns *oci.DNSConfig) (base.WorkerOpt, error) {
|
2017-12-15 08:06:54 +00:00
|
|
|
var opt base.WorkerOpt
|
2018-02-26 08:17:33 +00:00
|
|
|
name := "runc-" + snFactory.Name
|
2017-11-21 08:08:36 +00:00
|
|
|
root = filepath.Join(root, name)
|
2017-06-08 00:54:29 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2017-12-15 07:44:09 +00:00
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata.db"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2019-07-10 21:42:30 +00:00
|
|
|
|
|
|
|
np, err := network.Providers(nopt)
|
|
|
|
if err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
|
|
|
|
2018-05-30 02:49:43 +00:00
|
|
|
exe, err := runcexecutor.New(runcexecutor.Opt{
|
|
|
|
// Root directory
|
|
|
|
Root: filepath.Join(root, "executor"),
|
|
|
|
// without root privileges
|
2019-03-20 21:54:20 +00:00
|
|
|
Rootless: rootless,
|
|
|
|
ProcessMode: processMode,
|
|
|
|
IdentityMapping: idmap,
|
2019-06-06 01:46:52 +00:00
|
|
|
DNS: dns,
|
2019-07-10 21:42:30 +00:00
|
|
|
}, np)
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2018-02-26 08:17:33 +00:00
|
|
|
s, err := snFactory.New(filepath.Join(root, "snapshots"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 01:05:19 +00:00
|
|
|
c, err := local.NewStore(filepath.Join(root, "content"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:04:16 +00:00
|
|
|
db, err := bolt.Open(filepath.Join(root, "containerdmeta.db"), 0644, nil)
|
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-11-07 23:04:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
mdb := ctdmetadata.NewDB(db, c, map[string]ctdsnapshot.Snapshotter{
|
2018-02-26 08:17:33 +00:00
|
|
|
snFactory.Name: s,
|
2017-11-07 23:04:16 +00:00
|
|
|
})
|
|
|
|
if err := mdb.Init(context.TODO()); err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-11-07 23:04:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 05:07:19 +00:00
|
|
|
throttledGC := throttle.Throttle(time.Second, func() {
|
|
|
|
if _, err := mdb.GarbageCollect(context.TODO()); err != nil {
|
|
|
|
logrus.Errorf("GC error: %+v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-12-28 06:29:50 +00:00
|
|
|
gc := func(ctx context.Context) error {
|
2018-03-18 05:07:19 +00:00
|
|
|
throttledGC()
|
|
|
|
return nil
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 07:07:13 +00:00
|
|
|
c = containerdsnapshot.NewContentStore(mdb.ContentStore(), "buildkit", gc)
|
2017-06-08 00:54:29 +00:00
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
id, err := base.ID(root)
|
|
|
|
if err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
2018-02-26 08:17:33 +00:00
|
|
|
xlabels := base.Labels("oci", snFactory.Name)
|
2017-12-19 09:34:34 +00:00
|
|
|
for k, v := range labels {
|
|
|
|
xlabels[k] = v
|
|
|
|
}
|
2017-12-15 08:06:54 +00:00
|
|
|
opt = base.WorkerOpt{
|
2019-03-20 06:40:24 +00:00
|
|
|
ID: id,
|
|
|
|
Labels: xlabels,
|
|
|
|
MetadataStore: md,
|
|
|
|
Executor: exe,
|
2019-03-28 09:26:34 +00:00
|
|
|
Snapshotter: containerdsnapshot.NewSnapshotter(snFactory.Name, mdb.Snapshotter(snFactory.Name), c, md, "buildkit", gc, idmap),
|
2019-03-20 06:40:24 +00:00
|
|
|
ContentStore: c,
|
|
|
|
Applier: winlayers.NewFileSystemApplierWithWindows(c, apply.NewFileSystemApplier(c)),
|
|
|
|
Differ: winlayers.NewWalkingDiffWithWindows(c, walking.NewWalkingDiff(c)),
|
|
|
|
ImageStore: nil, // explicitly
|
|
|
|
Platforms: []specs.Platform{platforms.Normalize(platforms.DefaultSpec())},
|
|
|
|
IdentityMapping: idmap,
|
2019-05-21 18:32:21 +00:00
|
|
|
LeaseManager: leaseutil.WithNamespace(leaseutil.NewManager(mdb), "buildkit"),
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
return opt, nil
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|