2017-12-15 07:00:13 +00:00
// +build linux,!no_oci_worker
2017-06-08 00:54:29 +00:00
package main
import (
2020-10-28 14:10:14 +00:00
"context"
2020-05-17 01:16:43 +00:00
"os"
2017-11-21 08:08:36 +00:00
"os/exec"
2020-09-02 09:52:29 +00:00
"path/filepath"
2018-08-29 21:00:25 +00:00
"strconv"
2020-08-17 09:34:33 +00:00
"time"
2017-11-21 08:08:36 +00:00
2020-02-28 11:17:42 +00:00
fuseoverlayfs "github.com/AkihiroSuda/containerd-fuse-overlayfs"
2020-08-17 09:34:33 +00:00
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/pkg/dialer"
2020-10-28 14:10:14 +00:00
"github.com/containerd/containerd/remotes/docker"
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"
2020-08-17 09:34:33 +00:00
snproxy "github.com/containerd/containerd/snapshots/proxy"
2020-06-15 10:51:20 +00:00
"github.com/containerd/containerd/sys"
2020-09-02 09:52:29 +00:00
remotesn "github.com/containerd/stargz-snapshotter/snapshot"
"github.com/containerd/stargz-snapshotter/stargz"
2020-10-28 14:10:14 +00:00
sgzsource "github.com/containerd/stargz-snapshotter/stargz/source"
2018-08-29 21:00:25 +00:00
"github.com/moby/buildkit/cmd/buildkitd/config"
2019-01-03 10:12:12 +00:00
"github.com/moby/buildkit/executor/oci"
2019-08-06 05:23:23 +00:00
"github.com/moby/buildkit/util/network/cniprovider"
"github.com/moby/buildkit/util/network/netproviders"
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-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"
2020-08-17 09:34:33 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
2017-06-08 00:54:29 +00:00
)
2017-11-21 08:08:36 +00:00
func init ( ) {
2019-03-20 23:27:05 +00:00
defaultConf , _ , _ := defaultConf ( )
2018-08-29 21:00:25 +00:00
enabledValue := func ( b * bool ) string {
if b == nil {
return "auto"
}
return strconv . FormatBool ( * b )
}
if defaultConf . Workers . OCI . Snapshotter == "" {
defaultConf . Workers . OCI . Snapshotter = "auto"
}
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)" ,
2018-08-29 21:00:25 +00:00
Value : enabledValue ( defaultConf . Workers . OCI . Enabled ) ,
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" ,
2020-08-17 09:34:33 +00:00
Usage : "name of snapshotter (overlayfs, native, etc.)" ,
2018-08-29 21:00:25 +00:00
Value : defaultConf . Workers . OCI . Snapshotter ,
2018-05-30 02:49:43 +00:00
} ,
2020-08-17 09:34:33 +00:00
cli . StringFlag {
Name : "oci-worker-proxy-snapshotter-path" ,
Usage : "address of proxy snapshotter socket (do not include 'unix://' prefix)" ,
} ,
2018-06-22 02:06:12 +00:00
cli . StringSliceFlag {
Name : "oci-worker-platform" ,
Usage : "override supported platforms for worker" ,
} ,
2019-07-10 21:42:30 +00:00
cli . StringFlag {
Name : "oci-worker-net" ,
Usage : "worker network type (auto, cni or host)" ,
Value : defaultConf . Workers . OCI . NetworkConfig . Mode ,
} ,
cli . StringFlag {
Name : "oci-cni-config-path" ,
Usage : "path of cni config file" ,
Value : defaultConf . Workers . OCI . NetworkConfig . CNIConfigPath ,
} ,
cli . StringFlag {
Name : "oci-cni-binary-dir" ,
Usage : "path of cni binary files" ,
Value : defaultConf . Workers . OCI . NetworkConfig . CNIBinaryPath ,
} ,
2019-12-09 06:15:55 +00:00
cli . StringFlag {
Name : "oci-worker-binary" ,
Usage : "name of specified oci worker binary" ,
Value : defaultConf . Workers . OCI . Binary ,
} ,
2018-05-30 02:49:43 +00:00
}
n := "oci-worker-rootless"
u := "enable rootless mode"
2020-06-15 10:51:20 +00:00
if sys . 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 ,
} )
}
2019-01-03 10:12:12 +00:00
flags = append ( flags , cli . BoolFlag {
Name : "oci-worker-no-process-sandbox" ,
Usage : "use the host PID namespace and procfs (WARNING: allows build containers to kill (and potentially ptrace) an arbitrary process in the host namespace)" ,
} )
2019-01-23 01:28:43 +00:00
if defaultConf . Workers . OCI . GC == nil || * defaultConf . Workers . OCI . GC {
flags = append ( flags , cli . BoolTFlag {
Name : "oci-worker-gc" ,
Usage : "Enable automatic garbage collection on worker" ,
} )
} else {
flags = append ( flags , cli . BoolFlag {
Name : "oci-worker-gc" ,
Usage : "Enable automatic garbage collection on worker" ,
} )
}
flags = append ( flags , cli . Int64Flag {
Name : "oci-worker-gc-keepstorage" ,
Usage : "Amount of storage GC keep locally (MB)" ,
Value : func ( ) int64 {
if defaultConf . Workers . OCI . GCKeepStorage != 0 {
return defaultConf . Workers . OCI . GCKeepStorage / 1e6
}
return config . DetectDefaultGCCap ( defaultConf . Root ) / 1e6
} ( ) ,
Hidden : len ( defaultConf . Workers . OCI . GCPolicy ) != 0 ,
} )
2019-01-03 10:12:12 +00:00
2018-05-30 02:49:43 +00:00
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
}
2018-08-29 21:00:25 +00:00
func applyOCIFlags ( c * cli . Context , cfg * config . Config ) error {
if cfg . Workers . OCI . Snapshotter == "" {
cfg . Workers . OCI . Snapshotter = "auto"
2017-11-21 08:08:36 +00:00
}
2018-08-29 21:00:25 +00:00
if c . GlobalIsSet ( "oci-worker" ) {
boolOrAuto , err := parseBoolOrAuto ( c . GlobalString ( "oci-worker" ) )
if err != nil {
return err
}
cfg . Workers . OCI . Enabled = boolOrAuto
2017-11-21 08:08:36 +00:00
}
2018-08-29 21:00:25 +00:00
2017-12-19 09:34:34 +00:00
labels , err := attrMap ( c . GlobalStringSlice ( "oci-worker-labels" ) )
if err != nil {
2018-08-29 21:00:25 +00:00
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 )
}
2018-08-29 21:00:25 +00:00
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" ) {
2020-06-15 10:51:20 +00:00
if ! sys . RunningInUserNS ( ) || os . Geteuid ( ) > 0 {
2020-05-17 01:16:43 +00:00
return errors . New ( "rootless mode requires to be executed as the mapped root in a user namespace; you may use RootlessKit for setting up the namespace" )
}
2018-08-29 21:00:25 +00:00
cfg . Workers . OCI . Rootless = c . GlobalBool ( "oci-worker-rootless" )
}
2019-01-03 10:12:12 +00:00
if c . GlobalIsSet ( "oci-worker-no-process-sandbox" ) {
cfg . Workers . OCI . NoProcessSandbox = c . GlobalBool ( "oci-worker-no-process-sandbox" )
}
2018-08-29 21:00:25 +00:00
if platforms := c . GlobalStringSlice ( "oci-worker-platform" ) ; len ( platforms ) != 0 {
cfg . Workers . OCI . Platforms = platforms
}
2019-01-23 01:28:43 +00:00
if c . GlobalIsSet ( "oci-worker-gc" ) {
v := c . GlobalBool ( "oci-worker-gc" )
cfg . Workers . OCI . GC = & v
}
if c . GlobalIsSet ( "oci-worker-gc-keepstorage" ) {
cfg . Workers . OCI . GCKeepStorage = c . GlobalInt64 ( "oci-worker-gc-keepstorage" ) * 1e6
}
2019-07-10 21:42:30 +00:00
if c . GlobalIsSet ( "oci-worker-net" ) {
cfg . Workers . OCI . NetworkConfig . Mode = c . GlobalString ( "oci-worker-net" )
}
if c . GlobalIsSet ( "oci-cni-config-path" ) {
cfg . Workers . OCI . NetworkConfig . CNIConfigPath = c . GlobalString ( "oci-cni-worker-path" )
}
if c . GlobalIsSet ( "oci-cni-binary-dir" ) {
cfg . Workers . OCI . NetworkConfig . CNIBinaryPath = c . GlobalString ( "oci-cni-binary-dir" )
}
2019-12-09 06:15:55 +00:00
if c . GlobalIsSet ( "oci-worker-binary" ) {
cfg . Workers . OCI . Binary = c . GlobalString ( "oci-worker-binary" )
}
2020-08-17 09:34:33 +00:00
if c . GlobalIsSet ( "oci-worker-proxy-snapshotter-path" ) {
cfg . Workers . OCI . ProxySnapshotterPath = c . GlobalString ( "oci-worker-proxy-snapshotter-path" )
}
2018-08-29 21:00:25 +00:00
return nil
}
func ociWorkerInitializer ( c * cli . Context , common workerInitializerOpt ) ( [ ] worker . Worker , error ) {
if err := applyOCIFlags ( c , common . config ) ; err != nil {
2017-12-19 09:34:34 +00:00
return nil , err
}
2018-08-29 21:00:25 +00:00
cfg := common . config . Workers . OCI
if ( cfg . Enabled == nil && ! validOCIBinary ( ) ) || ( cfg . Enabled != nil && ! * cfg . Enabled ) {
return nil , nil
}
2019-03-20 06:40:24 +00:00
// TODO: this should never change the existing state dir
idmapping , err := parseIdentityMapping ( cfg . UserRemapUnsupported )
if err != nil {
return nil , err
}
2020-10-28 14:10:14 +00:00
hosts := resolverFunc ( common . config )
snFactory , err := snapshotterFactory ( common . config . Root , cfg , hosts )
2018-02-26 08:17:33 +00:00
if err != nil {
return nil , err
}
2018-08-29 21:00:25 +00:00
if cfg . Rootless {
2018-05-30 02:49:43 +00:00
logrus . Debugf ( "running in rootless mode" )
2019-07-10 23:55:04 +00:00
if common . config . Workers . OCI . NetworkConfig . Mode == "auto" {
common . config . Workers . OCI . NetworkConfig . Mode = "host"
}
2018-05-30 02:49:43 +00:00
}
2019-01-03 10:12:12 +00:00
processMode := oci . ProcessSandbox
if cfg . NoProcessSandbox {
logrus . Warn ( "NoProcessSandbox is enabled. Note that NoProcessSandbox allows build containers to kill (and potentially ptrace) an arbitrary process in the BuildKit host namespace. NoProcessSandbox should be enabled only when the BuildKit is running in a container as an unprivileged user." )
if ! cfg . Rootless {
return nil , errors . New ( "can't enable NoProcessSandbox without Rootless" )
}
processMode = oci . NoProcessSandbox
}
2019-06-06 01:46:52 +00:00
dns := getDNSConfig ( common . config . DNS )
2019-08-06 05:23:23 +00:00
nc := netproviders . Opt {
Mode : common . config . Workers . OCI . NetworkConfig . Mode ,
CNI : cniprovider . Opt {
Root : common . config . Root ,
ConfigPath : common . config . Workers . OCI . CNIConfigPath ,
BinaryDir : common . config . Workers . OCI . CNIBinaryPath ,
} ,
2019-07-10 21:42:30 +00:00
}
2019-12-09 06:15:55 +00:00
opt , err := runc . NewWorkerOpt ( common . config . Root , snFactory , cfg . Rootless , processMode , cfg . Labels , idmapping , nc , dns , cfg . Binary )
2017-11-21 08:08:36 +00:00
if err != nil {
return nil , err
}
2019-01-23 01:28:43 +00:00
opt . GCPolicy = getGCPolicy ( cfg . GCConfig , common . config . Root )
2020-10-28 14:10:14 +00:00
opt . RegistryHosts = hosts
2018-08-29 21:00:25 +00:00
if platformsStr := cfg . Platforms ; len ( platformsStr ) != 0 {
2018-06-22 02:06:12 +00:00
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
}
2020-10-28 14:10:14 +00:00
func snapshotterFactory ( commonRoot string , cfg config . OCIConfig , hosts docker . RegistryHosts ) ( runc . SnapshotterFactory , error ) {
2020-09-02 09:52:29 +00:00
var (
name = cfg . Snapshotter
address = cfg . ProxySnapshotterPath
)
2020-08-17 09:34:33 +00:00
if address != "" {
snFactory := runc . SnapshotterFactory {
Name : name ,
}
if _ , err := os . Stat ( address ) ; os . IsNotExist ( err ) {
return snFactory , errors . Wrapf ( err , "snapshotter doesn't exist on %q (Do not include 'unix://' prefix)" , address )
}
snFactory . New = func ( root string ) ( ctdsnapshot . Snapshotter , error ) {
backoffConfig := backoff . DefaultConfig
backoffConfig . MaxDelay = 3 * time . Second
connParams := grpc . ConnectParams {
Backoff : backoffConfig ,
}
gopts := [ ] grpc . DialOption {
grpc . WithInsecure ( ) ,
grpc . WithConnectParams ( connParams ) ,
grpc . WithContextDialer ( dialer . ContextDialer ) ,
grpc . WithDefaultCallOptions ( grpc . MaxCallRecvMsgSize ( defaults . DefaultMaxRecvMsgSize ) ) ,
grpc . WithDefaultCallOptions ( grpc . MaxCallSendMsgSize ( defaults . DefaultMaxSendMsgSize ) ) ,
}
conn , err := grpc . Dial ( dialer . DialAddress ( address ) , gopts ... )
if err != nil {
return nil , errors . Wrapf ( err , "failed to dial %q" , address )
}
return snproxy . NewSnapshotter ( snapshotsapi . NewSnapshotsClient ( conn ) , name ) , nil
}
return snFactory , nil
}
2018-06-05 08:38:16 +00:00
if name == "auto" {
if err := overlay . Supported ( commonRoot ) ; err == nil {
name = "overlayfs"
} else {
2020-02-28 11:17:42 +00:00
logrus . Debugf ( "auto snapshotter: overlayfs is not available for %s, trying fuse-overlayfs: %v" , commonRoot , err )
if err2 := fuseoverlayfs . Supported ( commonRoot ) ; err2 == nil {
name = "fuse-overlayfs"
} else {
logrus . Debugf ( "auto snapshotter: fuse-overlayfs is not available for %s, falling back to native: %v" , commonRoot , err2 )
name = "native"
}
2018-06-05 08:38:16 +00:00
}
2020-02-28 11:17:42 +00:00
logrus . Infof ( "auto snapshotter: using %s" , name )
2018-06-05 08:38:16 +00:00
}
2020-02-28 11:17:42 +00:00
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 ) {
2019-05-14 21:35:13 +00:00
return overlay . NewSnapshotter ( root , overlay . AsynchronousRemove )
2018-04-03 09:37:16 +00:00
}
2020-02-28 11:17:42 +00:00
case "fuse-overlayfs" :
snFactory . New = func ( root string ) ( ctdsnapshot . Snapshotter , error ) {
// no Opt (AsynchronousRemove is untested for fuse-overlayfs)
return fuseoverlayfs . NewSnapshotter ( root )
}
2020-09-02 09:52:29 +00:00
case "stargz" :
2020-10-28 14:10:14 +00:00
// Pass the registry configuration to stargz snapshotter
sgzhosts := func ( host string ) ( [ ] docker . RegistryHost , error ) {
base , err := hosts ( host )
if err != nil {
return nil , err
}
for i := range base {
if base [ i ] . Authorizer == nil {
// Default authorizer that don't fetch creds via session
// TODO(ktock): use session-based authorizer
base [ i ] . Authorizer = docker . NewDockerAuthorizer (
docker . WithAuthClient ( base [ i ] . Client ) )
}
}
return base , nil
}
2020-09-02 09:52:29 +00:00
snFactory . New = func ( root string ) ( ctdsnapshot . Snapshotter , error ) {
fs , err := stargz . NewFilesystem ( filepath . Join ( root , "stargz" ) ,
2020-10-28 14:10:14 +00:00
cfg . StargzSnapshotterConfig ,
stargz . WithGetSources (
// provides source info based on the registry config and
// default labels.
sgzsource . FromDefaultLabels ( sgzhosts ) ,
) ,
)
2020-09-02 09:52:29 +00:00
if err != nil {
return nil , err
}
2020-10-28 14:10:14 +00:00
return remotesn . NewSnapshotter ( context . Background ( ) ,
filepath . Join ( root , "snapshotter" ) ,
2020-09-02 09:52:29 +00:00
fs , remotesn . AsynchronousRemove )
}
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" )
2018-10-02 17:03:32 +00:00
_ , err1 := exec . LookPath ( "buildkit-runc" )
if err != nil && err1 != nil {
2017-11-21 08:08:36 +00:00
logrus . Warnf ( "skipping oci worker, as runc does not exist" )
return false
}
return true
2017-06-08 00:54:29 +00:00
}