2018-02-05 16:05:59 -05:00
|
|
|
package containerd // import "github.com/docker/docker/plugin/executor/containerd"
|
2017-07-14 16:45:32 -04:00
|
|
|
|
|
|
|
import (
|
2017-09-22 09:52:41 -04:00
|
|
|
"context"
|
2017-07-14 16:45:32 -04:00
|
|
|
"io"
|
2017-09-22 09:52:41 -04:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2017-07-14 16:45:32 -04:00
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
"github.com/containerd/containerd/cio"
|
|
|
|
"github.com/containerd/containerd/linux/runctypes"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-07-14 16:45:32 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/pkg/errors"
|
2017-09-22 09:52:41 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-14 16:45:32 -04:00
|
|
|
)
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// PluginNamespace is the name used for the plugins namespace
|
2017-12-15 17:48:31 -05:00
|
|
|
var PluginNamespace = "plugins.moby"
|
2017-09-22 09:52:41 -04:00
|
|
|
|
2017-07-14 16:45:32 -04:00
|
|
|
// ExitHandler represents an object that is called when the exit event is received from containerd
|
|
|
|
type ExitHandler interface {
|
|
|
|
HandleExitEvent(id string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new containerd plugin executor
|
2017-09-22 09:52:41 -04:00
|
|
|
func New(rootDir string, remote libcontainerd.Remote, exitHandler ExitHandler) (*Executor, error) {
|
|
|
|
e := &Executor{
|
|
|
|
rootDir: rootDir,
|
|
|
|
exitHandler: exitHandler,
|
|
|
|
}
|
|
|
|
client, err := remote.NewClient(PluginNamespace, e)
|
2017-07-14 16:45:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error creating containerd exec client")
|
|
|
|
}
|
|
|
|
e.client = client
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor is the containerd client implementation of a plugin executor
|
|
|
|
type Executor struct {
|
2017-09-22 09:52:41 -04:00
|
|
|
rootDir string
|
2017-07-14 16:45:32 -04:00
|
|
|
client libcontainerd.Client
|
|
|
|
exitHandler ExitHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new container
|
|
|
|
func (e *Executor) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
|
2017-11-29 19:15:20 -05:00
|
|
|
opts := runctypes.RuncOptions{
|
2017-09-22 09:52:41 -04:00
|
|
|
RuntimeRoot: filepath.Join(e.rootDir, "runtime-root"),
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
err := e.client.Create(ctx, id, &spec, &opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = e.client.Start(ctx, id, "", false, attachStreamsFunc(stdout, stderr))
|
|
|
|
return err
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores a container
|
|
|
|
func (e *Executor) Restore(id string, stdout, stderr io.WriteCloser) error {
|
2017-09-22 09:52:41 -04:00
|
|
|
alive, _, err := e.client.Restore(context.Background(), id, attachStreamsFunc(stdout, stderr))
|
|
|
|
if err != nil && !errdefs.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !alive {
|
|
|
|
_, _, err = e.client.DeleteTask(context.Background(), id)
|
|
|
|
if err != nil && !errdefs.IsNotFound(err) {
|
|
|
|
logrus.WithError(err).Errorf("failed to delete container plugin %s task from containerd", id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = e.client.Delete(context.Background(), id)
|
|
|
|
if err != nil && !errdefs.IsNotFound(err) {
|
|
|
|
logrus.WithError(err).Errorf("failed to delete container plugin %s from containerd", id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRunning returns if the container with the given id is running
|
|
|
|
func (e *Executor) IsRunning(id string) (bool, error) {
|
2017-09-22 09:52:41 -04:00
|
|
|
status, err := e.client.Status(context.Background(), id)
|
|
|
|
return status == libcontainerd.StatusRunning, err
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signal sends the specified signal to the container
|
|
|
|
func (e *Executor) Signal(id string, signal int) error {
|
2017-09-22 09:52:41 -04:00
|
|
|
return e.client.SignalProcess(context.Background(), id, libcontainerd.InitProcessName, signal)
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// ProcessEvent handles events from containerd
|
2017-07-14 16:45:32 -04:00
|
|
|
// All events are ignored except the exit event, which is sent of to the stored handler
|
2017-09-22 09:52:41 -04:00
|
|
|
func (e *Executor) ProcessEvent(id string, et libcontainerd.EventType, ei libcontainerd.EventInfo) error {
|
|
|
|
switch et {
|
|
|
|
case libcontainerd.EventExit:
|
|
|
|
// delete task and container
|
|
|
|
if _, _, err := e.client.DeleteTask(context.Background(), id); err != nil {
|
|
|
|
logrus.WithError(err).Errorf("failed to delete container plugin %s task from containerd", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.client.Delete(context.Background(), id); err != nil {
|
|
|
|
logrus.WithError(err).Errorf("failed to delete container plugin %s from containerd", id)
|
|
|
|
}
|
|
|
|
return e.exitHandler.HandleExitEvent(ei.ContainerID)
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
type rio struct {
|
|
|
|
cio.IO
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
func (c *rio) Wait() {
|
2017-09-22 09:52:41 -04:00
|
|
|
c.wg.Wait()
|
|
|
|
c.IO.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func attachStreamsFunc(stdout, stderr io.WriteCloser) libcontainerd.StdioCallback {
|
2017-12-07 14:26:27 -05:00
|
|
|
return func(iop *cio.DirectIO) (cio.IO, error) {
|
2017-09-22 09:52:41 -04:00
|
|
|
if iop.Stdin != nil {
|
|
|
|
iop.Stdin.Close()
|
|
|
|
// closing stdin shouldn't be needed here, it should never be open
|
|
|
|
panic("plugin stdin shouldn't have been created!")
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
rio := &rio{IO: iop}
|
|
|
|
rio.wg.Add(2)
|
2017-07-14 16:45:32 -04:00
|
|
|
go func() {
|
|
|
|
io.Copy(stdout, iop.Stdout)
|
|
|
|
stdout.Close()
|
2017-11-29 19:15:20 -05:00
|
|
|
rio.wg.Done()
|
2017-07-14 16:45:32 -04:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
io.Copy(stderr, iop.Stderr)
|
|
|
|
stderr.Close()
|
2017-11-29 19:15:20 -05:00
|
|
|
rio.wg.Done()
|
2017-07-14 16:45:32 -04:00
|
|
|
}()
|
2017-11-29 19:15:20 -05:00
|
|
|
return rio, nil
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
}
|