Add implementation for containerd worker

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
docker-18.09
Derek McGowan 2017-07-25 17:23:11 -07:00
parent 9ce5053a8b
commit e460a3926c
3 changed files with 72 additions and 9 deletions

View File

@ -6,12 +6,11 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/containerd/containerd"
"github.com/moby/buildkit/worker/runcworker"
"github.com/moby/buildkit/worker/containerdworker"
"github.com/pkg/errors"
)
@ -33,12 +32,7 @@ func NewContainerd(root, address string) (*Controller, error) {
return nil, err
}
w, err := runcworker.New(filepath.Join(root, "runc"))
if err != nil {
return nil, err
}
opt.Worker = w
opt.Worker = containerdworker.New(client)
return NewController(*opt)
}

View File

@ -18,7 +18,8 @@ ARG CONTAINERD_VERSION
RUN git clone https://github.com/containerd/containerd.git "$GOPATH/src/github.com/containerd/containerd" \
&& cd "$GOPATH/src/github.com/containerd/containerd" \
&& git checkout -q "$CONTAINERD_VERSION" \
&& make bin/containerd
&& make bin/containerd \
&& make bin/containerd-shim
FROM gobuild-base AS unit-tests
COPY --from=runc /usr/bin/runc /usr/bin/runc

View File

@ -1 +1,69 @@
package containerdworker
import (
"io"
"github.com/containerd/containerd"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/oci"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type containerdWorker struct {
client *containerd.Client
}
func New(client *containerd.Client) worker.Worker {
return containerdWorker{
client: client,
}
}
func (w containerdWorker) Exec(ctx context.Context, meta worker.Meta, root cache.Mountable, mounts []worker.Mount, stdout, stderr io.WriteCloser) error {
id := identity.NewID()
spec, err := oci.GenerateSpec(ctx, meta, mounts)
if err != nil {
return err
}
rootMounts, err := root.Mount(ctx, false)
if err != nil {
return err
}
container, err := w.client.NewContainer(ctx, id,
containerd.WithSpec(spec),
)
if err != nil {
return err
}
defer container.Delete(ctx)
task, err := container.NewTask(ctx, containerd.Stdio, containerd.WithRootFS(rootMounts))
if err != nil {
return err
}
defer task.Delete(ctx)
// TODO: Configure bridge networking
// TODO: support sending signals
if err := task.Start(ctx); err != nil {
return err
}
status, err := task.Wait(ctx)
if err != nil {
return err
}
if status != 0 {
return errors.Errorf("process returned non-zero status: %d", status)
}
return nil
}