api/services: define the container metadata service

Working from feedback on the existing implementation, we have now
introduced a central metadata object to represent the lifecycle and pin
the resources required to implement what people today know as
containers. This includes the runtime specification and the root
filesystem snapshots. We also allow arbitrary labeling of the container.
Such provisions will bring the containerd definition of container closer
to what is expected by users.

The objects that encompass today's ContainerService, centered around the
runtime, will be known as tasks. These tasks take on the existing
lifecycle behavior of containerd's containers, which means that they are
deleted when they exit. Largely, there are no other changes except for
naming.

The `Container` object will operate purely as a metadata object. No
runtime state will be held on `Container`. It only informs the execution
service on what is required for creating tasks and the resources in use
by that container. The resources referenced by that container will be
deleted when the container is deleted, if not in use. In this sense,
users can create, list, label and delete containers in a similar way as
they do with docker today, without the complexity of runtime locks that
plagues current implementations.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-05-15 17:44:50 -07:00
parent 8f3b89c79d
commit 539742881d
47 changed files with 4067 additions and 1115 deletions

View File

@@ -14,8 +14,8 @@ import (
"time"
"github.com/containerd/containerd/api/services/shim"
"github.com/containerd/containerd/api/types/container"
"github.com/containerd/containerd/api/types/mount"
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/plugin"
runc "github.com/containerd/go-runc"
@@ -83,10 +83,10 @@ type Runtime struct {
events chan *plugin.Event
eventsContext context.Context
eventsCancel func()
monitor plugin.ContainerMonitor
monitor plugin.TaskMonitor
}
func (r *Runtime) Create(ctx context.Context, id string, opts plugin.CreateOpts) (plugin.Container, error) {
func (r *Runtime) Create(ctx context.Context, id string, opts plugin.CreateOpts) (plugin.Task, error) {
path, err := r.newBundle(id, opts.Spec)
if err != nil {
return nil, err
@@ -127,16 +127,16 @@ func (r *Runtime) Create(ctx context.Context, id string, opts plugin.CreateOpts)
os.RemoveAll(path)
return nil, err
}
c := newContainer(id, opts.Spec, s)
// after the container is created, add it to the monitor
c := newTask(id, opts.Spec, s)
// after the task is create add it to the monitor
if err = r.monitor.Monitor(c); err != nil {
return nil, err
}
return c, nil
}
func (r *Runtime) Delete(ctx context.Context, c plugin.Container) (*plugin.Exit, error) {
lc, ok := c.(*Container)
func (r *Runtime) Delete(ctx context.Context, c plugin.Task) (*plugin.Exit, error) {
lc, ok := c.(*Task)
if !ok {
return nil, fmt.Errorf("container cannot be cast as *linux.Container")
}
@@ -153,15 +153,15 @@ func (r *Runtime) Delete(ctx context.Context, c plugin.Container) (*plugin.Exit,
return &plugin.Exit{
Status: rsp.ExitStatus,
Timestamp: rsp.ExitedAt,
}, r.deleteBundle(lc.id)
}, r.deleteBundle(lc.containerID)
}
func (r *Runtime) Containers(ctx context.Context) ([]plugin.Container, error) {
func (r *Runtime) Tasks(ctx context.Context) ([]plugin.Task, error) {
dir, err := ioutil.ReadDir(r.root)
if err != nil {
return nil, err
}
var o []plugin.Container
var o []plugin.Task
for _, fi := range dir {
if !fi.IsDir() {
continue
@@ -206,15 +206,15 @@ func (r *Runtime) forward(events shim.Shim_EventsClient) {
}
var et plugin.EventType
switch e.Type {
case container.Event_CREATE:
case task.Event_CREATE:
et = plugin.CreateEvent
case container.Event_EXEC_ADDED:
case task.Event_EXEC_ADDED:
et = plugin.ExecAddEvent
case container.Event_EXIT:
case task.Event_EXIT:
et = plugin.ExitEvent
case container.Event_OOM:
case task.Event_OOM:
et = plugin.OOMEvent
case container.Event_START:
case task.Event_START:
et = plugin.StartEvent
}
r.events <- &plugin.Event{
@@ -250,20 +250,22 @@ func (r *Runtime) deleteBundle(id string) error {
return os.RemoveAll(filepath.Join(r.root, id))
}
func (r *Runtime) loadContainer(path string) (*Container, error) {
func (r *Runtime) loadContainer(path string) (*Task, error) {
id := filepath.Base(path)
s, err := loadShim(path, r.remote)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(filepath.Join(path, configFilename))
if err != nil {
return nil, err
}
return &Container{
id: id,
shim: s,
spec: data,
return &Task{
containerID: id,
shim: s,
spec: data,
}, nil
}

View File

@@ -4,7 +4,7 @@ import (
"path/filepath"
shimapi "github.com/containerd/containerd/api/services/shim"
"github.com/containerd/containerd/api/types/container"
"github.com/containerd/containerd/api/types/task"
runc "github.com/containerd/go-runc"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"golang.org/x/net/context"
@@ -104,11 +104,11 @@ func (c *client) Checkpoint(ctx context.Context, in *shimapi.CheckpointRequest,
}
type events struct {
c chan *container.Event
c chan *task.Event
ctx context.Context
}
func (e *events) Recv() (*container.Event, error) {
func (e *events) Recv() (*task.Event, error) {
ev := <-e.c
return ev, nil
}

View File

@@ -10,7 +10,7 @@ import (
"github.com/containerd/console"
shimapi "github.com/containerd/containerd/api/services/shim"
"github.com/containerd/containerd/api/types/container"
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/reaper"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
@@ -25,7 +25,7 @@ func New(path string) *Service {
return &Service{
path: path,
processes: make(map[int]process),
events: make(chan *container.Event, 4096),
events: make(chan *task.Event, 4096),
}
}
@@ -36,7 +36,7 @@ type Service struct {
bundle string
mu sync.Mutex
processes map[int]process
events chan *container.Event
events chan *task.Event
execID int
}
@@ -56,8 +56,9 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateRequest) (*shimap
ExitCh: make(chan int, 1),
}
reaper.Default.Register(pid, cmd)
s.events <- &container.Event{
Type: container.Event_CREATE,
go s.waitExit(process, pid, cmd)
s.events <- &task.Event{
Type: task.Event_CREATE,
ID: r.ID,
Pid: uint32(pid),
}
@@ -71,8 +72,8 @@ func (s *Service) Start(ctx context.Context, r *shimapi.StartRequest) (*google_p
if err := s.initProcess.Start(ctx); err != nil {
return nil, err
}
s.events <- &container.Event{
Type: container.Event_START,
s.events <- &task.Event{
Type: task.Event_START,
ID: s.id,
Pid: uint32(s.initProcess.Pid()),
}
@@ -114,8 +115,9 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecRequest) (*shimapi.Ex
}
reaper.Default.RegisterNL(pid, cmd)
reaper.Default.Unlock()
s.events <- &container.Event{
Type: container.Event_EXEC_ADDED,
go s.waitExit(process, pid, cmd)
s.events <- &task.Event{
Type: task.Event_EXEC_ADDED,
ID: s.id,
Pid: uint32(pid),
}
@@ -159,35 +161,35 @@ func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.
if err != nil {
return nil, err
}
status := container.StatusUnknown
status := task.StatusUnknown
switch st {
case "created":
status = container.StatusCreated
status = task.StatusCreated
case "running":
status = container.StatusRunning
status = task.StatusRunning
case "stopped":
status = container.StatusStopped
status = task.StatusStopped
case "paused":
status = container.StatusPaused
status = task.StatusPaused
}
o := &shimapi.StateResponse{
ID: s.id,
Bundle: s.bundle,
Pid: uint32(s.initProcess.Pid()),
Status: status,
Processes: []*container.Process{},
Processes: []*task.Process{},
}
s.mu.Lock()
defer s.mu.Unlock()
for _, p := range s.processes {
status := container.StatusRunning
status := task.StatusRunning
if err := unix.Kill(p.Pid(), 0); err != nil {
if err != syscall.ESRCH {
return nil, err
}
status = container.StatusStopped
status = task.StatusStopped
}
o.Processes = append(o.Processes, &container.Process{
o.Processes = append(o.Processes, &task.Process{
Pid: uint32(p.Pid()),
Status: status,
})
@@ -255,9 +257,9 @@ func (s *Service) Processes(ctx context.Context, r *shimapi.ProcessesRequest) (*
return nil, err
}
ps := []*container.Process{}
ps := []*task.Process{}
for _, pid := range pids {
ps = append(ps, &container.Process{
ps = append(ps, &task.Process{
Pid: pid,
})
}
@@ -290,8 +292,8 @@ func (s *Service) Checkpoint(ctx context.Context, r *shimapi.CheckpointRequest)
func (s *Service) waitExit(p process, pid int, cmd *reaper.Cmd) {
status := <-cmd.ExitCh
p.Exited(status)
s.events <- &container.Event{
Type: container.Event_EXIT,
s.events <- &task.Event{
Type: task.Event_EXIT,
ID: s.id,
Pid: uint32(pid),
ExitStatus: uint32(status),

View File

@@ -6,7 +6,7 @@ import (
"context"
"github.com/containerd/containerd/api/services/shim"
"github.com/containerd/containerd/api/types/container"
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/plugin"
protobuf "github.com/gogo/protobuf/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -25,48 +25,47 @@ func (s State) Status() plugin.Status {
return s.status
}
func newContainer(id string, spec []byte, shim shim.ShimClient) *Container {
return &Container{
id: id,
shim: shim,
spec: spec,
type Task struct {
containerID string
spec []byte
shim shim.ShimClient
}
func newTask(id string, spec []byte, shim shim.ShimClient) *Task {
return &Task{
containerID: id,
shim: shim,
spec: spec,
}
}
type Container struct {
id string
spec []byte
shim shim.ShimClient
}
func (c *Container) Info() plugin.ContainerInfo {
return plugin.ContainerInfo{
ID: c.id,
Runtime: runtimeName,
Spec: c.spec,
func (c *Task) Info() plugin.TaskInfo {
return plugin.TaskInfo{
ContainerID: c.containerID,
Runtime: runtimeName,
Spec: c.spec,
}
}
func (c *Container) Start(ctx context.Context) error {
func (c *Task) Start(ctx context.Context) error {
_, err := c.shim.Start(ctx, &shim.StartRequest{})
return err
}
func (c *Container) State(ctx context.Context) (plugin.State, error) {
func (c *Task) State(ctx context.Context) (plugin.State, error) {
response, err := c.shim.State(ctx, &shim.StateRequest{})
if err != nil {
return nil, err
}
var status plugin.Status
switch response.Status {
case container.StatusCreated:
case task.StatusCreated:
status = plugin.CreatedStatus
case container.StatusRunning:
case task.StatusRunning:
status = plugin.RunningStatus
case container.StatusStopped:
case task.StatusStopped:
status = plugin.StoppedStatus
case container.StatusPaused:
case task.StatusPaused:
status = plugin.PausedStatus
// TODO: containerd.DeletedStatus
}
@@ -76,17 +75,17 @@ func (c *Container) State(ctx context.Context) (plugin.State, error) {
}, nil
}
func (c *Container) Pause(ctx context.Context) error {
func (c *Task) Pause(ctx context.Context) error {
_, err := c.shim.Pause(ctx, &shim.PauseRequest{})
return err
}
func (c *Container) Resume(ctx context.Context) error {
func (c *Task) Resume(ctx context.Context) error {
_, err := c.shim.Resume(ctx, &shim.ResumeRequest{})
return err
}
func (c *Container) Kill(ctx context.Context, signal uint32, pid uint32, all bool) error {
func (c *Task) Kill(ctx context.Context, signal uint32, pid uint32, all bool) error {
_, err := c.shim.Kill(ctx, &shim.KillRequest{
Signal: signal,
Pid: pid,
@@ -95,7 +94,7 @@ func (c *Container) Kill(ctx context.Context, signal uint32, pid uint32, all boo
return err
}
func (c *Container) Exec(ctx context.Context, opts plugin.ExecOpts) (plugin.Process, error) {
func (c *Task) Exec(ctx context.Context, opts plugin.ExecOpts) (plugin.Process, error) {
request := &shim.ExecRequest{
Stdin: opts.IO.Stdin,
Stdout: opts.IO.Stdout,
@@ -117,9 +116,9 @@ func (c *Container) Exec(ctx context.Context, opts plugin.ExecOpts) (plugin.Proc
}, nil
}
func (c *Container) Processes(ctx context.Context) ([]uint32, error) {
func (c *Task) Processes(ctx context.Context) ([]uint32, error) {
resp, err := c.shim.Processes(ctx, &shim.ProcessesRequest{
ID: c.id,
ID: c.containerID,
})
if err != nil {
@@ -135,7 +134,7 @@ func (c *Container) Processes(ctx context.Context) ([]uint32, error) {
return pids, nil
}
func (c *Container) Pty(ctx context.Context, pid uint32, size plugin.ConsoleSize) error {
func (c *Task) Pty(ctx context.Context, pid uint32, size plugin.ConsoleSize) error {
_, err := c.shim.Pty(ctx, &shim.PtyRequest{
Pid: pid,
Width: size.Width,
@@ -144,14 +143,14 @@ func (c *Container) Pty(ctx context.Context, pid uint32, size plugin.ConsoleSize
return err
}
func (c *Container) CloseStdin(ctx context.Context, pid uint32) error {
func (c *Task) CloseStdin(ctx context.Context, pid uint32) error {
_, err := c.shim.CloseStdin(ctx, &shim.CloseStdinRequest{
Pid: pid,
})
return err
}
func (c *Container) Checkpoint(ctx context.Context, opts plugin.CheckpointOpts) error {
func (c *Task) Checkpoint(ctx context.Context, opts plugin.CheckpointOpts) error {
_, err := c.shim.Checkpoint(ctx, &shim.CheckpointRequest{
Exit: opts.Exit,
AllowTcp: opts.AllowTCP,
@@ -166,7 +165,7 @@ func (c *Container) Checkpoint(ctx context.Context, opts plugin.CheckpointOpts)
type Process struct {
pid int
c *Container
c *Task
}
func (p *Process) Kill(ctx context.Context, signal uint32, _ bool) error {