Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
shimv2: use the UnmarshalAny() to unmarshal Protobuf.Any
Browse files Browse the repository at this point in the history
It'll be much clear to unmarshal Protobuf.Any using
UnmarshalAny().

Fixes: #1130

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Jan 17, 2019
1 parent 3a2c0a6 commit 5ee838d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
11 changes: 8 additions & 3 deletions containerd-shim-v2/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions containerd-shim-v2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package containerdshim

import (
"context"
"encoding/json"
"io/ioutil"
"os"
sysexec "os/exec"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 5ee838d

Please sign in to comment.