Add grpc methods to errdefs

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-11-10 12:09:48 -05:00
parent c81788b129
commit 8376b50b19
3 changed files with 24 additions and 7 deletions

View File

@ -4,7 +4,6 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -61,7 +60,7 @@ func FromGRPC(err error) error {
var cls error // divide these into error classes, becomes the cause var cls error // divide these into error classes, becomes the cause
switch grpc.Code(err) { switch code(err) {
case codes.InvalidArgument: case codes.InvalidArgument:
cls = ErrInvalidArgument cls = ErrInvalidArgument
case codes.AlreadyExists: case codes.AlreadyExists:
@ -94,7 +93,7 @@ func FromGRPC(err error) error {
// Effectively, we just remove the string of cls from the end of err if it // Effectively, we just remove the string of cls from the end of err if it
// appears there. // appears there.
func rebaseMessage(cls error, err error) string { func rebaseMessage(cls error, err error) string {
desc := grpc.ErrorDesc(err) desc := errDesc(err)
clss := cls.Error() clss := cls.Error()
if desc == clss { if desc == clss {
return "" return ""
@ -107,3 +106,17 @@ func isGRPCError(err error) bool {
_, ok := status.FromError(err) _, ok := status.FromError(err)
return ok return ok
} }
func code(err error) codes.Code {
if s, ok := status.FromError(err); ok {
return s.Code()
}
return codes.Unknown
}
func errDesc(err error) string {
if s, ok := status.FromError(err); ok {
return s.Message()
}
return err.Error()
}

View File

@ -268,7 +268,8 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
if err != nil { if err != nil {
return nil, errdefs.FromGRPC(err) return nil, errdefs.FromGRPC(err)
} }
t, err := newTask(id, namespace, int(cr.Pid), s, r.monitor, r.events, proc.NewRunc(ropts.RuntimeRoot, sopts.Bundle, namespace, rt, ropts.CriuPath, ropts.SystemdCgroup)) t, err := newTask(id, namespace, int(cr.Pid), s, r.monitor, r.events,
proc.NewRunc(ropts.RuntimeRoot, sopts.Bundle, namespace, rt, ropts.CriuPath, ropts.SystemdCgroup))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -396,6 +397,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
filepath.Join(r.state, ns, id), filepath.Join(r.state, ns, id),
filepath.Join(r.root, ns, id), filepath.Join(r.root, ns, id),
) )
ctx = namespaces.WithNamespace(ctx, ns)
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile)) pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
s, err := bundle.NewShimClient(ctx, ns, ShimConnect(), nil) s, err := bundle.NewShimClient(ctx, ns, ShimConnect(), nil)
if err != nil { if err != nil {
@ -417,7 +419,8 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
continue continue
} }
t, err := newTask(id, ns, pid, s, r.monitor, r.events, proc.NewRunc(ropts.RuntimeRoot, bundle.path, ns, ropts.Runtime, ropts.CriuPath, ropts.SystemdCgroup)) t, err := newTask(id, ns, pid, s, r.monitor, r.events,
proc.NewRunc(ropts.RuntimeRoot, bundle.path, ns, ropts.Runtime, ropts.CriuPath, ropts.SystemdCgroup))
if err != nil { if err != nil {
log.G(ctx).WithError(err).Error("loading task type") log.G(ctx).WithError(err).Error("loading task type")
continue continue
@ -543,5 +546,5 @@ func (r *Runtime) getRuncOptions(ctx context.Context, id string) (*runctypes.Run
return ropts, nil return ropts, nil
} }
return &runcopts.RuncOptions{}, nil return &runctypes.RuncOptions{}, nil
} }

View File

@ -19,6 +19,7 @@ import (
"github.com/containerd/containerd/linux/runctypes" "github.com/containerd/containerd/linux/runctypes"
shimapi "github.com/containerd/containerd/linux/shim/v1" shimapi "github.com/containerd/containerd/linux/shim/v1"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/reaper" "github.com/containerd/containerd/reaper"
"github.com/containerd/containerd/runtime" "github.com/containerd/containerd/runtime"
runc "github.com/containerd/go-runc" runc "github.com/containerd/go-runc"
@ -46,7 +47,7 @@ func NewService(config Config, publisher events.Publisher) (*Service, error) {
if config.Namespace == "" { if config.Namespace == "" {
return nil, fmt.Errorf("shim namespace cannot be empty") return nil, fmt.Errorf("shim namespace cannot be empty")
} }
ctx := context.Background() ctx := namespaces.WithNamespace(context.Background(), config.Namespace)
ctx = log.WithLogger(ctx, logrus.WithFields(logrus.Fields{ ctx = log.WithLogger(ctx, logrus.WithFields(logrus.Fields{
"namespace": config.Namespace, "namespace": config.Namespace,
"path": config.Path, "path": config.Path,