linux, linux/shim: remove error definitions

Since we now have a common set of error definitions, mapped to existing
error codes, we no longer need the specialized error codes used for
interaction with linux processes. The main issue was that string
matching was being used to map these to useful error codes. With this
change, we use errors defined in the `errdefs` package, which map
cleanly to GRPC error codes and are recoverable on either side of the
request.

The main focus of this PR was in removin these from the shim. We may
need follow ups to ensure error codes are preserved by the `Tasks`
service.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-07-18 15:41:05 -07:00
parent 6d305c741e
commit 6d0bcd5aec
12 changed files with 82 additions and 97 deletions

View File

@@ -7,25 +7,15 @@ import (
"strings"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/typeurl"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
var (
ErrNoImage = errors.New("container does not have an image")
ErrNoRunningTask = errors.New("no running task")
ErrDeleteRunningTask = errors.New("cannot delete container with running task")
ErrProcessExited = errors.New("process already exited")
ErrNoExecID = errors.New("exec id must be provided")
)
type DeleteOpts func(context.Context, *Client, containers.Container) error
type Container interface {
@@ -129,7 +119,7 @@ func WithRootFSDeletion(ctx context.Context, client *Client, c containers.Contai
// an error is returned if the container has running tasks
func (c *container) Delete(ctx context.Context, opts ...DeleteOpts) (err error) {
if _, err := c.Task(ctx, nil); err == nil {
return ErrDeleteRunningTask
return errors.Wrapf(errdefs.ErrFailedPrecondition, "cannot delete running task %v", c.ID())
}
for _, o := range opts {
if err := o(ctx, c.client, c.c); err != nil {
@@ -150,11 +140,11 @@ func (c *container) Task(ctx context.Context, attach IOAttach) (Task, error) {
// Image returns the image that the container is based on
func (c *container) Image(ctx context.Context) (Image, error) {
if c.c.Image == "" {
return nil, ErrNoImage
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 {
return nil, err
return nil, errors.Wrapf(err, "failed to get image for container")
}
return &image{
client: c.client,
@@ -229,8 +219,9 @@ func (c *container) loadTask(ctx context.Context, ioAttach IOAttach) (Task, erro
ContainerID: c.c.ID,
})
if err != nil {
if grpc.Code(errors.Cause(err)) == codes.NotFound {
return nil, ErrNoRunningTask
err = errdefs.FromGRPC(err)
if errdefs.IsNotFound(err) {
return nil, errors.Wrapf(err, "no running task found")
}
return nil, err
}