Skip to content

Commit

Permalink
Remove gogo from cmd/containerd-shim-runhcs-v1/stats/stats.proto
Browse files Browse the repository at this point in the history
Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys committed Nov 3, 2022
1 parent 3c231c4 commit 8559da9
Show file tree
Hide file tree
Showing 8 changed files with 896 additions and 2,584 deletions.
47 changes: 47 additions & 0 deletions Protobuild-stats.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version = "2"
generators = ["go"]
plugins = ["grpc", "fieldpath"]

# Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project.
[includes]
# Include paths that will be added before all others. Typically, you want to
# treat the root of the project as an include, but this may not be necessary.
before = ["./protobuf"]

# Paths that should be treated as include roots in relation to the vendor
# directory. These will be calculated with the vendor directory nearest the
# target package.
packages = ["github.com/gogo/protobuf"]

# This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
[packages]
"gogoproto/gogo.proto" = "github.com/gogo/protobuf/gogoproto"
"google/protobuf/any.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/empty.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/struct.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/descriptor.proto" = "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"google/protobuf/field_mask.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/duration.proto" = "github.com/gogo/protobuf/types"
"github/containerd/cgroups/stats/v1/metrics.proto" = "github.com/containerd/cgroups/stats/v1"

[[overrides]]
prefixes = ["github.com/Microsoft/hcsshim/internal/shimdiag"]
plugins = ["ttrpc"]

[[overrides]]
prefixes = ["github.com/Microsoft/hcsshim/internal/extendedtask"]
plugins = ["ttrpc"]

[[overrides]]
prefixes = ["github.com/Microsoft/hcsshim/internal/computeagent"]
plugins = ["ttrpc"]

[[overrides]]
prefixes = ["github.com/Microsoft/hcsshim/internal/ncproxyttrpc"]
plugins = ["ttrpc"]

[[overrides]]
prefixes = ["github.com/Microsoft/hcsshim/internal/vmservice"]
plugins = ["ttrpc"]
47 changes: 47 additions & 0 deletions cmd/containerd-shim-runhcs-v1/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"

"github.com/gogo/protobuf/types"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoregistry"
)

func marshalAny(in interface{}) (*types.Any, error) {
switch t := in.(type) {
case proto.Message:
data, err := proto.Marshal(t)
if err != nil {
return nil, err
}
url := string(t.ProtoReflect().Descriptor().FullName())
if err != nil {
return nil, err
}
return &types.Any{Value: data, TypeUrl: url}, nil
default:
return nil, fmt.Errorf("failed to marshal %+v", in)
}
}

func unmarshalAny(in *types.Any) (interface{}, error) {
mt, err := protoregistry.GlobalTypes.FindMessageByURL(in.TypeUrl)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal %+v: %w", in, err)
}

empty := mt.New().Interface()

switch t := empty.(type) {
case proto.Message:
err := proto.Unmarshal(in.Value, t)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("failed to unmarshal %+v", in)
}

return empty, nil
}
2 changes: 1 addition & 1 deletion cmd/containerd-shim-runhcs-v1/service_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (s *service) statsInternal(ctx context.Context, req *task.StatsRequest) (*t
if err != nil {
return nil, err
}
any, err := typeurl.MarshalAny(stats)
any, err := marshalAny(stats)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal Statistics for task: %s", req.ID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func Test_PodShim_statsInternal_InitTaskID_Success(t *testing.T) {
if resp == nil || resp.Stats == nil {
t.Fatal("should have returned valid stats response")
}
statsI, err := typeurl.UnmarshalAny(resp.Stats)
statsI, err := unmarshalAny(resp.Stats)
if err != nil {
t.Fatalf("should not have failed to unmarshal StatsResponse got: %v", err)
}
Expand All @@ -730,7 +730,7 @@ func Test_PodShim_statsInternal_2ndTaskID_Success(t *testing.T) {
if resp == nil || resp.Stats == nil {
t.Fatal("should have returned valid stats response")
}
statsI, err := typeurl.UnmarshalAny(resp.Stats)
statsI, err := unmarshalAny(resp.Stats)
if err != nil {
t.Fatalf("should not have failed to unmarshal StatsResponse got: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func Test_TaskShim_statsInternal_InitTaskID_Success(t *testing.T) {
if resp == nil || resp.Stats == nil {
t.Fatal("should have returned valid stats response")
}
statsI, err := typeurl.UnmarshalAny(resp.Stats)
statsI, err := unmarshalAny(resp.Stats)
if err != nil {
t.Fatalf("should not have failed to unmarshal StatsResponse got: %v", err)
}
Expand Down
Loading

0 comments on commit 8559da9

Please sign in to comment.