Skip to content

Commit

Permalink
Merge pull request containerd#10378 from akhilerm/use-marshall-from-t…
Browse files Browse the repository at this point in the history
…ype-url

use typeurl funcs for marshalling anypb.Any
  • Loading branch information
mxpv authored Jul 11, 2024
2 parents 231301c + 300fd77 commit ac0f34f
Show file tree
Hide file tree
Showing 36 changed files with 109 additions and 164 deletions.
3 changes: 1 addition & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import (
"github.com/containerd/containerd/v2/defaults"
"github.com/containerd/containerd/v2/pkg/dialer"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/protobuf"
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/errdefs"
Expand Down Expand Up @@ -898,7 +897,7 @@ func (c *Client) RuntimeInfo(ctx context.Context, runtimePath string, runtimeOpt
}
var err error
if runtimeOptions != nil {
rr.Options, err = protobuf.MarshalAnyToProto(runtimeOptions)
rr.Options, err = typeurl.MarshalAnyToProto(runtimeOptions)
if err != nil {
return nil, fmt.Errorf("failed to marshal %T: %w", runtimeOptions, err)
}
Expand Down
3 changes: 1 addition & 2 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/pkg/cio"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
"github.com/containerd/fifo"
"github.com/containerd/typeurl/v2"
Expand Down Expand Up @@ -288,7 +287,7 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
if err != nil {
return nil, err
}
request.Options = protobuf.FromAny(o)
request.Options = typeurl.MarshalProto(o)
}
t := &task{
client: c.client,
Expand Down
6 changes: 3 additions & 3 deletions client/container_checkpoint_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/containerd/v2/pkg/rootfs"
"github.com/containerd/platforms"
"github.com/containerd/typeurl/v2"
"github.com/opencontainers/go-digest"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -54,7 +54,7 @@ func WithCheckpointImage(ctx context.Context, client *Client, c *containers.Cont

// WithCheckpointTask includes the running task
func WithCheckpointTask(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
opt, err := protobuf.MarshalAnyToProto(copts)
opt, err := typeurl.MarshalAnyToProto(copts)
if err != nil {
return nil
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func WithCheckpointTask(ctx context.Context, client *Client, c *containers.Conta
// WithCheckpointRuntime includes the container runtime info
func WithCheckpointRuntime(ctx context.Context, client *Client, c *containers.Container, index *imagespec.Index, copts *options.CheckpointOptions) error {
if c.Runtime.Options != nil && c.Runtime.Options.GetValue() != nil {
opt := protobuf.FromAny(c.Runtime.Options)
opt := typeurl.MarshalProto(c.Runtime.Options)
data, err := proto.Marshal(opt)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions client/container_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
"github.com/opencontainers/image-spec/identity"
Expand Down Expand Up @@ -321,7 +320,7 @@ func WithSpec(s *oci.Spec, opts ...oci.SpecOpts) NewContainerOpts {
}

var err error
c.Spec, err = protobuf.MarshalAnyToProto(s)
c.Spec, err = typeurl.MarshalAnyToProto(s)
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/containerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ func (r *remoteContainers) Delete(ctx context.Context, id string) error {
func containerToProto(container *containers.Container) *containersapi.Container {
extensions := make(map[string]*ptypes.Any)
for k, v := range container.Extensions {
extensions[k] = protobuf.FromAny(v)
extensions[k] = typeurl.MarshalProto(v)
}
return &containersapi.Container{
ID: container.ID,
Labels: container.Labels,
Image: container.Image,
Runtime: &containersapi.Container_Runtime{
Name: container.Runtime.Name,
Options: protobuf.FromAny(container.Runtime.Options),
Options: typeurl.MarshalProto(container.Runtime.Options),
},
Spec: protobuf.FromAny(container.Spec),
Spec: typeurl.MarshalProto(container.Spec),
Snapshotter: container.Snapshotter,
SnapshotKey: container.SnapshotKey,
Extensions: extensions,
Expand Down
4 changes: 2 additions & 2 deletions client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (e *eventRemote) Publish(ctx context.Context, topic string, event events.Ev
}
req := &eventsapi.PublishRequest{
Topic: topic,
Event: protobuf.FromAny(evt),
Event: typeurl.MarshalProto(evt),
}
if _, err := e.client.Publish(ctx, req); err != nil {
return errdefs.FromGRPC(err)
Expand All @@ -67,7 +67,7 @@ func (e *eventRemote) Forward(ctx context.Context, envelope *events.Envelope) er
Timestamp: protobuf.ToTimestamp(envelope.Timestamp),
Namespace: envelope.Namespace,
Topic: envelope.Topic,
Event: protobuf.FromAny(envelope.Event),
Event: typeurl.MarshalProto(envelope.Event),
},
}
if _, err := e.client.Forward(ctx, req); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions client/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (t *task) Exec(ctx context.Context, id string, spec *specs.Process, ioCreat
i.Close()
}
}()
pSpec, err := protobuf.MarshalAnyToProto(spec)
pSpec, err := typeurl.MarshalAnyToProto(spec)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
}
request.ParentCheckpoint = i.ParentCheckpoint.String()
if i.Options != nil {
o, err := protobuf.MarshalAnyToProto(i.Options)
o, err := typeurl.MarshalAnyToProto(i.Options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -552,7 +552,7 @@ func (t *task) Update(ctx context.Context, opts ...UpdateTaskOpts) error {
if err != nil {
return err
}
request.Resources = protobuf.FromAny(r)
request.Resources = typeurl.MarshalProto(r)
}
if i.Annotations != nil {
request.Annotations = i.Annotations
Expand Down
6 changes: 3 additions & 3 deletions cmd/containerd-shim-runc-v2/manager/manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ import (
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/schedcore"
"github.com/containerd/containerd/v2/pkg/shim"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/errdefs"
runcC "github.com/containerd/go-runc"
"github.com/containerd/log"
"github.com/containerd/typeurl/v2"
"github.com/opencontainers/runtime-spec/specs-go/features"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -342,7 +342,7 @@ func (m manager) Info(ctx context.Context, optionsR io.Reader) (*types.RuntimeIn
}
}
if opts != nil {
info.Options, err = protobuf.MarshalAnyToProto(opts)
info.Options, err = typeurl.MarshalAnyToProto(opts)
if err != nil {
return nil, fmt.Errorf("failed to marshal %T: %w", opts, err)
}
Expand All @@ -362,7 +362,7 @@ func (m manager) Info(ctx context.Context, optionsR io.Reader) (*types.RuntimeIn
log.G(ctx).WithError(err).Debug("Failed to get the runtime features. The runc binary does not implement `runc features` command?")
}
if features != nil {
info.Features, err = protobuf.MarshalAnyToProto(features)
info.Features, err = typeurl.MarshalAnyToProto(features)
if err != nil {
return nil, fmt.Errorf("failed to marshal %T: %w", features, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/containerd-shim-runc-v2/task/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.Pi
d := &options.ProcessDetails{
ExecID: p.ID(),
}
a, err := protobuf.MarshalAnyToProto(d)
a, err := typeurl.MarshalAnyToProto(d)
if err != nil {
return nil, fmt.Errorf("failed to marshal process %d info: %w", pid, err)
}
Expand Down Expand Up @@ -654,7 +654,7 @@ func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.
return nil, err
}
return &taskAPI.StatsResponse{
Stats: protobuf.FromAny(data),
Stats: typeurl.MarshalProto(data),
}, nil
}

Expand Down
5 changes: 2 additions & 3 deletions core/diff/proxy/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/epoch"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/protobuf"
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"

"google.golang.org/protobuf/types/known/timestamppb"
)

Expand All @@ -54,7 +53,7 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts

payloads := make(map[string]*ptypes.Any)
for k, v := range config.ProcessorPayloads {
payloads[k] = protobuf.FromAny(v)
payloads[k] = typeurl.MarshalProto(v)
}

req := &diffapi.ApplyRequest{
Expand Down
3 changes: 1 addition & 2 deletions core/diff/stream_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"os/exec"
"sync"

"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/typeurl/v2"
)
Expand All @@ -41,7 +40,7 @@ func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProce

var payloadC io.Closer
if payload != nil {
pb := protobuf.FromAny(payload)
pb := typeurl.MarshalProto(payload)
data, err := proto.Marshal(pb)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions core/diff/stream_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/Microsoft/go-winio"

"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/log"
"github.com/containerd/typeurl/v2"
Expand All @@ -44,7 +43,7 @@ func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProce
cmd.Env = append(cmd.Env, env...)

if payload != nil {
pb := protobuf.FromAny(payload)
pb := typeurl.MarshalProto(payload)
data, err := proto.Marshal(pb)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions core/events/proxy/remote_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *grpcEventsProxy) Publish(ctx context.Context, topic string, event event
}
req := &api.PublishRequest{
Topic: topic,
Event: protobuf.FromAny(evt),
Event: typeurl.MarshalProto(evt),
}
if _, err := p.client.Publish(ctx, req); err != nil {
return errdefs.FromGRPC(err)
Expand All @@ -84,7 +84,7 @@ func (p *grpcEventsProxy) Forward(ctx context.Context, envelope *events.Envelope
Timestamp: protobuf.ToTimestamp(envelope.Timestamp),
Namespace: envelope.Namespace,
Topic: envelope.Topic,
Event: protobuf.FromAny(envelope.Event),
Event: typeurl.MarshalProto(envelope.Event),
},
}
if _, err := p.client.Forward(ctx, req); err != nil {
Expand Down Expand Up @@ -151,7 +151,7 @@ func (p *ttrpcEventsProxy) Publish(ctx context.Context, topic string, event even
}
req := &api.PublishRequest{
Topic: topic,
Event: protobuf.FromAny(evt),
Event: typeurl.MarshalProto(evt),
}
if _, err := p.client.Publish(ctx, req); err != nil {
return errdefs.FromGRPC(err)
Expand All @@ -165,7 +165,7 @@ func (p *ttrpcEventsProxy) Forward(ctx context.Context, envelope *events.Envelop
Timestamp: protobuf.ToTimestamp(envelope.Timestamp),
Namespace: envelope.Namespace,
Topic: envelope.Topic,
Event: protobuf.FromAny(envelope.Event),
Event: typeurl.MarshalProto(envelope.Event),
},
}
if _, err := p.client.Forward(ctx, req); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/introspection/proxy/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

api "github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/containerd/v2/core/introspection"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
"github.com/containerd/log"
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl/v2"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (i *introspectionRemote) Server(ctx context.Context) (*api.ServerResponse,
func (i *introspectionRemote) PluginInfo(ctx context.Context, pluginType, id string, options any) (resp *api.PluginInfoResponse, err error) {
var optionsPB *anypb.Any
if options != nil {
optionsPB, err = protobuf.MarshalAnyToProto(options)
optionsPB, err = typeurl.MarshalAnyToProto(options)
if err != nil {
return nil, fmt.Errorf("failed to marshal runtime requst: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions core/metadata/boltutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"time"

"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/typeurl/v2"
Expand Down Expand Up @@ -164,7 +163,7 @@ func WriteExtensions(bkt *bolt.Bucket, extensions map[string]typeurl.Any) error
}

for name, ext := range extensions {
ext := protobuf.FromAny(ext)
ext := typeurl.MarshalProto(ext)
p, err := proto.Marshal(ext)
if err != nil {
return err
Expand Down Expand Up @@ -206,7 +205,7 @@ func ReadExtensions(bkt *bolt.Bucket) (map[string]typeurl.Any, error) {

// WriteAny write a protobuf's Any type to the bucket
func WriteAny(bkt *bolt.Bucket, name []byte, any typeurl.Any) error {
pbany := protobuf.FromAny(any)
pbany := typeurl.MarshalProto(any)
if pbany == nil {
return nil
}
Expand Down
7 changes: 3 additions & 4 deletions core/metadata/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/pkg/filters"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/errdefs"
"github.com/containerd/log/logtest"
Expand All @@ -48,7 +47,7 @@ func TestContainersList(t *testing.T) {
ctx, db := testEnv(t)
store := NewContainerStore(NewDB(db, nil, nil))
spec := &specs.Spec{}
encoded, err := protobuf.MarshalAnyToProto(spec)
encoded, err := typeurl.MarshalAnyToProto(spec)
require.NoError(t, err)

testset := map[string]*containers.Container{}
Expand Down Expand Up @@ -178,11 +177,11 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
spec = &specs.Spec{}
)

encoded, err := protobuf.MarshalAnyToProto(spec)
encoded, err := typeurl.MarshalAnyToProto(spec)
require.NoError(t, err)

spec.Annotations = map[string]string{"updated": "true"}
encodedUpdated, err := protobuf.MarshalAnyToProto(spec)
encodedUpdated, err := typeurl.MarshalAnyToProto(spec)
require.NoError(t, err)

for _, testcase := range []struct {
Expand Down
9 changes: 4 additions & 5 deletions core/metrics/cgroups/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ import (
v2 "github.com/containerd/containerd/v2/core/metrics/cgroups/v2"
v1types "github.com/containerd/containerd/v2/core/metrics/types/v1"
v2types "github.com/containerd/containerd/v2/core/metrics/types/v2"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/prometheus/client_golang/prometheus"

"github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/typeurl/v2"
metrics "github.com/docker/go-metrics"
"github.com/prometheus/client_golang/prometheus"
)

// TestRegressionIssue6772 should not have dead-lock when Collect and Add run
Expand Down Expand Up @@ -151,7 +150,7 @@ func (t *mockStatT) Namespace() string {

func (t *mockStatT) Stats(context.Context) (*types.Any, error) {
if t.isV1 {
return protobuf.MarshalAnyToProto(&v1types.Metrics{})
return typeurl.MarshalAnyToProto(&v1types.Metrics{})
}
return protobuf.MarshalAnyToProto(&v2types.Metrics{})
return typeurl.MarshalAnyToProto(&v2types.Metrics{})
}
Loading

0 comments on commit ac0f34f

Please sign in to comment.