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 <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2022-04-19 16:25:10 +00:00
parent fd37cc75be
commit e3db7de8f5
8 changed files with 44 additions and 53 deletions

View File

@ -36,7 +36,7 @@ func (c *criService) ListContainerStats(
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to build metrics request: %w", err) 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 { if err != nil {
return nil, fmt.Errorf("failed to fetch metrics for tasks: %w", err) 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 // the information in the stats request and the containerStore
func (c *criService) buildTaskMetricsRequest( func (c *criService) buildTaskMetricsRequest(
r *runtime.ListContainerStatsRequest, r *runtime.ListContainerStatsRequest,
) (tasks.MetricsRequest, []containerstore.Container, error) { ) (*tasks.MetricsRequest, []containerstore.Container, error) {
var req tasks.MetricsRequest req := &tasks.MetricsRequest{}
if r.GetFilter() == nil { if r.GetFilter() == nil {
return req, c.containerStore.List(), nil return req, c.containerStore.List(), nil
} }

View File

@ -17,28 +17,28 @@
package protobuf package protobuf
import ( import (
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"google.golang.org/protobuf/types/known/anypb"
) )
// FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any. // 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 { if from == nil {
return nil return nil
} }
if pbany, ok := from.(*types.Any); ok { if pbany, ok := from.(*anypb.Any); ok {
return pbany return pbany
} }
return &types.Any{ return &anypb.Any{
TypeUrl: from.GetTypeUrl(), TypeUrl: from.GetTypeUrl(),
Value: from.GetValue(), Value: from.GetValue(),
} }
} }
// FromAny converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any. // 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) any, err := typeurl.MarshalAny(from)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -18,13 +18,13 @@
package proto package proto
import ( import (
gogo "github.com/gogo/protobuf/proto" google "google.golang.org/protobuf/proto"
) )
func Marshal(input gogo.Message) ([]byte, error) { func Marshal(input google.Message) ([]byte, error) {
return gogo.Marshal(input) return google.Marshal(input)
} }
func Unmarshal(input []byte, output gogo.Message) error { func Unmarshal(input []byte, output google.Message) error {
return gogo.Unmarshal(input, output) return google.Unmarshal(input, output)
} }

View File

@ -19,30 +19,18 @@ package protobuf
import ( import (
"time" "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. // 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 // 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. // ToTimestamp creates protobuf's Timestamp from time.Time.
func ToTimestamp(from time.Time) *types.Timestamp { func ToTimestamp(from time.Time) *timestamppb.Timestamp {
pt, err := types.TimestampProto(from) return timestamppb.New(from)
if err != nil {
panic(err)
}
return pt
} }
// FromTimestamp creates time.Time from protobuf's Timestamp. // FromTimestamp creates time.Time from protobuf's Timestamp.
func FromTimestamp(from *types.Timestamp) time.Time { func FromTimestamp(from *timestamppb.Timestamp) time.Time {
if from == nil { return from.AsTime()
// 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
} }

View File

@ -18,9 +18,11 @@
package types package types
import ( 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 Empty = emptypb.Empty
type Any = gogo.Any type Any = anypb.Any
type FieldMask = gogo.FieldMask type FieldMask = field_mask.FieldMask

View File

@ -449,13 +449,13 @@ func (s *Service) Checkpoint(ctx context.Context, r *shimapi.CheckpointTaskReque
if err != nil { if err != nil {
return nil, err return nil, err
} }
var options runctypes.CheckpointOptions var options *runctypes.CheckpointOptions
if r.Options != nil { if r.Options != nil {
v, err := typeurl.UnmarshalAny(r.Options) v, err := typeurl.UnmarshalAny(r.Options)
if err != nil { if err != nil {
return nil, err return nil, err
} }
options = *v.(*runctypes.CheckpointOptions) options = v.(*runctypes.CheckpointOptions)
} }
if err := p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{ if err := p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{
Path: r.Path, 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) { 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 { if r.Options != nil {
v, err := typeurl.UnmarshalAny(r.Options) v, err := typeurl.UnmarshalAny(r.Options)
if err != nil { if err != nil {
return nil, err return nil, err
} }
options = *v.(*runctypes.CreateOptions) options = v.(*runctypes.CreateOptions)
} }
runtime := process.NewRunc(runtimeRoot, path, namespace, r.Runtime, systemdCgroup) runtime := process.NewRunc(runtimeRoot, path, namespace, r.Runtime, systemdCgroup)

View File

@ -48,14 +48,14 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa
return nil, fmt.Errorf("create namespace: %w", err) return nil, fmt.Errorf("create namespace: %w", err)
} }
var opts options.Options opts := &options.Options{}
if r.Options.GetValue() != nil { if r.Options.GetValue() != nil {
v, err := typeurl.UnmarshalAny(r.Options) v, err := typeurl.UnmarshalAny(r.Options)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if v != nil { 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, ns,
platform, platform,
config, config,
&opts, opts,
rootfs, rootfs,
) )
if err != nil { if err != nil {
@ -188,7 +188,7 @@ func ReadOptions(path string) (*options.Options, error) {
} }
// WriteOptions writes the options information into the path // 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) data, err := json.Marshal(opts)
if err != nil { if err != nil {
return err return err
@ -467,13 +467,13 @@ func (c *Container) Checkpoint(ctx context.Context, r *task.CheckpointTaskReques
if err != nil { if err != nil {
return err return err
} }
var opts options.CheckpointOptions var opts *options.CheckpointOptions
if r.Options != nil { if r.Options != nil {
v, err := typeurl.UnmarshalAny(r.Options) v, err := typeurl.UnmarshalAny(r.Options)
if err != nil { if err != nil {
return err return err
} }
opts = *v.(*options.CheckpointOptions) opts = v.(*options.CheckpointOptions)
} }
return p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{ return p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{
Path: r.Path, Path: r.Path,

View File

@ -29,8 +29,9 @@ import (
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
ptypes "github.com/containerd/containerd/protobuf/types" ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/services" "github.com/containerd/containerd/services"
"github.com/gogo/googleapis/google/rpc"
"github.com/google/uuid" "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"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -55,7 +56,7 @@ type Local struct {
mu sync.Mutex mu sync.Mutex
root string root string
plugins *plugin.Set plugins *plugin.Set
pluginCache []api.Plugin pluginCache []*api.Plugin
} }
var _ = (api.IntrospectionClient)(&Local{}) var _ = (api.IntrospectionClient)(&Local{})
@ -79,7 +80,7 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc.
for _, p := range allPlugins { for _, p := range allPlugins {
p := p p := p
if filter.Match(adaptPlugin(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 }, nil
} }
func (l *Local) getPlugins() []api.Plugin { func (l *Local) getPlugins() []*api.Plugin {
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
plugins := l.plugins.GetAll() plugins := l.plugins.GetAll()
@ -148,7 +149,7 @@ func (l *Local) uuidPath() string {
} }
func adaptPlugin(o interface{}) filters.Adaptor { func adaptPlugin(o interface{}) filters.Adaptor {
obj := o.(api.Plugin) obj := o.(*api.Plugin)
return filters.AdapterFunc(func(fieldpath []string) (string, bool) { return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
if len(fieldpath) == 0 { if len(fieldpath) == 0 {
return "", false return "", false
@ -174,8 +175,8 @@ func adaptPlugin(o interface{}) filters.Adaptor {
}) })
} }
func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin { func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
var pluginsPB []api.Plugin var pluginsPB []*api.Plugin
for _, p := range plugins { for _, p := range plugins {
var platforms []*types.Platform var platforms []*types.Platform
for _, p := range p.Meta.Platforms { for _, p := range p.Meta.Platforms {
@ -209,13 +210,13 @@ func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin {
} }
} else { } else {
initErr = &rpc.Status{ initErr = &rpc.Status{
Code: int32(rpc.UNKNOWN), Code: int32(code.Code_UNKNOWN),
Message: err.Error(), Message: err.Error(),
} }
} }
} }
pluginsPB = append(pluginsPB, api.Plugin{ pluginsPB = append(pluginsPB, &api.Plugin{
Type: p.Registration.Type.String(), Type: p.Registration.Type.String(),
ID: p.Registration.ID, ID: p.Registration.ID,
Requires: requires, Requires: requires,