59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// +build containerd
|
|
|
|
package control
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/containerd/containerd"
|
|
"github.com/moby/buildkit/worker/containerdworker"
|
|
"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
|
|
client, err := containerd.New(address, containerd.WithDefaultNamespace("buildkit"))
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", address)
|
|
}
|
|
|
|
pd := newContainerdPullDeps(client)
|
|
|
|
opt, err := defaultControllerOpts(root, *pd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
opt.Worker = containerdworker.New(client)
|
|
|
|
return NewController(*opt)
|
|
}
|
|
|
|
func newContainerdPullDeps(client *containerd.Client) *pullDeps {
|
|
diff := client.DiffService()
|
|
return &pullDeps{
|
|
Snapshotter: client.SnapshotService(containerd.DefaultSnapshotter),
|
|
ContentStore: client.ContentStore(),
|
|
Applier: diff,
|
|
Differ: diff,
|
|
Images: client.ImageService(),
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|