From 5ee838d41206fd04c5bc0fcceabb505899fd82cc Mon Sep 17 00:00:00 2001 From: fupan Date: Thu, 17 Jan 2019 00:09:02 +0800 Subject: [PATCH] shimv2: use the UnmarshalAny() to unmarshal Protobuf.Any It'll be much clear to unmarshal Protobuf.Any using UnmarshalAny(). Fixes: #1130 Signed-off-by: fupan --- containerd-shim-v2/exec.go | 11 ++++++++--- containerd-shim-v2/service.go | 15 ++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/containerd-shim-v2/exec.go b/containerd-shim-v2/exec.go index 1a91461303..fcbb95e885 100644 --- a/containerd-shim-v2/exec.go +++ b/containerd-shim-v2/exec.go @@ -6,13 +6,13 @@ package containerdshim import ( - "encoding/json" "fmt" "strings" "time" "github.com/containerd/containerd/api/types/task" "github.com/containerd/containerd/errdefs" + "github.com/containerd/typeurl" googleProtobuf "github.com/gogo/protobuf/types" "github.com/kata-containers/runtime/virtcontainers/types" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -72,10 +72,15 @@ func newExec(c *container, stdin, stdout, stderr string, terminal bool, jspec *g } // process exec request - var spec specs.Process - if err := json.Unmarshal(jspec.Value, &spec); err != nil { + var spec *specs.Process + v, err := typeurl.UnmarshalAny(jspec) + if err != nil { return nil, err } + spec, ok := v.(*specs.Process) + if !ok { + return nil, errdefs.ToGRPCf(errdefs.ErrInvalidArgument, "Get an invalid spec type") + } if spec.ConsoleSize != nil { height = uint32(spec.ConsoleSize.Height) diff --git a/containerd-shim-v2/service.go b/containerd-shim-v2/service.go index 32dcf0cc1e..2b3707dc32 100644 --- a/containerd-shim-v2/service.go +++ b/containerd-shim-v2/service.go @@ -7,7 +7,6 @@ package containerdshim import ( "context" - "encoding/json" "io/ioutil" "os" sysexec "os/exec" @@ -31,6 +30,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" "github.com/containerd/containerd/api/types/task" + "github.com/containerd/typeurl" ptypes "github.com/gogo/protobuf/types" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -700,12 +700,17 @@ func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (*pt s.Lock() defer s.Unlock() - var resources specs.LinuxResources - if err := json.Unmarshal(r.Resources.Value, &resources); err != nil { - return empty, err + var resources *specs.LinuxResources + v, err := typeurl.UnmarshalAny(r.Resources) + if err != nil { + return nil, err + } + resources, ok := v.(*specs.LinuxResources) + if !ok { + return nil, errdefs.ToGRPCf(errdefs.ErrInvalidArgument, "Invalid resources type for %s", s.id) } - err := s.sandbox.UpdateContainer(r.ID, resources) + err = s.sandbox.UpdateContainer(r.ID, *resources) if err != nil { return nil, errdefs.ToGRPC(err) }