Skip to content

Commit 59550fa

Browse files
kzysKirtana Ashok
authored and
Kirtana Ashok
committed
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 containerd#6564. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent e92145d commit 59550fa

File tree

8 files changed

+44
-53
lines changed

8 files changed

+44
-53
lines changed

pkg/cri/server/container_stats_list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c *criService) ListContainerStats(
3636
if err != nil {
3737
return nil, fmt.Errorf("failed to build metrics request: %w", err)
3838
}
39-
resp, err := c.client.TaskService().Metrics(ctx, &request)
39+
resp, err := c.client.TaskService().Metrics(ctx, request)
4040
if err != nil {
4141
return nil, fmt.Errorf("failed to fetch metrics for tasks: %w", err)
4242
}
@@ -79,8 +79,8 @@ func (c *criService) normalizeContainerStatsFilter(filter *runtime.ContainerStat
7979
// the information in the stats request and the containerStore
8080
func (c *criService) buildTaskMetricsRequest(
8181
r *runtime.ListContainerStatsRequest,
82-
) (tasks.MetricsRequest, []containerstore.Container, error) {
83-
var req tasks.MetricsRequest
82+
) (*tasks.MetricsRequest, []containerstore.Container, error) {
83+
req := &tasks.MetricsRequest{}
8484
if r.GetFilter() == nil {
8585
return req, c.containerStore.List(), nil
8686
}

protobuf/any.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717
package protobuf
1818

1919
import (
20-
"github.com/containerd/containerd/protobuf/types"
2120
"github.com/containerd/typeurl"
21+
"google.golang.org/protobuf/types/known/anypb"
2222
)
2323

2424
// FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any.
25-
func FromAny(from typeurl.Any) *types.Any {
25+
func FromAny(from typeurl.Any) *anypb.Any {
2626
if from == nil {
2727
return nil
2828
}
2929

30-
if pbany, ok := from.(*types.Any); ok {
30+
if pbany, ok := from.(*anypb.Any); ok {
3131
return pbany
3232
}
3333

34-
return &types.Any{
34+
return &anypb.Any{
3535
TypeUrl: from.GetTypeUrl(),
3636
Value: from.GetValue(),
3737
}
3838
}
3939

4040
// FromAny converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any.
41-
func MarshalAnyToProto(from interface{}) (*types.Any, error) {
41+
func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
4242
any, err := typeurl.MarshalAny(from)
4343
if err != nil {
4444
return nil, err

protobuf/proto/proto.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
package proto
1919

2020
import (
21-
gogo "github.com/gogo/protobuf/proto"
21+
google "google.golang.org/protobuf/proto"
2222
)
2323

24-
func Marshal(input gogo.Message) ([]byte, error) {
25-
return gogo.Marshal(input)
24+
func Marshal(input google.Message) ([]byte, error) {
25+
return google.Marshal(input)
2626
}
2727

28-
func Unmarshal(input []byte, output gogo.Message) error {
29-
return gogo.Unmarshal(input, output)
28+
func Unmarshal(input []byte, output google.Message) error {
29+
return google.Unmarshal(input, output)
3030
}

protobuf/timestamp.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,18 @@ package protobuf
1919
import (
2020
"time"
2121

22-
"github.com/gogo/protobuf/types"
22+
"google.golang.org/protobuf/types/known/timestamppb"
2323
)
2424

2525
// Once we migrate off from gogo/protobuf, we can use the function below, which don't return any errors.
2626
// https://github.com/protocolbuffers/protobuf-go/blob/v1.28.0/types/known/timestamppb/timestamp.pb.go#L200-L208
2727

2828
// ToTimestamp creates protobuf's Timestamp from time.Time.
29-
func ToTimestamp(from time.Time) *types.Timestamp {
30-
pt, err := types.TimestampProto(from)
31-
if err != nil {
32-
panic(err)
33-
}
34-
return pt
29+
func ToTimestamp(from time.Time) *timestamppb.Timestamp {
30+
return timestamppb.New(from)
3531
}
3632

3733
// FromTimestamp creates time.Time from protobuf's Timestamp.
38-
func FromTimestamp(from *types.Timestamp) time.Time {
39-
if from == nil {
40-
// Return time.Time's zero value as like timestamppb.
41-
return time.Time{}.UTC()
42-
}
43-
tt, err := types.TimestampFromProto(from)
44-
if err != nil {
45-
panic(err)
46-
}
47-
return tt
34+
func FromTimestamp(from *timestamppb.Timestamp) time.Time {
35+
return from.AsTime()
4836
}

protobuf/types/types.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package types
1919

2020
import (
21-
gogo "github.com/gogo/protobuf/types"
21+
"google.golang.org/genproto/protobuf/field_mask"
22+
"google.golang.org/protobuf/types/known/anypb"
23+
"google.golang.org/protobuf/types/known/emptypb"
2224
)
2325

24-
type Empty = gogo.Empty
25-
type Any = gogo.Any
26-
type FieldMask = gogo.FieldMask
26+
type Empty = emptypb.Empty
27+
type Any = anypb.Any
28+
type FieldMask = field_mask.FieldMask

runtime/v1/shim/service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,13 @@ func (s *Service) Checkpoint(ctx context.Context, r *shimapi.CheckpointTaskReque
449449
if err != nil {
450450
return nil, err
451451
}
452-
var options runctypes.CheckpointOptions
452+
var options *runctypes.CheckpointOptions
453453
if r.Options != nil {
454454
v, err := typeurl.UnmarshalAny(r.Options)
455455
if err != nil {
456456
return nil, err
457457
}
458-
options = *v.(*runctypes.CheckpointOptions)
458+
options = v.(*runctypes.CheckpointOptions)
459459
}
460460
if err := p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{
461461
Path: r.Path,
@@ -644,13 +644,13 @@ func getTopic(ctx context.Context, e interface{}) string {
644644
}
645645

646646
func newInit(ctx context.Context, path, workDir, runtimeRoot, namespace string, systemdCgroup bool, platform stdio.Platform, r *process.CreateConfig, rootfs string) (*process.Init, error) {
647-
var options runctypes.CreateOptions
647+
options := &runctypes.CreateOptions{}
648648
if r.Options != nil {
649649
v, err := typeurl.UnmarshalAny(r.Options)
650650
if err != nil {
651651
return nil, err
652652
}
653-
options = *v.(*runctypes.CreateOptions)
653+
options = v.(*runctypes.CreateOptions)
654654
}
655655

656656
runtime := process.NewRunc(runtimeRoot, path, namespace, r.Runtime, systemdCgroup)

runtime/v2/runc/container.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa
4848
return nil, fmt.Errorf("create namespace: %w", err)
4949
}
5050

51-
var opts options.Options
51+
opts := &options.Options{}
5252
if r.Options.GetValue() != nil {
5353
v, err := typeurl.UnmarshalAny(r.Options)
5454
if err != nil {
5555
return nil, err
5656
}
5757
if v != nil {
58-
opts = *v.(*options.Options)
58+
opts = v.(*options.Options)
5959
}
6060
}
6161

@@ -123,7 +123,7 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa
123123
ns,
124124
platform,
125125
config,
126-
&opts,
126+
opts,
127127
rootfs,
128128
)
129129
if err != nil {
@@ -188,7 +188,7 @@ func ReadOptions(path string) (*options.Options, error) {
188188
}
189189

190190
// WriteOptions writes the options information into the path
191-
func WriteOptions(path string, opts options.Options) error {
191+
func WriteOptions(path string, opts *options.Options) error {
192192
data, err := json.Marshal(opts)
193193
if err != nil {
194194
return err
@@ -467,13 +467,13 @@ func (c *Container) Checkpoint(ctx context.Context, r *task.CheckpointTaskReques
467467
if err != nil {
468468
return err
469469
}
470-
var opts options.CheckpointOptions
470+
var opts *options.CheckpointOptions
471471
if r.Options != nil {
472472
v, err := typeurl.UnmarshalAny(r.Options)
473473
if err != nil {
474474
return err
475475
}
476-
opts = *v.(*options.CheckpointOptions)
476+
opts = v.(*options.CheckpointOptions)
477477
}
478478
return p.(*process.Init).Checkpoint(ctx, &process.CheckpointConfig{
479479
Path: r.Path,

services/introspection/local.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import (
2929
"github.com/containerd/containerd/plugin"
3030
ptypes "github.com/containerd/containerd/protobuf/types"
3131
"github.com/containerd/containerd/services"
32-
"github.com/gogo/googleapis/google/rpc"
3332
"github.com/google/uuid"
33+
"google.golang.org/genproto/googleapis/rpc/code"
34+
rpc "google.golang.org/genproto/googleapis/rpc/status"
3435
"google.golang.org/grpc"
3536
"google.golang.org/grpc/status"
3637
)
@@ -55,7 +56,7 @@ type Local struct {
5556
mu sync.Mutex
5657
root string
5758
plugins *plugin.Set
58-
pluginCache []api.Plugin
59+
pluginCache []*api.Plugin
5960
}
6061

6162
var _ = (api.IntrospectionClient)(&Local{})
@@ -79,7 +80,7 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc.
7980
for _, p := range allPlugins {
8081
p := p
8182
if filter.Match(adaptPlugin(p)) {
82-
plugins = append(plugins, &p)
83+
plugins = append(plugins, p)
8384
}
8485
}
8586

@@ -88,7 +89,7 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc.
8889
}, nil
8990
}
9091

91-
func (l *Local) getPlugins() []api.Plugin {
92+
func (l *Local) getPlugins() []*api.Plugin {
9293
l.mu.Lock()
9394
defer l.mu.Unlock()
9495
plugins := l.plugins.GetAll()
@@ -148,7 +149,7 @@ func (l *Local) uuidPath() string {
148149
}
149150

150151
func adaptPlugin(o interface{}) filters.Adaptor {
151-
obj := o.(api.Plugin)
152+
obj := o.(*api.Plugin)
152153
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
153154
if len(fieldpath) == 0 {
154155
return "", false
@@ -174,8 +175,8 @@ func adaptPlugin(o interface{}) filters.Adaptor {
174175
})
175176
}
176177

177-
func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin {
178-
var pluginsPB []api.Plugin
178+
func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
179+
var pluginsPB []*api.Plugin
179180
for _, p := range plugins {
180181
var platforms []*types.Platform
181182
for _, p := range p.Meta.Platforms {
@@ -209,13 +210,13 @@ func pluginsToPB(plugins []*plugin.Plugin) []api.Plugin {
209210
}
210211
} else {
211212
initErr = &rpc.Status{
212-
Code: int32(rpc.UNKNOWN),
213+
Code: int32(code.Code_UNKNOWN),
213214
Message: err.Error(),
214215
}
215216
}
216217
}
217218

218-
pluginsPB = append(pluginsPB, api.Plugin{
219+
pluginsPB = append(pluginsPB, &api.Plugin{
219220
Type: p.Registration.Type.String(),
220221
ID: p.Registration.ID,
221222
Requires: requires,

0 commit comments

Comments
 (0)