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"
|
2018-03-27 10:03:53 -04:00
|
|
|
"time"
|
2017-07-14 16:45:32 -04:00
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
"github.com/containerd/containerd"
|
2017-11-29 19:15:20 -05:00
|
|
|
"github.com/containerd/containerd/cio"
|
2018-06-08 20:39:07 -04:00
|
|
|
"github.com/containerd/containerd/runtime/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
|
|
|
)
|
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
// PluginNamespace is the name used for the plugins namespace
|
|
|
|
const 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
|
|
|
|
}
|
|
|
|
|
2018-03-27 10:03:53 -04:00
|
|
|
// Client is used by the exector to perform operations.
|
|
|
|
// TODO(@cpuguy83): This should really just be based off the containerd client interface.
|
|
|
|
// However right now this whole package is tied to github.com/docker/docker/libcontainerd
|
|
|
|
type Client interface {
|
|
|
|
Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}) error
|
|
|
|
Restore(ctx context.Context, containerID string, attachStdio libcontainerd.StdioCallback) (alive bool, pid int, err error)
|
|
|
|
Status(ctx context.Context, containerID string) (libcontainerd.Status, error)
|
|
|
|
Delete(ctx context.Context, containerID string) error
|
|
|
|
DeleteTask(ctx context.Context, containerID string) (uint32, time.Time, error)
|
|
|
|
Start(ctx context.Context, containerID, checkpointDir string, withStdin bool, attachStdio libcontainerd.StdioCallback) (pid int, err error)
|
|
|
|
SignalProcess(ctx context.Context, containerID, processID string, signal int) error
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:45:32 -04:00
|
|
|
// New creates a new containerd plugin executor
|
2018-05-23 15:15:21 -04:00
|
|
|
func New(ctx context.Context, rootDir string, cli *containerd.Client, exitHandler ExitHandler) (*Executor, error) {
|
2017-09-22 09:52:41 -04:00
|
|
|
e := &Executor{
|
|
|
|
rootDir: rootDir,
|
|
|
|
exitHandler: exitHandler,
|
|
|
|
}
|
2018-05-23 15:15:21 -04:00
|
|
|
|
|
|
|
client, err := libcontainerd.NewClient(ctx, cli, rootDir, 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
|
2018-03-27 10:03:53 -04:00
|
|
|
client Client
|
2017-07-14 16:45:32 -04:00
|
|
|
exitHandler ExitHandler
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:48:54 -04:00
|
|
|
// deleteTaskAndContainer deletes plugin task and then plugin container from containerd
|
|
|
|
func deleteTaskAndContainer(ctx context.Context, cli Client, id string) {
|
|
|
|
_, _, err := cli.DeleteTask(ctx, id)
|
|
|
|
if err != nil && !errdefs.IsNotFound(err) {
|
|
|
|
logrus.WithError(err).WithField("id", id).Error("failed to delete plugin task from containerd")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cli.Delete(ctx, id)
|
|
|
|
if err != nil && !errdefs.IsNotFound(err) {
|
|
|
|
logrus.WithError(err).WithField("id", id).Error("failed to delete plugin container from containerd")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:45:32 -04:00
|
|
|
// 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 {
|
2018-03-27 10:03:53 -04:00
|
|
|
status, err2 := e.client.Status(ctx, id)
|
|
|
|
if err2 != nil {
|
|
|
|
if !errdefs.IsNotFound(err2) {
|
|
|
|
logrus.WithError(err2).WithField("id", id).Warn("Received an error while attempting to read plugin status")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if status != libcontainerd.StatusRunning && status != libcontainerd.StatusUnknown {
|
|
|
|
if err2 := e.client.Delete(ctx, id); err2 != nil && !errdefs.IsNotFound(err2) {
|
|
|
|
logrus.WithError(err2).WithField("plugin", id).Error("Error cleaning up containerd container")
|
|
|
|
}
|
|
|
|
err = e.client.Create(ctx, id, &spec, &opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating containerd container")
|
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = e.client.Start(ctx, id, "", false, attachStreamsFunc(stdout, stderr))
|
2018-03-27 10:03:53 -04:00
|
|
|
if err != nil {
|
2018-04-20 10:48:54 -04:00
|
|
|
deleteTaskAndContainer(ctx, e.client, id)
|
2018-03-27 10:03:53 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
return err
|
2017-07-14 16:45:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores a container
|
2018-04-20 10:48:54 -04:00
|
|
|
func (e *Executor) Restore(id string, stdout, stderr io.WriteCloser) (bool, 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) {
|
2018-04-20 10:48:54 -04:00
|
|
|
return false, err
|
2017-09-22 09:52:41 -04:00
|
|
|
}
|
|
|
|
if !alive {
|
2018-04-20 10:48:54 -04:00
|
|
|
deleteTaskAndContainer(context.Background(), e.client, id)
|
2017-09-22 09:52:41 -04:00
|
|
|
}
|
2018-04-20 10:48:54 -04:00
|
|
|
return alive, 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:
|
2018-04-20 10:48:54 -04:00
|
|
|
deleteTaskAndContainer(context.Background(), e.client, id)
|
2017-09-22 09:52:41 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|