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"
|
|
|
|
|
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"
|
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"
|
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"
|
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"
|
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-12-09 06:15:55 +00:00
|
|
|
func NewWorkerOpt(root string, snFactory SnapshotterFactory, rootless bool, processMode oci.ProcessMode, labels map[string]string, idmap *idtools.IdentityMapping, nopt netproviders.Opt, dns *oci.DNSConfig, binary string) (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
|
|
|
}
|
2019-07-10 21:42:30 +00:00
|
|
|
|
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 opt, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 06:15:55 +00:00
|
|
|
// Check if user has specified OCI worker binary; if they have, append it to cmds
|
|
|
|
var cmds []string
|
|
|
|
if binary != "" {
|
|
|
|
cmds = append(cmds, binary)
|
|
|
|
}
|
|
|
|
|
2018-05-30 02:49:43 +00:00
|
|
|
exe, err := runcexecutor.New(runcexecutor.Opt{
|
|
|
|
// Root directory
|
|
|
|
Root: filepath.Join(root, "executor"),
|
2019-12-09 06:15:55 +00:00
|
|
|
// If user has specified OCI worker binary, it will be sent to the runc executor to find and use
|
|
|
|
// Otherwise, a nil array will be sent and the default OCI worker binary will be used
|
|
|
|
CommandCandidates: cmds,
|
2018-05-30 02:49:43 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
c = containerdsnapshot.NewContentStore(mdb.ContentStore(), "buildkit")
|
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
|
|
|
|
}
|
2019-09-30 23:38:02 +00:00
|
|
|
snap := containerdsnapshot.NewSnapshotter(snFactory.Name, mdb.Snapshotter(snFactory.Name), "buildkit", idmap)
|
|
|
|
lm := leaseutil.WithNamespace(ctdmetadata.NewLeaseManager(mdb), "buildkit")
|
|
|
|
if err := cache.MigrateV2(context.TODO(), filepath.Join(root, "metadata.db"), filepath.Join(root, "metadata_v2.db"), c, snap, lm); err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata_v2.db"))
|
|
|
|
if err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
opt = base.WorkerOpt{
|
2019-03-20 06:40:24 +00:00
|
|
|
ID: id,
|
|
|
|
Labels: xlabels,
|
2020-09-15 06:40:05 +00:00
|
|
|
MdStore: md,
|
2019-03-20 06:40:24 +00:00
|
|
|
Executor: exe,
|
2019-09-30 23:38:02 +00:00
|
|
|
Snapshotter: snap,
|
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-09-30 23:38:02 +00:00
|
|
|
LeaseManager: lm,
|
2019-09-18 00:18:32 +00:00
|
|
|
GarbageCollect: mdb.GarbageCollect,
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
return opt, nil
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|