Skip to content

Commit

Permalink
Remove gogo/protobuf and adjust types
Browse files Browse the repository at this point in the history
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 containerd#6564.

Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys authored and Kirtana Ashok committed Jan 18, 2023
1 parent e92145d commit 59550fa
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 53 deletions.
6 changes: 3 additions & 3 deletions pkg/cri/server/container_stats_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions protobuf/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions protobuf/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 5 additions & 17 deletions protobuf/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
10 changes: 6 additions & 4 deletions protobuf/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions runtime/v1/shim/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions runtime/v2/runc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa
ns,
platform,
config,
&opts,
opts,
rootfs,
)
if err != nil {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions services/introspection/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{})
Expand All @@ -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)
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 59550fa

Please sign in to comment.