2017-12-15 07:00:13 +00:00
|
|
|
// +build linux,!no_oci_worker
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-11-21 08:08:36 +00:00
|
|
|
"os/exec"
|
|
|
|
|
2018-04-03 09:37:16 +00:00
|
|
|
ctdsnapshot "github.com/containerd/containerd/snapshots"
|
|
|
|
"github.com/containerd/containerd/snapshots/native"
|
2018-02-26 08:17:33 +00:00
|
|
|
"github.com/containerd/containerd/snapshots/overlay"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/worker/base"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/worker/runc"
|
2018-07-02 04:56:50 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2018-02-26 08:17:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
func init() {
|
2018-05-30 02:49:43 +00:00
|
|
|
flags := []cli.Flag{
|
2017-11-21 08:08:36 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "oci-worker",
|
|
|
|
Usage: "enable oci workers (true/false/auto)",
|
|
|
|
Value: "auto",
|
2017-12-19 09:34:34 +00:00
|
|
|
},
|
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "oci-worker-labels",
|
|
|
|
Usage: "user-specific annotation labels (com.example.foo=bar)",
|
|
|
|
},
|
2018-02-19 05:33:49 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "oci-worker-snapshotter",
|
2018-04-03 09:37:16 +00:00
|
|
|
Usage: "name of snapshotter (overlayfs or native)",
|
2018-05-30 02:49:43 +00:00
|
|
|
Value: "auto",
|
|
|
|
},
|
2018-06-22 02:06:12 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "oci-worker-platform",
|
|
|
|
Usage: "override supported platforms for worker",
|
|
|
|
},
|
2018-05-30 02:49:43 +00:00
|
|
|
}
|
|
|
|
n := "oci-worker-rootless"
|
|
|
|
u := "enable rootless mode"
|
2018-07-02 04:56:50 +00:00
|
|
|
if system.RunningInUserNS() {
|
2018-05-30 02:49:43 +00:00
|
|
|
flags = append(flags, cli.BoolTFlag{
|
|
|
|
Name: n,
|
|
|
|
Usage: u,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
flags = append(flags, cli.BoolFlag{
|
|
|
|
Name: n,
|
|
|
|
Usage: u,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
registerWorkerInitializer(
|
|
|
|
workerInitializer{
|
|
|
|
fn: ociWorkerInitializer,
|
|
|
|
priority: 0,
|
2018-02-19 05:33:49 +00:00
|
|
|
},
|
2018-05-30 02:49:43 +00:00
|
|
|
flags...,
|
2017-12-19 09:34:34 +00:00
|
|
|
)
|
2018-05-30 02:49:43 +00:00
|
|
|
// TODO: allow multiple oci runtimes
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func ociWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error) {
|
2017-11-21 08:08:36 +00:00
|
|
|
boolOrAuto, err := parseBoolOrAuto(c.GlobalString("oci-worker"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if (boolOrAuto == nil && !validOCIBinary()) || (boolOrAuto != nil && !*boolOrAuto) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-19 09:34:34 +00:00
|
|
|
labels, err := attrMap(c.GlobalStringSlice("oci-worker-labels"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-05 08:38:16 +00:00
|
|
|
snFactory, err := snapshotterFactory(common.root, c.GlobalString("oci-worker-snapshotter"))
|
2018-02-26 08:17:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-30 02:49:43 +00:00
|
|
|
// GlobalBool works for BoolT as well
|
|
|
|
rootless := c.GlobalBool("oci-worker-rootless") || c.GlobalBool("rootless")
|
|
|
|
if rootless {
|
|
|
|
logrus.Debugf("running in rootless mode")
|
|
|
|
}
|
|
|
|
opt, err := runc.NewWorkerOpt(common.root, snFactory, rootless, labels)
|
2017-11-21 08:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opt.SessionManager = common.sessionManager
|
2018-06-22 02:06:12 +00:00
|
|
|
platformsStr := c.GlobalStringSlice("oci-worker-platform")
|
|
|
|
if len(platformsStr) != 0 {
|
|
|
|
platforms, err := parsePlatforms(platformsStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid platforms")
|
|
|
|
}
|
|
|
|
opt.Platforms = platforms
|
|
|
|
}
|
2017-12-15 08:06:54 +00:00
|
|
|
w, err := base.NewWorker(opt)
|
2017-11-21 08:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-15 08:06:54 +00:00
|
|
|
return []worker.Worker{w}, nil
|
2017-06-08 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 08:38:16 +00:00
|
|
|
func snapshotterFactory(commonRoot, name string) (runc.SnapshotterFactory, error) {
|
|
|
|
if name == "auto" {
|
|
|
|
if err := overlay.Supported(commonRoot); err == nil {
|
|
|
|
logrus.Debug("auto snapshotter: using overlayfs")
|
|
|
|
name = "overlayfs"
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("auto snapshotter: using native, because overlayfs is not available for %s: %v", commonRoot, err)
|
|
|
|
name = "native"
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 08:17:33 +00:00
|
|
|
snFactory := runc.SnapshotterFactory{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
switch name {
|
2018-04-03 09:37:16 +00:00
|
|
|
case "native":
|
|
|
|
snFactory.New = native.NewSnapshotter
|
2018-02-26 08:17:33 +00:00
|
|
|
case "overlayfs": // not "overlay", for consistency with containerd snapshotter plugin ID.
|
2018-04-03 09:37:16 +00:00
|
|
|
snFactory.New = func(root string) (ctdsnapshot.Snapshotter, error) {
|
|
|
|
return overlay.NewSnapshotter(root)
|
|
|
|
}
|
2018-02-26 08:17:33 +00:00
|
|
|
default:
|
2018-06-05 08:38:16 +00:00
|
|
|
return snFactory, errors.Errorf("unknown snapshotter name: %q", name)
|
2018-02-26 08:17:33 +00:00
|
|
|
}
|
2018-06-05 08:38:16 +00:00
|
|
|
return snFactory, nil
|
2018-02-26 08:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
func validOCIBinary() bool {
|
|
|
|
_, err := exec.LookPath("runc")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("skipping oci worker, as runc does not exist")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|