2017-06-08 19:00:31 +00:00
|
|
|
// +build containerd
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-06-13 21:51:06 +00:00
|
|
|
"github.com/containerd/containerd"
|
2017-07-26 00:23:11 +00:00
|
|
|
"github.com/moby/buildkit/worker/containerdworker"
|
2017-06-08 19:00:31 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewContainerd(root, address string) (*Controller, error) {
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create %s", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: take lock to make sure there are no duplicates
|
2017-06-13 21:51:06 +00:00
|
|
|
client, err := containerd.New(address, containerd.WithDefaultNamespace("buildkit"))
|
2017-06-08 19:00:31 +00:00
|
|
|
if err != nil {
|
2017-06-13 21:51:06 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", address)
|
2017-06-08 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 20:39:00 +00:00
|
|
|
pd := newContainerdPullDeps(client)
|
2017-06-08 19:00:31 +00:00
|
|
|
|
|
|
|
opt, err := defaultControllerOpts(root, *pd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-26 00:23:11 +00:00
|
|
|
opt.Worker = containerdworker.New(client)
|
2017-06-08 19:00:31 +00:00
|
|
|
|
|
|
|
return NewController(*opt)
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:39:00 +00:00
|
|
|
func newContainerdPullDeps(client *containerd.Client) *pullDeps {
|
2017-07-10 20:03:38 +00:00
|
|
|
diff := client.DiffService()
|
2017-06-08 19:00:31 +00:00
|
|
|
return &pullDeps{
|
2017-09-07 08:26:14 +00:00
|
|
|
Snapshotter: client.SnapshotService(containerd.DefaultSnapshotter),
|
2017-06-13 21:51:06 +00:00
|
|
|
ContentStore: client.ContentStore(),
|
2017-07-10 20:03:38 +00:00
|
|
|
Applier: diff,
|
|
|
|
Differ: diff,
|
|
|
|
Images: client.ImageService(),
|
2017-06-13 21:51:06 +00:00
|
|
|
}
|
2017-06-08 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dialer(address string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
address = strings.TrimPrefix(address, "unix://")
|
|
|
|
return net.DialTimeout("unix", address, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialAddress(address string) string {
|
|
|
|
return fmt.Sprintf("unix://%s", address)
|
|
|
|
}
|