buildkit/cmd/buildkitd/main_oci_worker.go

195 lines
4.9 KiB
Go
Raw Normal View History

// +build linux,!no_oci_worker
package main
import (
"os/exec"
"strconv"
ctdsnapshot "github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/native"
"github.com/containerd/containerd/snapshots/overlay"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"github.com/moby/buildkit/worker/runc"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func init() {
defaultConf, _ := defaultConf()
enabledValue := func(b *bool) string {
if b == nil {
return "auto"
}
return strconv.FormatBool(*b)
}
if defaultConf.Workers.OCI.Snapshotter == "" {
defaultConf.Workers.OCI.Snapshotter = "auto"
}
flags := []cli.Flag{
cli.StringFlag{
Name: "oci-worker",
Usage: "enable oci workers (true/false/auto)",
Value: enabledValue(defaultConf.Workers.OCI.Enabled),
},
cli.StringSliceFlag{
Name: "oci-worker-labels",
Usage: "user-specific annotation labels (com.example.foo=bar)",
},
cli.StringFlag{
Name: "oci-worker-snapshotter",
Usage: "name of snapshotter (overlayfs or native)",
Value: defaultConf.Workers.OCI.Snapshotter,
},
cli.StringSliceFlag{
Name: "oci-worker-platform",
Usage: "override supported platforms for worker",
},
}
n := "oci-worker-rootless"
u := "enable rootless mode"
if system.RunningInUserNS() {
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,
},
flags...,
)
// TODO: allow multiple oci runtimes
}
func applyOCIFlags(c *cli.Context, cfg *config.Config) error {
if cfg.Workers.OCI.Snapshotter == "" {
cfg.Workers.OCI.Snapshotter = "auto"
}
if c.GlobalIsSet("oci-worker") {
boolOrAuto, err := parseBoolOrAuto(c.GlobalString("oci-worker"))
if err != nil {
return err
}
cfg.Workers.OCI.Enabled = boolOrAuto
}
labels, err := attrMap(c.GlobalStringSlice("oci-worker-labels"))
if err != nil {
return err
}
Initialise workers' label maps before assigning. Otherwise: panic: assignment to entry in nil map goroutine 1 [running]: main.applyOCIFlags(0xc4200e71e0, 0xc420400000, 0x0, 0x0) /go/src/github.com/moby/buildkit/cmd/buildkitd/main_oci_worker.go:97 +0x1ac main.ociWorkerInitializer(0xc4200e71e0, 0xc4204104e0, 0xc420400000, 0x43409b, 0x12, 0xc42026b0f8, 0x4337fc, 0xc420000180) /go/src/github.com/moby/buildkit/cmd/buildkitd/main_oci_worker.go:118 +0x50 main.newWorkerController(0xc4200e71e0, 0xc4204104e0, 0xc420400000, 0xc420422000, 0xe5dc54, 0x11) /go/src/github.com/moby/buildkit/cmd/buildkitd/main.go:520 +0x324 main.newController(0xc4200e71e0, 0xc420400000, 0x1c0, 0x0, 0x0) /go/src/github.com/moby/buildkit/cmd/buildkitd/main.go:489 +0xdc main.main.func3(0xc4200e71e0, 0x0, 0x0) /go/src/github.com/moby/buildkit/cmd/buildkitd/main.go:203 +0x3dd github.com/moby/buildkit/vendor/github.com/urfave/cli.HandleAction(0xcdd420, 0xe93e98, 0xc4200e71e0, 0xc4200e71e0, 0xc42026b888) /go/src/github.com/moby/buildkit/vendor/github.com/urfave/cli/app.go:502 +0xc8 github.com/moby/buildkit/vendor/github.com/urfave/cli.(*App).Run(0xc4201b6540, 0xc4200300a0, 0xa, 0xa, 0x0, 0x0) /go/src/github.com/moby/buildkit/vendor/github.com/urfave/cli/app.go:268 +0x60c main.main() /go/src/github.com/moby/buildkit/cmd/buildkitd/main.go:238 +0xc64 Also add some random labels to the integration sandbox (which I have confirmed is enough to trigger this issue before the fix). Signed-off-by: Ian Campbell <ijc@docker.com>
2018-09-03 10:31:03 +00:00
if cfg.Workers.OCI.Labels == nil {
cfg.Workers.OCI.Labels = make(map[string]string)
}
for k, v := range labels {
cfg.Workers.OCI.Labels[k] = v
}
if c.GlobalIsSet("oci-worker-snapshotter") {
cfg.Workers.OCI.Snapshotter = c.GlobalString("oci-worker-snapshotter")
}
if c.GlobalIsSet("rootless") || c.GlobalBool("rootless") {
cfg.Workers.OCI.Rootless = c.GlobalBool("rootless")
}
if c.GlobalIsSet("oci-worker-rootless") {
cfg.Workers.OCI.Rootless = c.GlobalBool("oci-worker-rootless")
}
if platforms := c.GlobalStringSlice("oci-worker-platform"); len(platforms) != 0 {
cfg.Workers.OCI.Platforms = platforms
}
return nil
}
func ociWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error) {
if err := applyOCIFlags(c, common.config); err != nil {
return nil, err
}
cfg := common.config.Workers.OCI
if (cfg.Enabled == nil && !validOCIBinary()) || (cfg.Enabled != nil && !*cfg.Enabled) {
return nil, nil
}
snFactory, err := snapshotterFactory(common.config.Root, cfg.Snapshotter)
if err != nil {
return nil, err
}
if cfg.Rootless {
logrus.Debugf("running in rootless mode")
}
opt, err := runc.NewWorkerOpt(common.config.Root, snFactory, cfg.Rootless, cfg.Labels)
if err != nil {
return nil, err
}
opt.SessionManager = common.sessionManager
opt.GCPolicy = getGCPolicy(cfg.GCPolicy, common.config.Root)
opt.ResolveOptionsFunc = resolverFunc(common.config)
if platformsStr := cfg.Platforms; len(platformsStr) != 0 {
platforms, err := parsePlatforms(platformsStr)
if err != nil {
return nil, errors.Wrap(err, "invalid platforms")
}
opt.Platforms = platforms
}
w, err := base.NewWorker(opt)
if err != nil {
return nil, err
}
return []worker.Worker{w}, nil
}
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"
}
}
snFactory := runc.SnapshotterFactory{
Name: name,
}
switch name {
case "native":
snFactory.New = native.NewSnapshotter
case "overlayfs": // not "overlay", for consistency with containerd snapshotter plugin ID.
snFactory.New = func(root string) (ctdsnapshot.Snapshotter, error) {
return overlay.NewSnapshotter(root)
}
default:
return snFactory, errors.Errorf("unknown snapshotter name: %q", name)
}
return snFactory, nil
}
func validOCIBinary() bool {
_, err := exec.LookPath("runc")
if err != nil {
logrus.Warnf("skipping oci worker, as runc does not exist")
return false
}
return true
}