From 59550fac2f876be73b417e14773f20d484c236fe Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Tue, 19 Apr 2022 16:25:10 +0000 Subject: [PATCH] Remove gogo/protobuf and adjust types This commit migrates containerd/protobuf from github.com/gogo/protobuf to google.golang.org/protobuf and adjust types. Proto-generated structs cannot be passed as values. Fixes #6564. Signed-off-by: Kazuyoshi Kato --- pkg/cri/server/container_stats_list.go | 6 +++--- protobuf/any.go | 10 +++++----- protobuf/proto/proto.go | 10 +++++----- protobuf/timestamp.go | 22 +++++----------------- protobuf/types/types.go | 10 ++++++---- runtime/v1/shim/service.go | 8 ++++---- runtime/v2/runc/container.go | 12 ++++++------ services/introspection/local.go | 19 ++++++++++--------- 8 files changed, 44 insertions(+), 53 deletions(-) diff --git a/pkg/cri/server/container_stats_list.go b/pkg/cri/server/container_stats_list.go index 865c6bbf3616f..6b02703867c91 100644 --- a/pkg/cri/server/container_stats_list.go +++ b/pkg/cri/server/container_stats_list.go @@ -36,7 +36,7 @@ func (c *criService) ListContainerStats( if err != nil { return nil, fmt.Errorf("failed to build metrics request: %w", err) } - resp, err := c.client.TaskService().Metrics(ctx, &request) + resp, err := c.client.TaskService().Metrics(ctx, request) if err != nil { return nil, fmt.Errorf("failed to fetch metrics for tasks: %w", err) } @@ -79,8 +79,8 @@ func (c *criService) normalizeContainerStatsFilter(filter *runtime.ContainerStat // the information in the stats request and the containerStore func (c *criService) buildTaskMetricsRequest( r *runtime.ListContainerStatsRequest, -) (tasks.MetricsRequest, []containerstore.Container, error) { - var req tasks.MetricsRequest +) (*tasks.MetricsRequest, []containerstore.Container, error) { + req := &tasks.MetricsRequest{} if r.GetFilter() == nil { return req, c.containerStore.List(), nil } diff --git a/protobuf/any.go b/protobuf/any.go index f0f7b4e23fbdc..8be32916253dc 100644 --- a/protobuf/any.go +++ b/protobuf/any.go @@ -17,28 +17,28 @@ package protobuf import ( - "github.com/containerd/containerd/protobuf/types" "github.com/containerd/typeurl" + "google.golang.org/protobuf/types/known/anypb" ) // FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any. -func FromAny(from typeurl.Any) *types.Any { +func FromAny(from typeurl.Any) *anypb.Any { if from == nil { return nil } - if pbany, ok := from.(*types.Any); ok { + if pbany, ok := from.(*anypb.Any); ok { return pbany } - return &types.Any{ + return &anypb.Any{ TypeUrl: from.GetTypeUrl(), Value: from.GetValue(), } } // FromAny converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any. -func MarshalAnyToProto(from interface{}) (*types.Any, error) { +func MarshalAnyToProto(from interface{}) (*anypb.Any, error) { any, err := typeurl.MarshalAny(from) if err != nil { return nil, err diff --git a/protobuf/proto/proto.go b/protobuf/proto/proto.go index 6f5b75ccebff6..6c5e7b75e3add 100644 --- a/protobuf/proto/proto.go +++ b/protobuf/proto/proto.go @@ -18,13 +18,13 @@ package proto import ( - gogo "github.com/gogo/protobuf/proto" + google "google.golang.org/protobuf/proto" ) -func Marshal(input gogo.Message) ([]byte, error) { - return gogo.Marshal(input) +func Marshal(input google.Message) ([]byte, error) { + return google.Marshal(input) } -func Unmarshal(input []byte, output gogo.Message) error { - return gogo.Unmarshal(input, output) +func Unmarshal(input []byte, output google.Message) error { + return google.Unmarshal(input, output) } diff --git a/protobuf/timestamp.go b/protobuf/timestamp.go index 4edef09dc0fa5..0615f823b04a5 100644 --- a/protobuf/timestamp.go +++ b/protobuf/timestamp.go @@ -19,30 +19,18 @@ package protobuf import ( "time" - "github.com/gogo/protobuf/types" + "google.golang.org/protobuf/types/known/timestamppb" ) // Once we migrate off from gogo/protobuf, we can use the function below, which don't return any errors. // https://github.com/protocolbuffers/protobuf-go/blob/v1.28.0/types/known/timestamppb/timestamp.pb.go#L200-L208 // ToTimestamp creates protobuf's Timestamp from time.Time. -func ToTimestamp(from time.Time) *types.Timestamp { - pt, err := types.TimestampProto(from) - if err != nil { - panic(err) - } - return pt +func ToTimestamp(from time.Time) *timestamppb.Timestamp { + return timestamppb.New(from) } // FromTimestamp creates time.Time from protobuf's Timestamp. -func FromTimestamp(from *types.Timestamp) time.Time { - if from == nil { - // Return time.Time's zero value as like timestamppb. - return time.Time{}.UTC() - } - tt, err := types.TimestampFromProto(from) - if err != nil { - panic(err) - } - return tt +func FromTimestamp(from *timestamppb.Timestamp) time.Time { + return from.AsTime() } diff --git a/protobuf/types/types.go b/protobuf/types/types.go index e668f123b8b81..1a920a2f98e71 100644 --- a/protobuf/types/types.go +++ b/protobuf/types/types.go @@ -18,9 +18,11 @@ package types import ( - gogo "github.com/gogo/protobuf/types" + "google.golang.org/genproto/protobuf/field_mask" + "google.golang.org/protobuf/types/known/anypb" + "google.golang.org/protobuf/types/known/emptypb" ) -type Empty = gogo.Empty -type Any = gogo.Any -type FieldMask = gogo.FieldMask +type Empty = emptypb.Empty +type Any = anypb.Any +type FieldMask = field_mask.FieldMask diff --git a/runtime/v1/shim/service.go b/runtime/v1/shim/service.go index 81ecf0773cc46..45b1a5c16d500 100644 --- a/runtime/v1/shim/service.go +++ b/runtime/v1/shim/service.go @@ -449,13 +449,13 @@ func (s *Service) Checkpoint(ctx context.Context, r *shimapi.CheckpointTaskReque if err != nil { return nil, err } - var options runctypes.CheckpointOptions + var options *runctypes.CheckpointOptions if r.Options != nil { v, err := typeurl.UnmarshalAny(r.Options) if err != nil { return nil, err } - options = *v.(*runctypes.CheckpointOptions) + options = v.(*runctypes.CheckpointOptions) } if err := p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{ Path: r.Path, @@ -644,13 +644,13 @@ func getTopic(ctx context.Context, e interface{}) string { } func newInit(ctx context.Context, path, workDir, runtimeRoot, namespace string, systemdCgroup bool, platform stdio.Platform, r *process.CreateConfig, rootfs string) (*process.Init, error) { - var options runctypes.CreateOptions + options := &runctypes.CreateOptions{} if r.Options != nil { v, err := typeurl.UnmarshalAny(r.Options) if err != nil { return nil, err } - options = *v.(*runctypes.CreateOptions) + options = v.(*runctypes.CreateOptions) } runtime := process.NewRunc(runtimeRoot, path, namespace, r.Runtime, systemdCgroup) diff --git a/runtime/v2/runc/container.go b/runtime/v2/runc/container.go index eb972ff6b44d4..2fa1734874655 100644 --- a/runtime/v2/runc/container.go +++ b/runtime/v2/runc/container.go @@ -48,14 +48,14 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa return nil, fmt.Errorf("create namespace: %w", err) } - var opts options.Options + opts := &options.Options{} if r.Options.GetValue() != nil { v, err := typeurl.UnmarshalAny(r.Options) if err != nil { return nil, err } if v != nil { - opts = *v.(*options.Options) + opts = v.(*options.Options) } } @@ -123,7 +123,7 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa ns, platform, config, - &opts, + opts, rootfs, ) if err != nil { @@ -188,7 +188,7 @@ func ReadOptions(path string) (*options.Options, error) { } // WriteOptions writes the options information into the path -func WriteOptions(path string, opts options.Options) error { +func WriteOptions(path string, opts *options.Options) error { data, err := json.Marshal(opts) if err != nil { return err @@ -467,13 +467,13 @@ func (c *Container) Checkpoint(ctx context.Context, r *task.CheckpointTaskReques if err != nil { return err } - var opts options.CheckpointOptions + var opts *options.CheckpointOptions if r.Options != nil { v, err := typeurl.UnmarshalAny(r.Options) if err != nil { return err } - opts = *v.(*options.CheckpointOptions) + opts = v.(*options.CheckpointOptions) } return p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{ Path: r.Path, diff --git a/services/introspection/local.go b/services/introspection/local.go index 33f53f150eb38..be26f5250763c 100644 --- a/services/introspection/local.go +++ b/services/introspection/local.go @@ -29,8 +29,9 @@ import ( "github.com/containerd/containerd/plugin" ptypes "github.com/containerd/containerd/protobuf/types" "github.com/containerd/containerd/services" - "github.com/gogo/googleapis/google/rpc" "github.com/google/uuid" + "google.golang.org/genproto/googleapis/rpc/code" + rpc "google.golang.org/genproto/googleapis/rpc/status" "google.golang.org/grpc" "google.golang.org/grpc/status" ) @@ -55,7 +56,7 @@ type Local struct { mu sync.Mutex root string plugins *plugin.Set - pluginCache []api.Plugin + pluginCache []*api.Plugin } var _ = (api.IntrospectionClient)(&Local{}) @@ -79,7 +80,7 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc. for _, p := range allPlugins { p := p if filter.Match(adaptPlugin(p)) { - plugins = append(plugins, &p) + plugins = append(plugins, p) } } @@ -88,7 +89,7 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc. }, nil } -func (l *Local) getPlugins() []api.Plugin { +func (l *Local) getPlugins() []*api.Plugin { l.mu.Lock() defer l.mu.Unlock() plugins := l.plugins.GetAll() @@ -148,7 +149,7 @@ func (l *Local) uuidPath() string { } func adaptPlugin(o interface{}) filters.Adaptor { - obj := o.(api.Plugin) + obj := o.(*api.Plugin) return filters.AdapterFunc(func(fieldpath []string) (string, bool) { if len(fieldpath) == 0 { return "", false @@ -174,8 +175,8 @@ func adaptPlugin(o interface{}) filters.Adaptor { }) } -func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin { - var pluginsPB []api.Plugin +func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin { + var pluginsPB []*api.Plugin for _, p := range plugins { var platforms []*types.Platform for _, p := range p.Meta.Platforms { @@ -209,13 +210,13 @@ func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin { } } else { initErr = &rpc.Status{ - Code: int32(rpc.UNKNOWN), + Code: int32(code.Code_UNKNOWN), Message: err.Error(), } } } - pluginsPB = append(pluginsPB, api.Plugin{ + pluginsPB = append(pluginsPB, &api.Plugin{ Type: p.Registration.Type.String(), ID: p.Registration.ID, Requires: requires,