2017-12-15 07:00:13 +00:00
// +build linux,!no_containerd_worker
2017-06-08 19:00:31 +00:00
package main
import (
2017-11-21 08:08:36 +00:00
"os"
"strings"
ctd "github.com/containerd/containerd"
"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/containerd"
"github.com/sirupsen/logrus"
2017-06-08 19:00:31 +00:00
"github.com/urfave/cli"
)
2017-11-21 08:08:36 +00:00
func init ( ) {
registerWorkerInitializer (
workerInitializer {
fn : containerdWorkerInitializer ,
// 1 is less preferred than 0 (runcCtor)
priority : 1 ,
} ,
cli . StringFlag {
Name : "containerd-worker" ,
Usage : "enable containerd workers (true/false/auto)" ,
Value : "auto" ,
} ,
2017-06-08 19:00:31 +00:00
cli . StringFlag {
2017-11-21 08:08:36 +00:00
Name : "containerd-worker-addr" ,
2017-06-08 19:00:31 +00:00
Usage : "containerd socket" ,
Value : "/run/containerd/containerd.sock" ,
2017-12-19 09:34:34 +00:00
} ,
cli . StringSliceFlag {
Name : "containerd-worker-labels" ,
Usage : "user-specific annotation labels (com.example.foo=bar)" ,
} ,
)
2017-11-21 08:08:36 +00:00
// TODO(AkihiroSuda): allow using multiple snapshotters. should be useful for some applications that does not work with the default overlay snapshotter. e.g. mysql (docker/for-linux#72)",
2017-06-08 19:00:31 +00:00
}
2017-12-15 08:06:54 +00:00
func containerdWorkerInitializer ( c * cli . Context , common workerInitializerOpt ) ( [ ] worker . Worker , error ) {
2017-11-21 08:08:36 +00:00
socket := c . GlobalString ( "containerd-worker-addr" )
boolOrAuto , err := parseBoolOrAuto ( c . GlobalString ( "containerd-worker" ) )
if err != nil {
return nil , err
}
if ( boolOrAuto == nil && ! validContainerdSocket ( socket ) ) || ( boolOrAuto != nil && ! * boolOrAuto ) {
return nil , nil
}
2017-12-19 09:34:34 +00:00
labels , err := attrMap ( c . GlobalStringSlice ( "containerd-worker-labels" ) )
if err != nil {
return nil , err
}
opt , err := containerd . NewWorkerOpt ( common . root , socket , ctd . DefaultSnapshotter , labels )
2017-11-21 08:08:36 +00:00
if err != nil {
return nil , err
}
opt . SessionManager = common . sessionManager
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-11-21 08:08:36 +00:00
}
2017-06-08 19:00:31 +00:00
2017-11-21 08:08:36 +00:00
func validContainerdSocket ( socket string ) bool {
if strings . HasPrefix ( socket , "tcp://" ) {
// FIXME(AkihiroSuda): prohibit tcp?
return true
}
socketPath := strings . TrimPrefix ( socket , "unix://" )
if _ , err := os . Stat ( socketPath ) ; os . IsNotExist ( err ) {
// FIXME(AkihiroSuda): add more conditions
logrus . Warnf ( "skipping containerd worker, as %q does not exist" , socketPath )
return false
}
// TODO: actually dial and call introspection API
return true
2017-06-08 19:00:31 +00:00
}