2017-11-27 22:24:29 +00:00
package integration
import (
"bytes"
2018-05-08 06:26:19 +00:00
"fmt"
2017-11-27 22:24:29 +00:00
"io/ioutil"
2019-07-12 17:22:25 +00:00
"log"
2017-11-27 22:24:29 +00:00
"os"
"os/exec"
"path/filepath"
2018-07-06 06:16:38 +00:00
"strings"
2017-11-27 22:24:29 +00:00
"time"
2018-07-06 06:16:38 +00:00
"github.com/pkg/errors"
2017-11-27 22:24:29 +00:00
)
2019-03-22 06:49:08 +00:00
func InitContainerdWorker ( ) {
Register ( & containerd {
2021-04-09 06:34:45 +00:00
name : "containerd" ,
containerd : "containerd" ,
2018-07-06 06:16:38 +00:00
} )
2019-10-18 07:36:14 +00:00
// defined in Dockerfile
2019-08-01 22:38:24 +00:00
// e.g. `containerd-1.1=/opt/containerd-1.1/bin,containerd-42.0=/opt/containerd-42.0/bin`
2018-07-06 06:16:38 +00:00
if s := os . Getenv ( "BUILDKIT_INTEGRATION_CONTAINERD_EXTRA" ) ; s != "" {
entries := strings . Split ( s , "," )
for _ , entry := range entries {
pair := strings . Split ( strings . TrimSpace ( entry ) , "=" )
if len ( pair ) != 2 {
panic ( errors . Errorf ( "unexpected BUILDKIT_INTEGRATION_CONTAINERD_EXTRA: %q" , s ) )
}
name , bin := pair [ 0 ] , pair [ 1 ]
2019-03-22 06:49:08 +00:00
Register ( & containerd {
2021-04-09 06:34:45 +00:00
name : name ,
containerd : filepath . Join ( bin , "containerd" ) ,
// override PATH to make sure that the expected version of the shim binary is used
extraEnv : [ ] string { fmt . Sprintf ( "PATH=%s:%s" , bin , os . Getenv ( "PATH" ) ) } ,
2018-07-06 06:16:38 +00:00
} )
}
2018-05-08 06:26:19 +00:00
}
2020-08-24 06:40:04 +00:00
2020-09-03 05:40:57 +00:00
if s := os . Getenv ( "BUILDKIT_INTEGRATION_SNAPSHOTTER" ) ; s != "" {
2020-08-24 06:40:04 +00:00
Register ( & containerd {
2021-04-09 06:34:45 +00:00
name : fmt . Sprintf ( "containerd-snapshotter-%s" , s ) ,
containerd : "containerd" ,
snapshotter : s ,
2020-08-24 06:40:04 +00:00
} )
}
2017-11-27 22:24:29 +00:00
}
type containerd struct {
2021-04-09 06:34:45 +00:00
name string
containerd string
snapshotter string
extraEnv [ ] string // e.g. "PATH=/opt/containerd-1.4/bin:/usr/bin:..."
2017-11-27 22:24:29 +00:00
}
func ( c * containerd ) Name ( ) string {
2018-07-06 06:16:38 +00:00
return c . name
2017-11-27 22:24:29 +00:00
}
2019-08-23 21:21:34 +00:00
func ( c * containerd ) New ( cfg * BackendConfig ) ( b Backend , cl func ( ) error , err error ) {
2018-05-08 06:26:19 +00:00
if err := lookupBinary ( c . containerd ) ; err != nil {
return nil , nil , err
}
2017-12-18 07:33:02 +00:00
if err := lookupBinary ( "buildkitd" ) ; err != nil {
2017-11-27 22:24:29 +00:00
return nil , nil , err
}
if err := requireRoot ( ) ; err != nil {
return nil , nil , err
}
deferF := & multiCloser { }
cl = deferF . F ( )
defer func ( ) {
if err != nil {
deferF . F ( ) ( )
cl = nil
}
} ( )
tmpdir , err := ioutil . TempDir ( "" , "bktest_containerd" )
if err != nil {
return nil , nil , err
}
deferF . append ( func ( ) error { return os . RemoveAll ( tmpdir ) } )
address := filepath . Join ( tmpdir , "containerd.sock" )
2018-05-08 06:26:19 +00:00
config := fmt . Sprintf ( ` root = % q
state = % q
2018-08-23 08:57:51 +00:00
# CRI plugins listens on 10010 / tcp for stream server .
# We disable CRI plugin so that multiple instance can run simultaneously .
disabled_plugins = [ "cri" ]
2018-05-08 06:26:19 +00:00
[ grpc ]
address = % q
[ debug ]
level = "debug"
2018-10-17 01:59:44 +00:00
address = % q
2021-04-09 06:34:45 +00:00
` , filepath . Join ( tmpdir , "root" ) , filepath . Join ( tmpdir , "state" ) , address , filepath . Join ( tmpdir , "debug.sock" ) )
2020-08-24 06:40:04 +00:00
var snBuildkitdArgs [ ] string
2020-09-03 05:40:57 +00:00
if c . snapshotter != "" {
snBuildkitdArgs = append ( snBuildkitdArgs ,
fmt . Sprintf ( "--containerd-worker-snapshotter=%s" , c . snapshotter ) )
if c . snapshotter == "stargz" {
snPath , snCl , err := runStargzSnapshotter ( cfg )
if err != nil {
return nil , nil , err
}
deferF . append ( snCl )
config = fmt . Sprintf ( ` % s
2020-08-24 06:40:04 +00:00
[ proxy_plugins ]
[ proxy_plugins . stargz ]
type = "snapshot"
address = % q
` , config , snPath )
2020-09-03 05:40:57 +00:00
}
2020-08-24 06:40:04 +00:00
}
2018-05-08 06:26:19 +00:00
configFile := filepath . Join ( tmpdir , "config.toml" )
if err := ioutil . WriteFile ( configFile , [ ] byte ( config ) , 0644 ) ; err != nil {
return nil , nil , err
}
2017-11-27 22:24:29 +00:00
2018-05-08 06:26:19 +00:00
cmd := exec . Command ( c . containerd , "--config" , configFile )
2021-04-09 06:34:45 +00:00
cmd . Env = append ( os . Environ ( ) , c . extraEnv ... )
2017-11-27 22:24:29 +00:00
2019-08-23 21:21:34 +00:00
ctdStop , err := startCmd ( cmd , cfg . Logs )
2018-08-23 08:43:32 +00:00
if err != nil {
2017-11-27 22:24:29 +00:00
return nil , nil , err
}
2020-08-24 06:40:04 +00:00
if err := waitUnix ( address , 10 * time . Second ) ; err != nil {
2018-08-23 08:43:32 +00:00
ctdStop ( )
2019-08-23 21:21:34 +00:00
return nil , nil , errors . Wrapf ( err , "containerd did not start up: %s" , formatLogs ( cfg . Logs ) )
2017-11-27 22:24:29 +00:00
}
2018-08-23 08:43:32 +00:00
deferF . append ( ctdStop )
2017-11-27 22:24:29 +00:00
2020-08-24 06:40:04 +00:00
buildkitdArgs := append ( [ ] string { "buildkitd" ,
2017-12-15 07:00:13 +00:00
"--oci-worker=false" ,
2019-03-11 04:04:50 +00:00
"--containerd-worker-gc=false" ,
2017-12-15 07:00:13 +00:00
"--containerd-worker=true" ,
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
"--containerd-worker-addr" , address ,
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true" , // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
2020-08-24 06:40:04 +00:00
} , snBuildkitdArgs ... )
2018-09-11 00:08:07 +00:00
2021-04-09 06:34:45 +00:00
buildkitdSock , stop , err := runBuildkitd ( cfg , buildkitdArgs , cfg . Logs , 0 , 0 , c . extraEnv )
2017-11-27 22:24:29 +00:00
if err != nil {
2019-08-23 21:21:34 +00:00
printLogs ( cfg . Logs , log . Println )
2017-11-27 22:24:29 +00:00
return nil , nil , err
}
deferF . append ( stop )
2020-06-21 01:35:25 +00:00
return backend {
address : buildkitdSock ,
2019-08-23 21:21:34 +00:00
containerdAddress : address ,
2020-06-21 01:35:25 +00:00
rootless : false ,
2020-09-03 05:40:57 +00:00
snapshotter : c . snapshotter ,
2020-06-21 01:35:25 +00:00
} , cl , nil
2017-12-01 00:56:58 +00:00
}
2018-08-23 08:43:32 +00:00
func formatLogs ( m map [ string ] * bytes . Buffer ) string {
var ss [ ] string
for k , b := range m {
if b != nil {
ss = append ( ss , fmt . Sprintf ( "%q:%q" , k , b . String ( ) ) )
}
}
return strings . Join ( ss , "," )
}