forked from moby/buildkit
/
containerd.go
115 lines (95 loc) · 2.36 KB
/
containerd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package integration
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"
)
func init() {
// empty value stands for system-wide PATH, which would contain containerd v1.1.
paths := []string{"", "/opt/containerd-1.0/bin"}
for _, path := range paths {
register(&containerd{
containerd: filepath.Join(path, "containerd"),
containerdShim: filepath.Join(path, "containerd-shim"),
})
}
}
type containerd struct {
containerd string
containerdShim string
}
func (c *containerd) Name() string {
return c.containerd
}
func (c *containerd) New() (sb Sandbox, cl func() error, err error) {
if err := lookupBinary(c.containerd); err != nil {
return nil, nil, err
}
if err := lookupBinary(c.containerdShim); err != nil {
return nil, nil, err
}
if err := lookupBinary("buildkitd"); err != nil {
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")
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
}
cmd := exec.Command(c.containerd, "--config", configFile)
logs := map[string]*bytes.Buffer{}
if stop, err := startCmd(cmd, logs); err != nil {
return nil, nil, err
} else {
deferF.append(stop)
}
if err := waitUnix(address, 5*time.Second); err != nil {
return nil, nil, err
}
buildkitdSock, stop, err := runBuildkitd([]string{"buildkitd",
"--oci-worker=false",
"--containerd-worker=true",
"--containerd-worker-addr", address}, logs)
if err != nil {
return nil, nil, err
}
deferF.append(stop)
return &cdsandbox{address: address, sandbox: sandbox{address: buildkitdSock, logs: logs, cleanup: deferF}}, cl, nil
}
type cdsandbox struct {
sandbox
address string
}
func (s *cdsandbox) ContainerdAddress() string {
return s.address
}