Fetch current container info before operations

This makes sure the client is always in sync with the server before
performing any type of operations on the container metadata.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-10-04 13:16:39 -04:00
parent acba0f50ef
commit fa9e9bdf46
8 changed files with 89 additions and 84 deletions

View File

@ -24,7 +24,7 @@ var taskAttachCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -49,15 +49,18 @@ var containersCommand = cli.Command{
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "CONTAINER\tIMAGE\tRUNTIME\t") fmt.Fprintln(w, "CONTAINER\tIMAGE\tRUNTIME\t")
for _, c := range containers { for _, c := range containers {
imageName := c.Info().Image info, err := c.Info(ctx)
if err != nil {
return err
}
imageName := info.Image
if imageName == "" { if imageName == "" {
imageName = "-" imageName = "-"
} }
record := c.Info()
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t\n", if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t\n",
c.ID(), c.ID(),
imageName, imageName,
record.Runtime.Name, info.Runtime.Name,
); err != nil { ); err != nil {
return err return err
} }

View File

@ -47,7 +47,7 @@ var taskExecCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -28,7 +28,11 @@ var containerInfoCommand = cli.Command{
return err return err
} }
printAsJSON(container.Info()) info, err := container.Info(ctx)
if err != nil {
return err
}
printAsJSON(info)
return nil return nil
}, },

View File

@ -34,7 +34,7 @@ var taskStartCommand = cli.Command{
return err return err
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"github.com/containerd/containerd/api/services/tasks/v1" "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/api/types" "github.com/containerd/containerd/api/types"
@ -22,13 +21,13 @@ type Container interface {
// ID identifies the container // ID identifies the container
ID() string ID() string
// Info returns the underlying container record type // Info returns the underlying container record type
Info() containers.Container Info(context.Context) (containers.Container, error)
// Delete removes the container // Delete removes the container
Delete(context.Context, ...DeleteOpts) error Delete(context.Context, ...DeleteOpts) error
// NewTask creates a new task based on the container metadata // NewTask creates a new task based on the container metadata
NewTask(context.Context, IOCreation, ...NewTaskOpts) (Task, error) NewTask(context.Context, IOCreation, ...NewTaskOpts) (Task, error)
// Spec returns the OCI runtime specification // Spec returns the OCI runtime specification
Spec() (*specs.Spec, error) Spec(context.Context) (*specs.Spec, error)
// Task returns the current task for the container // Task returns the current task for the container
// //
// If IOAttach options are passed the client will reattach to the IO for the running // If IOAttach options are passed the client will reattach to the IO for the running
@ -41,7 +40,7 @@ type Container interface {
// SetLabels sets the provided labels for the container and returns the final label set // SetLabels sets the provided labels for the container and returns the final label set
SetLabels(context.Context, map[string]string) (map[string]string, error) SetLabels(context.Context, map[string]string) (map[string]string, error)
// Extensions returns the extensions set on the container // Extensions returns the extensions set on the container
Extensions() map[string]prototypes.Any Extensions(context.Context) (map[string]prototypes.Any, error)
// Update a container // Update a container
Update(context.Context, ...UpdateContainerOpts) error Update(context.Context, ...UpdateContainerOpts) error
} }
@ -49,47 +48,45 @@ type Container interface {
func containerFromRecord(client *Client, c containers.Container) *container { func containerFromRecord(client *Client, c containers.Container) *container {
return &container{ return &container{
client: client, client: client,
c: c, id: c.ID,
} }
} }
var _ = (Container)(&container{}) var _ = (Container)(&container{})
type container struct { type container struct {
mu sync.Mutex
client *Client client *Client
c containers.Container id string
} }
// 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.id
} }
func (c *container) Info() containers.Container { func (c *container) Info(ctx context.Context) (containers.Container, error) {
return c.c return c.get(ctx)
} }
func (c *container) Labels(ctx context.Context) (map[string]string, error) { func (c *container) Extensions(ctx context.Context) (map[string]prototypes.Any, error) {
r, err := c.client.ContainerService().Get(ctx, c.ID()) r, err := c.get(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return r.Extensions, nil
}
c.c = r func (c *container) Labels(ctx context.Context) (map[string]string, error) {
r, err := c.get(ctx)
m := make(map[string]string, len(r.Labels)) if err != nil {
for k, v := range c.c.Labels { return nil, err
m[k] = v
} }
return r.Labels, nil
return m, nil
} }
func (c *container) SetLabels(ctx context.Context, labels map[string]string) (map[string]string, error) { func (c *container) SetLabels(ctx context.Context, labels map[string]string) (map[string]string, error) {
container := containers.Container{ container := containers.Container{
ID: c.ID(), ID: c.id,
Labels: labels, Labels: labels,
} }
@ -104,20 +101,17 @@ func (c *container) SetLabels(ctx context.Context, labels map[string]string) (ma
if err != nil { if err != nil {
return nil, err return nil, err
} }
return r.Labels, nil
c.c = r // update our local container
m := make(map[string]string, len(r.Labels))
for k, v := range c.c.Labels {
m[k] = v
}
return m, nil
} }
// 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(ctx context.Context) (*specs.Spec, error) {
r, err := c.get(ctx)
if err != nil {
return nil, err
}
var s specs.Spec var s specs.Spec
if err := json.Unmarshal(c.c.Spec.Value, &s); err != nil { if err := json.Unmarshal(r.Spec.Value, &s); err != nil {
return nil, err return nil, err
} }
return &s, nil return &s, nil
@ -125,20 +119,20 @@ 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, opts ...DeleteOpts) (err error) { func (c *container) Delete(ctx context.Context, opts ...DeleteOpts) error {
if _, err := c.Task(ctx, nil); err == nil { if _, err := c.loadTask(ctx, nil); err == nil {
return errors.Wrapf(errdefs.ErrFailedPrecondition, "cannot delete running task %v", c.ID()) return errors.Wrapf(errdefs.ErrFailedPrecondition, "cannot delete running task %v", c.id)
}
r, err := c.get(ctx)
if err != nil {
return err
} }
for _, o := range opts { for _, o := range opts {
if err := o(ctx, c.client, c.c); err != nil { if err := o(ctx, c.client, r); err != nil {
return err return err
} }
} }
return c.client.ContainerService().Delete(ctx, c.id)
if cerr := c.client.ContainerService().Delete(ctx, c.ID()); err == nil {
err = cerr
}
return err
} }
func (c *container) Task(ctx context.Context, attach IOAttach) (Task, error) { func (c *container) Task(ctx context.Context, attach IOAttach) (Task, error) {
@ -147,12 +141,16 @@ func (c *container) Task(ctx context.Context, attach IOAttach) (Task, error) {
// Image returns the image that the container is based on // Image returns the image that the container is based on
func (c *container) Image(ctx context.Context) (Image, error) { func (c *container) Image(ctx context.Context) (Image, error) {
if c.c.Image == "" { r, err := c.get(ctx)
return nil, errors.Wrapf(errdefs.ErrNotFound, "container not created from an image")
}
i, err := c.client.ImageService().Get(ctx, c.c.Image)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to get image for container") return nil, err
}
if r.Image == "" {
return nil, errors.Wrap(errdefs.ErrNotFound, "container not created from an image")
}
i, err := c.client.ImageService().Get(ctx, r.Image)
if err != nil {
return nil, errors.Wrapf(err, "failed to get image %s for container", r.Image)
} }
return &image{ return &image{
client: c.client, client: c.client,
@ -160,34 +158,30 @@ func (c *container) Image(ctx context.Context) (Image, error) {
}, nil }, nil
} }
func (c *container) Extensions() map[string]prototypes.Any {
c.mu.Lock()
defer c.mu.Unlock()
return c.c.Extensions
}
func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...NewTaskOpts) (Task, error) { func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...NewTaskOpts) (Task, error) {
c.mu.Lock() i, err := ioCreate(c.id)
defer c.mu.Unlock()
i, err := ioCreate(c.c.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := i.Config() cfg := i.Config()
request := &tasks.CreateTaskRequest{ request := &tasks.CreateTaskRequest{
ContainerID: c.c.ID, ContainerID: c.id,
Terminal: cfg.Terminal, Terminal: cfg.Terminal,
Stdin: cfg.Stdin, Stdin: cfg.Stdin,
Stdout: cfg.Stdout, Stdout: cfg.Stdout,
Stderr: cfg.Stderr, Stderr: cfg.Stderr,
} }
if c.c.SnapshotKey != "" { r, err := c.get(ctx)
if c.c.Snapshotter == "" { if err != nil {
return nil, err
}
if r.SnapshotKey != "" {
if r.Snapshotter == "" {
return nil, errors.Wrapf(errdefs.ErrInvalidArgument, "unable to resolve rootfs mounts without snapshotter on container") return nil, errors.Wrapf(errdefs.ErrInvalidArgument, "unable to resolve rootfs mounts without snapshotter on container")
} }
// get the rootfs from the snapshotter and add it to the request // get the rootfs from the snapshotter and add it to the request
mounts, err := c.client.SnapshotService(c.c.Snapshotter).Mounts(ctx, c.c.SnapshotKey) mounts, err := c.client.SnapshotService(r.Snapshotter).Mounts(ctx, r.SnapshotKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -224,7 +218,7 @@ func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...Ne
t := &task{ t := &task{
client: c.client, client: c.client,
io: i, io: i,
id: c.ID(), id: c.id,
} }
if info.Checkpoint != nil { if info.Checkpoint != nil {
request.Checkpoint = info.Checkpoint request.Checkpoint = info.Checkpoint
@ -238,29 +232,25 @@ func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...Ne
} }
func (c *container) Update(ctx context.Context, opts ...UpdateContainerOpts) error { func (c *container) Update(ctx context.Context, opts ...UpdateContainerOpts) error {
c.mu.Lock()
defer c.mu.Unlock()
// fetch the current container config before updating it // fetch the current container config before updating it
current, err := c.client.ContainerService().Get(ctx, c.ID()) r, err := c.get(ctx)
if err != nil { if err != nil {
return err return err
} }
for _, o := range opts { for _, o := range opts {
if err := o(ctx, c.client, &current); err != nil { if err := o(ctx, c.client, &r); err != nil {
return err return err
} }
} }
nc, err := c.client.ContainerService().Update(ctx, current) if _, err := c.client.ContainerService().Update(ctx, r); err != nil {
if err != nil {
return errdefs.FromGRPC(err) return errdefs.FromGRPC(err)
} }
c.c = nc
return nil return nil
} }
func (c *container) loadTask(ctx context.Context, ioAttach IOAttach) (Task, error) { func (c *container) loadTask(ctx context.Context, ioAttach IOAttach) (Task, error) {
response, err := c.client.TaskService().Get(ctx, &tasks.GetRequest{ response, err := c.client.TaskService().Get(ctx, &tasks.GetRequest{
ContainerID: c.c.ID, ContainerID: c.id,
}) })
if err != nil { if err != nil {
err = errdefs.FromGRPC(err) err = errdefs.FromGRPC(err)
@ -284,6 +274,10 @@ func (c *container) loadTask(ctx context.Context, ioAttach IOAttach) (Task, erro
return t, nil return t, nil
} }
func (c *container) get(ctx context.Context) (containers.Container, error) {
return c.client.ContainerService().Get(ctx, c.id)
}
func attachExistingIO(response *tasks.GetResponse, ioAttach IOAttach) (IO, error) { func attachExistingIO(response *tasks.GetResponse, ioAttach IOAttach) (IO, error) {
// get the existing fifo paths from the task information stored by the daemon // get the existing fifo paths from the task information stored by the daemon
paths := &FIFOSet{ paths := &FIFOSet{

View File

@ -513,7 +513,7 @@ func TestContainerAttachProcess(t *testing.T) {
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@ -71,7 +71,7 @@ func TestNewContainer(t *testing.T) {
if container.ID() != id { if container.ID() != id {
t.Errorf("expected container id %q but received %q", id, container.ID()) t.Errorf("expected container id %q but received %q", id, container.ID())
} }
if _, err = container.Spec(); err != nil { if _, err = container.Spec(ctx); err != nil {
t.Error(err) t.Error(err)
return return
} }
@ -268,7 +268,7 @@ func TestContainerExec(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -679,7 +679,7 @@ func TestContainerExecNoBinaryExists(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -910,7 +910,7 @@ func TestWaitStoppedProcess(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -1059,7 +1059,7 @@ func TestProcessForceDelete(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -1283,7 +1283,7 @@ func TestDeleteContainerExecCreated(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -1460,7 +1460,11 @@ func TestContainerExtensions(t *testing.T) {
defer container.Delete(ctx) defer container.Delete(ctx)
checkExt := func(container Container) { checkExt := func(container Container) {
cExts := container.Extensions() cExts, err := container.Extensions(ctx)
if err != nil {
t.Error(err)
return
}
if len(cExts) != 1 { if len(cExts) != 1 {
t.Errorf("expected 1 container extension") t.Errorf("expected 1 container extension")
} }
@ -1502,7 +1506,7 @@ func TestContainerUpdate(t *testing.T) {
} }
defer container.Delete(ctx) defer container.Delete(ctx)
spec, err := container.Spec() spec, err := container.Spec(ctx)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -1522,7 +1526,7 @@ func TestContainerUpdate(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if spec, err = container.Spec(); err != nil { if spec, err = container.Spec(ctx); err != nil {
t.Error(err) t.Error(err)
return return
} }