buildkit/util/testutil/integration/oci.go

82 lines
2.1 KiB
Go
Raw Normal View History

package integration
import (
"fmt"
"log"
"os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func InitOCIWorker() {
Register(&oci{})
// the rootless uid is defined in Dockerfile
if s := os.Getenv("BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR"); s != "" {
var uid, gid int
if _, err := fmt.Sscanf(s, "%d:%d", &uid, &gid); err != nil {
logrus.Fatalf("unexpected BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR: %q", s)
}
if rootlessSupported(uid) {
Register(&oci{uid: uid, gid: gid})
}
}
if s := os.Getenv("BUILDKIT_INTEGRATION_SNAPSHOTTER"); s != "" {
Register(&oci{snapshotter: s})
}
}
type oci struct {
uid int
gid int
snapshotter string
}
func (s *oci) Name() string {
if s.uid != 0 {
return "oci-rootless"
}
if s.snapshotter != "" {
return fmt.Sprintf("oci-snapshotter-%s", s.snapshotter)
}
return "oci"
}
func (s *oci) New(cfg *BackendConfig) (Backend, func() error, error) {
if err := lookupBinary("buildkitd"); err != nil {
return nil, nil, err
}
if err := requireRoot(); err != nil {
return nil, nil, 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
// Include use of --oci-worker-labels to trigger https://github.com/moby/buildkit/pull/603
buildkitdArgs := []string{"buildkitd", "--oci-worker=true", "--containerd-worker=false", "--oci-worker-gc=false", "--oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true"}
if s.snapshotter != "" {
buildkitdArgs = append(buildkitdArgs,
fmt.Sprintf("--oci-worker-snapshotter=%s", s.snapshotter))
}
if s.uid != 0 {
if s.gid == 0 {
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", s.uid, s.gid)
}
// TODO: make sure the user exists and subuid/subgid are configured.
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", s.uid), "-i", "--", "exec", "rootlesskit"}, buildkitdArgs...)
}
buildkitdSock, stop, err := runBuildkitd(cfg, buildkitdArgs, cfg.Logs, s.uid, s.gid, nil)
if err != nil {
printLogs(cfg.Logs, log.Println)
return nil, nil, err
}
return backend{
address: buildkitdSock,
rootless: s.uid != 0,
snapshotter: s.snapshotter,
}, stop, nil
}