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"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-07-06 06:16:38 +00:00
|
|
|
register(&containerd{
|
|
|
|
name: "containerd",
|
|
|
|
containerd: "containerd",
|
|
|
|
containerdShim: "containerd-shim",
|
|
|
|
})
|
|
|
|
// defined in hack/dockerfiles/test.Dockerfile.
|
|
|
|
// e.g. `containerd-1.0=/opt/containerd-1.0/bin,containerd-42.0=/opt/containerd-42.0/bin`
|
|
|
|
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]
|
|
|
|
register(&containerd{
|
|
|
|
name: name,
|
|
|
|
containerd: filepath.Join(bin, "containerd"),
|
|
|
|
containerdShim: filepath.Join(bin, "containerd-shim"),
|
|
|
|
})
|
|
|
|
}
|
2018-05-08 06:26:19 +00:00
|
|
|
}
|
2017-11-27 22:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type containerd struct {
|
2018-07-06 06:16:38 +00:00
|
|
|
name string
|
2018-05-08 06:26:19 +00:00
|
|
|
containerd string
|
|
|
|
containerdShim string
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerd) New() (sb Sandbox, cl func() error, err error) {
|
2018-05-08 06:26:19 +00:00
|
|
|
if err := lookupBinary(c.containerd); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := lookupBinary(c.containerdShim); err != nil {
|
2017-11-27 22:24:29 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
[grpc]
|
|
|
|
address = %q
|
|
|
|
|
|
|
|
[debug]
|
|
|
|
level = "debug"
|
|
|
|
|
|
|
|
[plugins]
|
|
|
|
[plugins.linux]
|
|
|
|
shim = %q
|
|
|
|
`, filepath.Join(tmpdir, "root"), filepath.Join(tmpdir, "state"), address, c.containerdShim)
|
|
|
|
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)
|
2017-11-27 22:24:29 +00:00
|
|
|
|
|
|
|
logs := map[string]*bytes.Buffer{}
|
|
|
|
|
2018-08-23 08:43:32 +00:00
|
|
|
ctdStop, err := startCmd(cmd, logs)
|
|
|
|
if err != nil {
|
2017-11-27 22:24:29 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := waitUnix(address, 5*time.Second); err != nil {
|
2018-08-23 08:43:32 +00:00
|
|
|
ctdStop()
|
|
|
|
return nil, nil, errors.Wrapf(err, "containerd did not start up: %s", formatLogs(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
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitdSock, stop, err := runBuildkitd([]string{"buildkitd",
|
2017-12-15 07:00:13 +00:00
|
|
|
"--oci-worker=false",
|
|
|
|
"--containerd-worker=true",
|
2018-07-04 09:18:00 +00:00
|
|
|
"--containerd-worker-addr", address}, logs, 0, 0)
|
2017-11-27 22:24:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
deferF.append(stop)
|
|
|
|
|
2018-07-04 09:18:00 +00:00
|
|
|
return &cdsandbox{address: address, sandbox: sandbox{address: buildkitdSock, logs: logs, cleanup: deferF, rootless: false}}, 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, ",")
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:56:58 +00:00
|
|
|
type cdsandbox struct {
|
|
|
|
sandbox
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cdsandbox) ContainerdAddress() string {
|
|
|
|
return s.address
|
2017-11-27 22:24:29 +00:00
|
|
|
}
|