Make Task, Container, Image interface types

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-05-25 11:05:58 -07:00
parent 1de25c09e3
commit 608e6daaa4
5 changed files with 61 additions and 32 deletions

View File

@ -83,12 +83,12 @@ type Client struct {
} }
// Containers returns all containers created in containerd // Containers returns all containers created in containerd
func (c *Client) Containers(ctx context.Context) ([]*Container, error) { func (c *Client) Containers(ctx context.Context) ([]Container, error) {
r, err := c.containers().List(ctx, &containers.ListContainersRequest{}) r, err := c.containers().List(ctx, &containers.ListContainersRequest{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
var out []*Container var out []Container
for _, container := range r.Containers { for _, container := range r.Containers {
out = append(out, containerFromProto(c, container)) out = append(out, containerFromProto(c, container))
} }
@ -119,9 +119,9 @@ func WithExistingRootFS(id string) NewContainerOpts {
// WithNewRootFS allocates a new snapshot to be used by the container as the // WithNewRootFS allocates a new snapshot to be used by the container as the
// root filesystem in read-write mode // root filesystem in read-write mode
func WithNewRootFS(id string, image *Image) NewContainerOpts { func WithNewRootFS(id string, i Image) NewContainerOpts {
return func(ctx context.Context, client *Client, c *containers.Container) error { return func(ctx context.Context, client *Client, c *containers.Container) error {
diffIDs, err := image.i.RootFS(ctx, client.content()) diffIDs, err := i.(*image).i.RootFS(ctx, client.content())
if err != nil { if err != nil {
return err return err
} }
@ -135,9 +135,9 @@ func WithNewRootFS(id string, image *Image) NewContainerOpts {
// WithNewReadonlyRootFS allocates a new snapshot to be used by the container as the // WithNewReadonlyRootFS allocates a new snapshot to be used by the container as the
// root filesystem in read-only mode // root filesystem in read-only mode
func WithNewReadonlyRootFS(id string, image *Image) NewContainerOpts { func WithNewReadonlyRootFS(id string, i Image) NewContainerOpts {
return func(ctx context.Context, client *Client, c *containers.Container) error { return func(ctx context.Context, client *Client, c *containers.Container) error {
diffIDs, err := image.i.RootFS(ctx, client.content()) diffIDs, err := i.(*image).i.RootFS(ctx, client.content())
if err != nil { if err != nil {
return err return err
} }
@ -158,7 +158,7 @@ func WithRuntime(name string) NewContainerOpts {
// NewContainer will create a new container in container with the provided id // NewContainer will create a new container in container with the provided id
// the id must be unique within the namespace // the id must be unique within the namespace
func (c *Client) NewContainer(ctx context.Context, id string, spec *specs.Spec, opts ...NewContainerOpts) (*Container, error) { func (c *Client) NewContainer(ctx context.Context, id string, spec *specs.Spec, opts ...NewContainerOpts) (Container, error) {
data, err := json.Marshal(spec) data, err := json.Marshal(spec)
if err != nil { if err != nil {
return nil, err return nil, err
@ -258,7 +258,7 @@ func (s *snapshotUnpacker) getLayers(ctx context.Context, image images.Image) ([
return layers, nil return layers, nil
} }
func (c *Client) Pull(ctx context.Context, ref string, opts ...PullOpts) (*Image, error) { func (c *Client) Pull(ctx context.Context, ref string, opts ...PullOpts) (Image, error) {
pullCtx := defaultPullContext() pullCtx := defaultPullContext()
for _, o := range opts { for _, o := range opts {
if err := o(c, pullCtx); err != nil { if err := o(c, pullCtx); err != nil {
@ -296,7 +296,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...PullOpts) (*Image
return nil, err return nil, err
} }
} }
return &Image{ return &image{
client: c, client: c,
i: i, i: i,
}, nil }, nil

View File

@ -10,27 +10,37 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
) )
func containerFromProto(client *Client, c containers.Container) *Container { type Container interface {
return &Container{ ID() string
Delete(context.Context) error
NewTask(context.Context, IOCreation) (Task, error)
Spec() (*specs.Spec, error)
Task() Task
}
func containerFromProto(client *Client, c containers.Container) *container {
return &container{
client: client, client: client,
c: c, c: c,
} }
} }
type Container struct { var _ = (Container)(&container{})
type container struct {
client *Client client *Client
c containers.Container c containers.Container
task *Task task *task
} }
// ID returns the container's unique id // ID returns the container's unique id
func (c *Container) ID() string { func (c *container) ID() string {
return c.c.ID return c.c.ID
} }
// Spec returns the current OCI specification for the container // Spec returns the current OCI specification for the container
func (c *Container) Spec() (*specs.Spec, error) { func (c *container) Spec() (*specs.Spec, error) {
var s specs.Spec var s specs.Spec
if err := json.Unmarshal(c.c.Spec.Value, &s); err != nil { if err := json.Unmarshal(c.c.Spec.Value, &s); err != nil {
return nil, err return nil, err
@ -40,7 +50,7 @@ func (c *Container) Spec() (*specs.Spec, error) {
// Delete deletes an existing container // Delete deletes an existing container
// an error is returned if the container has running tasks // an error is returned if the container has running tasks
func (c *Container) Delete(ctx context.Context) error { func (c *container) Delete(ctx context.Context) error {
// TODO: should the client be the one removing resources attached // TODO: should the client be the one removing resources attached
// to the container at the moment before we have GC? // to the container at the moment before we have GC?
err := c.client.snapshotter().Remove(ctx, c.c.RootFS) err := c.client.snapshotter().Remove(ctx, c.c.RootFS)
@ -53,11 +63,11 @@ func (c *Container) Delete(ctx context.Context) error {
return err return err
} }
func (c *Container) Task() *Task { func (c *container) Task() Task {
return c.task return c.task
} }
func (c *Container) NewTask(ctx context.Context, ioCreate IOCreation) (*Task, error) { func (c *container) NewTask(ctx context.Context, ioCreate IOCreation) (Task, error) {
i, err := ioCreate() i, err := ioCreate()
if err != nil { if err != nil {
return nil, err return nil, err
@ -85,7 +95,7 @@ func (c *Container) NewTask(ctx context.Context, ioCreate IOCreation) (*Task, er
if err != nil { if err != nil {
return nil, err return nil, err
} }
t := &Task{ t := &task{
client: c.client, client: c.client,
io: i, io: i,
containerID: response.ContainerID, containerID: response.ContainerID,

View File

@ -2,7 +2,12 @@ package containerd
import "github.com/containerd/containerd/images" import "github.com/containerd/containerd/images"
type Image struct { type Image interface {
}
var _ = (Image)(&image{})
type image struct {
client *Client client *Client
i images.Image i images.Image

View File

@ -183,8 +183,9 @@ func WithHostNamespace(ns specs.LinuxNamespaceType) SpecOpts {
} }
} }
func WithImage(ctx context.Context, image *Image) SpecOpts { func WithImage(ctx context.Context, i Image) SpecOpts {
return func(s *specs.Spec) error { return func(s *specs.Spec) error {
image := i.(*image)
store := image.client.content() store := image.client.content()
ic, err := image.i.Config(ctx, store) ic, err := image.i.Config(ctx, store)
if err != nil { if err != nil {

35
task.go
View File

@ -10,7 +10,7 @@ import (
"syscall" "syscall"
"github.com/containerd/containerd/api/services/execution" "github.com/containerd/containerd/api/services/execution"
"github.com/containerd/containerd/api/types/task" taskapi "github.com/containerd/containerd/api/types/task"
"github.com/containerd/fifo" "github.com/containerd/fifo"
) )
@ -158,7 +158,20 @@ func (g *wgCloser) Close() error {
return nil return nil
} }
type Task struct { type Task interface {
Delete(context.Context) (uint32, error)
Kill(context.Context, syscall.Signal) error
Pause(context.Context) error
Resume(context.Context) error
Pid() uint32
Start(context.Context) error
Status(context.Context) (string, error)
Wait(context.Context) (uint32, error)
}
var _ = (Task)(&task{})
type task struct {
client *Client client *Client
io *IO io *IO
@ -167,18 +180,18 @@ type Task struct {
} }
// Pid returns the pid or process id for the task // Pid returns the pid or process id for the task
func (t *Task) Pid() uint32 { func (t *task) Pid() uint32 {
return t.pid return t.pid
} }
func (t *Task) Start(ctx context.Context) error { func (t *task) Start(ctx context.Context) error {
_, err := t.client.tasks().Start(ctx, &execution.StartRequest{ _, err := t.client.tasks().Start(ctx, &execution.StartRequest{
ContainerID: t.containerID, ContainerID: t.containerID,
}) })
return err return err
} }
func (t *Task) Kill(ctx context.Context, s syscall.Signal) error { func (t *task) Kill(ctx context.Context, s syscall.Signal) error {
_, err := t.client.tasks().Kill(ctx, &execution.KillRequest{ _, err := t.client.tasks().Kill(ctx, &execution.KillRequest{
Signal: uint32(s), Signal: uint32(s),
ContainerID: t.containerID, ContainerID: t.containerID,
@ -189,21 +202,21 @@ func (t *Task) Kill(ctx context.Context, s syscall.Signal) error {
return err return err
} }
func (t *Task) Pause(ctx context.Context) error { func (t *task) Pause(ctx context.Context) error {
_, err := t.client.tasks().Pause(ctx, &execution.PauseRequest{ _, err := t.client.tasks().Pause(ctx, &execution.PauseRequest{
ContainerID: t.containerID, ContainerID: t.containerID,
}) })
return err return err
} }
func (t *Task) Resume(ctx context.Context) error { func (t *task) Resume(ctx context.Context) error {
_, err := t.client.tasks().Resume(ctx, &execution.ResumeRequest{ _, err := t.client.tasks().Resume(ctx, &execution.ResumeRequest{
ContainerID: t.containerID, ContainerID: t.containerID,
}) })
return err return err
} }
func (t *Task) Status(ctx context.Context) (string, error) { func (t *task) Status(ctx context.Context) (string, error) {
r, err := t.client.tasks().Info(ctx, &execution.InfoRequest{ r, err := t.client.tasks().Info(ctx, &execution.InfoRequest{
ContainerID: t.containerID, ContainerID: t.containerID,
}) })
@ -214,7 +227,7 @@ func (t *Task) Status(ctx context.Context) (string, error) {
} }
// Wait is a blocking call that will wait for the task to exit and return the exit status // Wait is a blocking call that will wait for the task to exit and return the exit status
func (t *Task) Wait(ctx context.Context) (uint32, error) { func (t *task) Wait(ctx context.Context) (uint32, error) {
events, err := t.client.tasks().Events(ctx, &execution.EventsRequest{}) events, err := t.client.tasks().Events(ctx, &execution.EventsRequest{})
if err != nil { if err != nil {
return 255, err return 255, err
@ -224,7 +237,7 @@ func (t *Task) Wait(ctx context.Context) (uint32, error) {
if err != nil { if err != nil {
return 255, err return 255, err
} }
if e.Type != task.Event_EXIT { if e.Type != taskapi.Event_EXIT {
continue continue
} }
if e.ID == t.containerID && e.Pid == t.pid { if e.ID == t.containerID && e.Pid == t.pid {
@ -236,7 +249,7 @@ func (t *Task) Wait(ctx context.Context) (uint32, error) {
// Delete deletes the task and its runtime state // Delete deletes the task and its runtime state
// it returns the exit status of the task and any errors that were encountered // it returns the exit status of the task and any errors that were encountered
// during cleanup // during cleanup
func (t *Task) Delete(ctx context.Context) (uint32, error) { func (t *task) Delete(ctx context.Context) (uint32, error) {
cerr := t.io.Close() cerr := t.io.Close()
r, err := t.client.tasks().Delete(ctx, &execution.DeleteRequest{ r, err := t.client.tasks().Delete(ctx, &execution.DeleteRequest{
ContainerID: t.containerID, ContainerID: t.containerID,