Change Container interface to include Info

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-02-15 16:59:58 -08:00
parent c869eb0c61
commit 47ececd6b8
8 changed files with 23 additions and 23 deletions

View file

@ -124,7 +124,7 @@ func getTempDir(id string) (string, error) {
return tmpDir, nil return tmpDir, nil
} }
func waitContainer(events execution.ContainerService_EventsClient, respose *execution.CreateResponse) (uint32, error) { func waitContainer(events execution.ContainerService_EventsClient, response *execution.CreateResponse) (uint32, error) {
for { for {
e, err := events.Recv() e, err := events.Recv()
if err != nil { if err != nil {
@ -133,8 +133,8 @@ func waitContainer(events execution.ContainerService_EventsClient, respose *exec
if e.Type != container.Event_EXIT { if e.Type != container.Event_EXIT {
continue continue
} }
if e.ID == respose.ID && if e.ID == response.ID &&
e.Pid == respose.Pid { e.Pid == response.Pid {
return e.ExitStatus, nil return e.ExitStatus, nil
} }
} }

View file

@ -56,7 +56,7 @@ func (c *collector) collect(r Runtime) error {
// It forwards events until the channels are closed or the EventWriter // It forwards events until the channels are closed or the EventWriter
// returns an error // returns an error
// This is a blocking call // This is a blocking call
func (c *collector) forward(w EventWriter) (err error) { func (c *collector) forward(w EventWriter) error {
client := &eventClient{ client := &eventClient{
w: w, w: w,
eCh: make(chan error, 1), eCh: make(chan error, 1),
@ -64,9 +64,7 @@ func (c *collector) forward(w EventWriter) (err error) {
c.mu.Lock() c.mu.Lock()
c.eventClients[client] = struct{}{} c.eventClients[client] = struct{}{}
c.mu.Unlock() c.mu.Unlock()
if serr := <-client.eCh; serr != nil { err := <-client.eCh
err = serr
}
c.mu.Lock() c.mu.Lock()
delete(c.eventClients, client) delete(c.eventClients, client)
c.mu.Unlock() c.mu.Unlock()

View file

@ -2,11 +2,14 @@ package containerd
import "golang.org/x/net/context" import "golang.org/x/net/context"
type ContainerInfo struct {
ID string
Runtime string
}
type Container interface { type Container interface {
// ID of the container // Information of the container
ID() string Info() ContainerInfo
// Runtime returns the runtime name that the container was created with
Runtime() string
// Start the container's user defined process // Start the container's user defined process
Start(context.Context) error Start(context.Context) error
// State returns the container's state // State returns the container's state

View file

@ -25,12 +25,11 @@ type Container struct {
shim shim.ShimClient shim shim.ShimClient
} }
func (c *Container) ID() string { func (c *Container) Info() containerd.ContainerInfo {
return c.id return containerd.ContainerInfo{
} ID: c.id,
Runtime: runtimeName,
func (Container) Runtime() string { }
return runtimeName
} }
func (c *Container) Start(ctx context.Context) error { func (c *Container) Start(ctx context.Context) error {

View file

@ -51,7 +51,6 @@ type Runtime struct {
func (r *Runtime) Create(ctx context.Context, id string, opts containerd.CreateOpts) (containerd.Container, error) { func (r *Runtime) Create(ctx context.Context, id string, opts containerd.CreateOpts) (containerd.Container, error) {
path, err := r.newBundle(id, opts.Spec) path, err := r.newBundle(id, opts.Spec)
if err != nil { if err != nil {
os.RemoveAll(path)
return nil, err return nil, err
} }
s, err := newShim(path) s, err := newShim(path)

View file

@ -72,7 +72,7 @@ type Runtime interface {
Create(ctx context.Context, id string, opts CreateOpts) (Container, error) Create(ctx context.Context, id string, opts CreateOpts) (Container, error)
// Containers returns all the current containers for the runtime // Containers returns all the current containers for the runtime
Containers() ([]Container, error) Containers() ([]Container, error)
// Delete returns the container in the runtime // Delete removes the container in the runtime
Delete(ctx context.Context, c Container) error Delete(ctx context.Context, c Container) error
// Events returns events for the runtime and all containers created by the runtime // Events returns events for the runtime and all containers created by the runtime
Events(context.Context) <-chan *Event Events(context.Context) <-chan *Event

View file

@ -47,6 +47,7 @@ func (s *Service) Create(ctx context.Context, r *api.CreateRequest) (*api.Create
} }
state, err := c.State(ctx) state, err := c.State(ctx)
if err != nil { if err != nil {
s.s.Delete(ctx, r.ID)
return nil, err return nil, err
} }
return &api.CreateResponse{ return &api.CreateResponse{
@ -92,7 +93,7 @@ func (s *Service) List(ctx context.Context, r *api.ListRequest) (*api.ListRespon
status = container.Status_PAUSED status = container.Status_PAUSED
} }
resp.Containers = append(resp.Containers, &container.Container{ resp.Containers = append(resp.Containers, &container.Container{
ID: c.ID(), ID: c.Info().ID,
Pid: state.Pid(), Pid: state.Pid(),
Status: status, Status: status,
}) })

View file

@ -22,7 +22,7 @@ func NewSupervisor(ctx context.Context, runtimes map[string]Runtime) (*Superviso
return nil, err return nil, err
} }
for _, c := range containers { for _, c := range containers {
s.containers[c.ID()] = c s.containers[c.Info().ID] = c
} }
} }
return s, nil return s, nil
@ -59,7 +59,7 @@ func (s *Supervisor) Create(ctx context.Context, id, runtime string, opts Create
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.containers[c.ID()] = c s.containers[c.Info().ID] = c
return c, nil return c, nil
} }
@ -71,7 +71,7 @@ func (s *Supervisor) Delete(ctx context.Context, id string) error {
if !ok { if !ok {
return ErrContainerNotExist return ErrContainerNotExist
} }
err := s.runtimes[c.Runtime()].Delete(ctx, c) err := s.runtimes[c.Info().Runtime].Delete(ctx, c)
if err != nil { if err != nil {
return err return err
} }