Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Upgrade containerd/cgroups #1356

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Protobuild-stats.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Currently cmd/containerd-shim-runhcs-v1/stats package is generated by
# Google's protoc-gen-go since it includes the fields from containerd/cgroups.
#
# The rest is generated by gogo/protobuf since containerd's gogo -> google migration
# was only happening in 1.7.x and onwards.
version = "2"
generators = ["go"]

[packages]
"github/containerd/cgroups/stats/v1/metrics.proto" = "github.com/containerd/cgroups/stats/v1"
37 changes: 37 additions & 0 deletions cmd/containerd-shim-runhcs-v1/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"

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

// marshalAny marshals Google's proto.Message to Gogo's Any.
func marshalAny(in proto.Message) (*types.Any, error) {
data, err := proto.Marshal(in)
if err != nil {
return nil, err
}
url := string(in.ProtoReflect().Descriptor().FullName())
if err != nil {
return nil, err
}
return &types.Any{Value: data, TypeUrl: url}, nil
}

// unmarshalAny unmarshals Gogo's Any to Google's proto.Message
func unmarshalAny(in *types.Any) (proto.Message, error) {
mt, err := protoregistry.GlobalTypes.FindMessageByURL(in.TypeUrl)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal %+v: %w", in, err)
}

out := mt.New().Interface()
err = proto.Unmarshal(in.Value, out)
if err != nil {
return nil, err
}
return out, 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
2 changes: 1 addition & 1 deletion cmd/containerd-shim-runhcs-v1/service_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func verifyExpectedCgroupMetrics(t *testing.T, v *v1.Metrics) {
t.Fatal("expected non-nil Metrics.CPU")
}
if v.CPU.Usage.Total != 100 {
t.Fatalf("Expected Metrics.CPU.Usage == 100, got: %d", v.CPU.Usage)
t.Fatalf("Expected Metrics.CPU.Usage == 100, got: %+v", v.CPU.Usage)
}
if v.Memory == nil {
t.Fatal("expected non-nil Metrics.Memory")
Expand Down
Loading