From 47ececd6b8549180d09c7441f51d429dbff06dfd Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 15 Feb 2017 16:59:58 -0800 Subject: [PATCH] Change Container interface to include Info Signed-off-by: Michael Crosby --- cmd/ctr/utils.go | 6 +++--- collector.go | 6 ++---- container.go | 11 +++++++---- linux/container.go | 11 +++++------ linux/runtime.go | 1 - runtime.go | 2 +- services/execution/service.go | 3 ++- supervisor.go | 6 +++--- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cmd/ctr/utils.go b/cmd/ctr/utils.go index e6030ec..df2f790 100644 --- a/cmd/ctr/utils.go +++ b/cmd/ctr/utils.go @@ -124,7 +124,7 @@ func getTempDir(id string) (string, error) { 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 { e, err := events.Recv() if err != nil { @@ -133,8 +133,8 @@ func waitContainer(events execution.ContainerService_EventsClient, respose *exec if e.Type != container.Event_EXIT { continue } - if e.ID == respose.ID && - e.Pid == respose.Pid { + if e.ID == response.ID && + e.Pid == response.Pid { return e.ExitStatus, nil } } diff --git a/collector.go b/collector.go index 50d95aa..e69e227 100644 --- a/collector.go +++ b/collector.go @@ -56,7 +56,7 @@ func (c *collector) collect(r Runtime) error { // It forwards events until the channels are closed or the EventWriter // returns an error // This is a blocking call -func (c *collector) forward(w EventWriter) (err error) { +func (c *collector) forward(w EventWriter) error { client := &eventClient{ w: w, eCh: make(chan error, 1), @@ -64,9 +64,7 @@ func (c *collector) forward(w EventWriter) (err error) { c.mu.Lock() c.eventClients[client] = struct{}{} c.mu.Unlock() - if serr := <-client.eCh; serr != nil { - err = serr - } + err := <-client.eCh c.mu.Lock() delete(c.eventClients, client) c.mu.Unlock() diff --git a/container.go b/container.go index 1da1cc9..be44e7b 100644 --- a/container.go +++ b/container.go @@ -2,11 +2,14 @@ package containerd import "golang.org/x/net/context" +type ContainerInfo struct { + ID string + Runtime string +} + type Container interface { - // ID of the container - ID() string - // Runtime returns the runtime name that the container was created with - Runtime() string + // Information of the container + Info() ContainerInfo // Start the container's user defined process Start(context.Context) error // State returns the container's state diff --git a/linux/container.go b/linux/container.go index b2240dc..6e4ae9f 100644 --- a/linux/container.go +++ b/linux/container.go @@ -25,12 +25,11 @@ type Container struct { shim shim.ShimClient } -func (c *Container) ID() string { - return c.id -} - -func (Container) Runtime() string { - return runtimeName +func (c *Container) Info() containerd.ContainerInfo { + return containerd.ContainerInfo{ + ID: c.id, + Runtime: runtimeName, + } } func (c *Container) Start(ctx context.Context) error { diff --git a/linux/runtime.go b/linux/runtime.go index 6de710a..99bbb78 100644 --- a/linux/runtime.go +++ b/linux/runtime.go @@ -51,7 +51,6 @@ type Runtime struct { func (r *Runtime) Create(ctx context.Context, id string, opts containerd.CreateOpts) (containerd.Container, error) { path, err := r.newBundle(id, opts.Spec) if err != nil { - os.RemoveAll(path) return nil, err } s, err := newShim(path) diff --git a/runtime.go b/runtime.go index 9ac5c9e..1ab8d7d 100644 --- a/runtime.go +++ b/runtime.go @@ -72,7 +72,7 @@ type Runtime interface { Create(ctx context.Context, id string, opts CreateOpts) (Container, error) // Containers returns all the current containers for the runtime Containers() ([]Container, error) - // Delete returns the container in the runtime + // Delete removes the container in the runtime Delete(ctx context.Context, c Container) error // Events returns events for the runtime and all containers created by the runtime Events(context.Context) <-chan *Event diff --git a/services/execution/service.go b/services/execution/service.go index ba109c0..84c07b4 100644 --- a/services/execution/service.go +++ b/services/execution/service.go @@ -47,6 +47,7 @@ func (s *Service) Create(ctx context.Context, r *api.CreateRequest) (*api.Create } state, err := c.State(ctx) if err != nil { + s.s.Delete(ctx, r.ID) return nil, err } return &api.CreateResponse{ @@ -92,7 +93,7 @@ func (s *Service) List(ctx context.Context, r *api.ListRequest) (*api.ListRespon status = container.Status_PAUSED } resp.Containers = append(resp.Containers, &container.Container{ - ID: c.ID(), + ID: c.Info().ID, Pid: state.Pid(), Status: status, }) diff --git a/supervisor.go b/supervisor.go index 08dafcc..dedd534 100644 --- a/supervisor.go +++ b/supervisor.go @@ -22,7 +22,7 @@ func NewSupervisor(ctx context.Context, runtimes map[string]Runtime) (*Superviso return nil, err } for _, c := range containers { - s.containers[c.ID()] = c + s.containers[c.Info().ID] = c } } return s, nil @@ -59,7 +59,7 @@ func (s *Supervisor) Create(ctx context.Context, id, runtime string, opts Create if err != nil { return nil, err } - s.containers[c.ID()] = c + s.containers[c.Info().ID] = c return c, nil } @@ -71,7 +71,7 @@ func (s *Supervisor) Delete(ctx context.Context, id string) error { if !ok { return ErrContainerNotExist } - err := s.runtimes[c.Runtime()].Delete(ctx, c) + err := s.runtimes[c.Info().Runtime].Delete(ctx, c) if err != nil { return err }