use typeurl funcs for marshalling anypb.Any

Signed-off-by: Akhil Mohan <akhilerm@gmail.com>
This commit is contained in:
Akhil Mohan 2024-07-08 21:48:31 +05:30
parent cbb2fc78e9
commit 300fd770a0
No known key found for this signature in database
GPG Key ID: CB72A322B3B5DEDA
36 changed files with 109 additions and 164 deletions

View File

@ -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"
@ -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)
}

View File

@ -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"
@ -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,

View File

@ -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"
)
@ -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
}
@ -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

View File

@ -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"
@ -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
}
}

View File

@ -152,7 +152,7 @@ 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,
@ -160,9 +160,9 @@ func containerToProto(container *containers.Container) *containersapi.Container
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,

View File

@ -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)
@ -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 {

View File

@ -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
}
@ -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
}
@ -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

View File

@ -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"
)
@ -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)
}
@ -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)
}

View File

@ -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)
}
@ -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
}

View File

@ -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"
)
@ -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{

View File

@ -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"
)
@ -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

View File

@ -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"
@ -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

View File

@ -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)
@ -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 {
@ -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)
@ -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 {

View File

@ -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"
@ -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)
}

View File

@ -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"
@ -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
@ -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
}

View File

@ -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"
@ -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{}
@ -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 {

View File

@ -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
@ -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{})
}

View File

@ -28,9 +28,6 @@ import (
"strings"
"time"
"github.com/containerd/containerd/v2/pkg/atomicfile"
"github.com/containerd/containerd/v2/pkg/dialer"
"github.com/containerd/ttrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
@ -40,6 +37,8 @@ import (
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/v2/core/events/exchange"
"github.com/containerd/containerd/v2/core/runtime"
"github.com/containerd/containerd/v2/pkg/atomicfile"
"github.com/containerd/containerd/v2/pkg/dialer"
"github.com/containerd/containerd/v2/pkg/identifiers"
"github.com/containerd/containerd/v2/pkg/protobuf"
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
@ -47,6 +46,8 @@ import (
"github.com/containerd/containerd/v2/pkg/timeout"
"github.com/containerd/errdefs"
"github.com/containerd/log"
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl/v2"
)
const (
@ -564,7 +565,7 @@ func (s *shimTask) Create(ctx context.Context, opts runtime.CreateOpts) (runtime
Stderr: opts.IO.Stderr,
Terminal: opts.IO.Terminal,
Checkpoint: opts.Checkpoint,
Options: protobuf.FromAny(topts),
Options: typeurl.MarshalProto(topts),
}
for _, m := range opts.Rootfs {
request.Rootfs = append(request.Rootfs, &types.Mount{

View File

@ -26,10 +26,6 @@ import (
"strings"
"sync"
"github.com/containerd/log"
"github.com/containerd/plugin"
"github.com/containerd/plugin/registry"
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/core/events/exchange"
"github.com/containerd/containerd/v2/core/metadata"
@ -37,11 +33,14 @@ import (
"github.com/containerd/containerd/v2/core/sandbox"
"github.com/containerd/containerd/v2/internal/cleanup"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/protobuf"
shimbinary "github.com/containerd/containerd/v2/pkg/shim"
"github.com/containerd/containerd/v2/pkg/timeout"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/log"
"github.com/containerd/plugin"
"github.com/containerd/plugin/registry"
"github.com/containerd/typeurl/v2"
)
// ShimConfig for the shim
@ -256,7 +255,7 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string,
ttrpcAddress: m.containerdTTRPCAddress,
env: m.env,
})
shim, err := b.Start(ctx, protobuf.FromAny(topts), func() {
shim, err := b.Start(ctx, typeurl.MarshalProto(topts), func() {
log.G(ctx).WithField("id", id).Info("shim disconnected")
cleanupAfterDeadShim(cleanup.Background(ctx), id, m.shims, m.events, b)

View File

@ -37,7 +37,6 @@ import (
"github.com/containerd/containerd/v2/core/runtime"
"github.com/containerd/containerd/v2/internal/cleanup"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/containerd/v2/pkg/timeout"
"github.com/containerd/containerd/v2/plugins"
@ -272,7 +271,7 @@ func (m *TaskManager) validateRuntimeFeatures(ctx context.Context, opts runtime.
topts = opts.RuntimeOptions
}
pInfo, err := m.PluginInfo(ctx, &apitypes.RuntimeRequest{RuntimePath: opts.Runtime, Options: protobuf.FromAny(topts)})
pInfo, err := m.PluginInfo(ctx, &apitypes.RuntimeRequest{RuntimePath: opts.Runtime, Options: typeurl.MarshalProto(topts)})
if err != nil {
return fmt.Errorf("runtime info: %w", err)
}

View File

@ -27,20 +27,20 @@ import (
func ToProto(sandbox *Sandbox) *types.Sandbox {
extensions := make(map[string]*gogo_types.Any)
for k, v := range sandbox.Extensions {
extensions[k] = protobuf.FromAny(v)
extensions[k] = typeurl.MarshalProto(v)
}
return &types.Sandbox{
SandboxID: sandbox.ID,
Runtime: &types.Sandbox_Runtime{
Name: sandbox.Runtime.Name,
Options: protobuf.FromAny(sandbox.Runtime.Options),
Options: typeurl.MarshalProto(sandbox.Runtime.Options),
},
Sandboxer: sandbox.Sandboxer,
Labels: sandbox.Labels,
CreatedAt: protobuf.ToTimestamp(sandbox.CreatedAt),
UpdatedAt: protobuf.ToTimestamp(sandbox.UpdatedAt),
Extensions: extensions,
Spec: protobuf.FromAny(sandbox.Spec),
Spec: typeurl.MarshalProto(sandbox.Spec),
}
}

View File

@ -26,6 +26,7 @@ import (
"github.com/containerd/containerd/v2/core/sandbox"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"google.golang.org/protobuf/types/known/anypb"
)
@ -206,17 +207,17 @@ func (s *remoteSandboxController) Update(
}
func toAPISandbox(sb sandbox.Sandbox) (types.Sandbox, error) {
options, err := protobuf.MarshalAnyToProto(sb.Runtime.Options)
options, err := typeurl.MarshalAnyToProto(sb.Runtime.Options)
if err != nil {
return types.Sandbox{}, err
}
spec, err := protobuf.MarshalAnyToProto(sb.Spec)
spec, err := typeurl.MarshalAnyToProto(sb.Spec)
if err != nil {
return types.Sandbox{}, err
}
extensions := make(map[string]*anypb.Any)
for k, v := range sb.Extensions {
pb, err := protobuf.MarshalAnyToProto(v)
pb, err := typeurl.MarshalAnyToProto(v)
if err != nil {
return types.Sandbox{}, err
}

View File

@ -24,7 +24,6 @@ import (
streamingapi "github.com/containerd/containerd/api/services/streaming/v1"
"github.com/containerd/containerd/v2/core/streaming"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl/v2"
@ -82,7 +81,7 @@ func (sc *streamCreator) Create(ctx context.Context, id string) (streaming.Strea
if err != nil {
return nil, err
}
err = stream.Send(protobuf.FromAny(a))
err = stream.Send(typeurl.MarshalProto(a))
if err != nil {
if !errors.Is(err, io.EOF) {
err = errdefs.FromGRPC(err)
@ -108,7 +107,7 @@ type clientStream struct {
}
func (cs *clientStream) Send(a typeurl.Any) (err error) {
err = cs.s.Send(protobuf.FromAny(a))
err = cs.s.Send(typeurl.MarshalProto(a))
if !errors.Is(err, io.EOF) {
err = errdefs.FromGRPC(err)
}

2
go.mod
View File

@ -25,7 +25,7 @@ require (
github.com/containerd/platforms v0.2.1
github.com/containerd/plugin v0.1.0
github.com/containerd/ttrpc v1.2.5
github.com/containerd/typeurl/v2 v2.1.1
github.com/containerd/typeurl/v2 v2.2.0
github.com/containernetworking/cni v1.2.2
github.com/containernetworking/plugins v1.5.1
github.com/coreos/go-systemd/v22 v22.5.0

5
go.sum
View File

@ -61,8 +61,8 @@ github.com/containerd/plugin v0.1.0 h1:CYMyZk9beRAIe1FEKItbMLLAz/z16aXrGc+B+nv0f
github.com/containerd/plugin v0.1.0/go.mod h1:j6HlpMtkiZMgT4UsfVNxPBUkwdw9KQGU6nCLfRxnq+w=
github.com/containerd/ttrpc v1.2.5 h1:IFckT1EFQoFBMG4c3sMdT8EP3/aKfumK1msY+Ze4oLU=
github.com/containerd/ttrpc v1.2.5/go.mod h1:YCXHsb32f+Sq5/72xHubdiJRQY9inL4a4ZQrAbN1q9o=
github.com/containerd/typeurl/v2 v2.1.1 h1:3Q4Pt7i8nYwy2KmQWIw2+1hTvwTE/6w9FqcttATPO/4=
github.com/containerd/typeurl/v2 v2.1.1/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
github.com/containerd/typeurl/v2 v2.2.0 h1:6NBDbQzr7I5LHgp34xAXYF5DOTQDn05X58lsPEmzLso=
github.com/containerd/typeurl/v2 v2.2.0/go.mod h1:8XOOxnyatxSWuG8OfsZXVnAF4iZfedjS/8UHSPJnX4g=
github.com/containernetworking/cni v1.2.2 h1:9IbP6KJQQxVKo4hhnm8r50YcVKrJbJu3Dqw+Rbt1vYk=
github.com/containernetworking/cni v1.2.2/go.mod h1:DuLgF+aPd3DzcTQTtp/Nvl1Kim23oFKdm2okJzBQA5M=
github.com/containernetworking/plugins v1.5.1 h1:T5ji+LPYjjgW0QM+KyrigZbLsZ8jaX+E5J/EcKOE4gQ=
@ -497,7 +497,6 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

View File

@ -55,7 +55,6 @@ import (
"github.com/containerd/containerd/v2/internal/registrar"
"github.com/containerd/containerd/v2/pkg/oci"
osinterface "github.com/containerd/containerd/v2/pkg/os"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/containerd/v2/plugins"
)
@ -398,7 +397,7 @@ func introspectRuntimeFeatures(ctx context.Context, intro introspection.Service,
if r.Type != plugins.RuntimeRuncV2 {
return nil, fmt.Errorf("introspecting OCI runtime features needs the runtime type to be %q, got %q",
plugins.RuntimeRuncV2, r.Type)
// For other runtimes, protobuf.MarshalAnyToProto will cause nil panic during typeurl dereference
// For other runtimes, typeurl.MarshalAnyToProto will cause nil panic during typeurl dereference
}
rr := &apitypes.RuntimeRequest{
@ -412,7 +411,7 @@ func introspectRuntimeFeatures(ctx context.Context, intro introspection.Service,
return nil, err
}
if options != nil {
rr.Options, err = protobuf.MarshalAnyToProto(options)
rr.Options, err = typeurl.MarshalAnyToProto(options)
if err != nil {
return nil, fmt.Errorf("failed to marshal %T: %w", options, err)
}

View File

@ -1,47 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package protobuf
import (
"github.com/containerd/typeurl/v2"
"google.golang.org/protobuf/types/known/anypb"
)
// FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any.
func FromAny(from typeurl.Any) *anypb.Any {
if from == nil {
return nil
}
if pbany, ok := from.(*anypb.Any); ok {
return pbany
}
return &anypb.Any{
TypeUrl: from.GetTypeUrl(),
Value: from.GetValue(),
}
}
// MarshalAnyToProto converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any.
func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
anyType, err := typeurl.MarshalAny(from)
if err != nil {
return nil, err
}
return FromAny(anyType), nil
}

View File

@ -1,26 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package protobuf
import "testing"
func TestFromAny(t *testing.T) {
actual := FromAny(nil)
if actual != nil {
t.Fatalf("expected nil, got %v", actual)
}
}

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd/v2/pkg/ttrpcutil"
"github.com/containerd/log"
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl/v2"
)
const (
@ -111,7 +112,7 @@ func (l *RemoteEventsPublisher) Publish(ctx context.Context, topic string, event
if err != nil {
return err
}
evt, err := protobuf.MarshalAnyToProto(event)
evt, err := typeurl.MarshalAnyToProto(event)
if err != nil {
return err
}

View File

@ -38,7 +38,7 @@ func containersToProto(containers []containers.Container) []*api.Container {
func containerToProto(container *containers.Container) *api.Container {
extensions := make(map[string]*types.Any)
for k, v := range container.Extensions {
extensions[k] = protobuf.FromAny(v)
extensions[k] = typeurl.MarshalProto(v)
}
return &api.Container{
ID: container.ID,
@ -46,9 +46,9 @@ func containerToProto(container *containers.Container) *api.Container {
Image: container.Image,
Runtime: &api.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,
CreatedAt: protobuf.ToTimestamp(container.CreatedAt),

View File

@ -32,6 +32,7 @@ import (
"github.com/containerd/plugin"
"github.com/containerd/plugin/registry"
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl/v2"
"google.golang.org/grpc"
)
@ -120,7 +121,7 @@ func toProto(env *events.Envelope) *types.Envelope {
Timestamp: protobuf.ToTimestamp(env.Timestamp),
Namespace: env.Namespace,
Topic: env.Topic,
Event: protobuf.FromAny(env.Event),
Event: typeurl.MarshalProto(env.Event),
}
}

View File

@ -22,7 +22,6 @@ import (
api "github.com/containerd/containerd/api/services/streaming/v1"
"github.com/containerd/containerd/v2/core/streaming"
"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"
@ -92,7 +91,7 @@ func (s *service) Stream(srv api.Streaming_StreamServer) error {
}
// Send response packet after registering stream
if err := srv.Send(protobuf.FromAny(emptyResponse)); err != nil {
if err := srv.Send(typeurl.MarshalProto(emptyResponse)); err != nil {
return err
}
@ -111,7 +110,7 @@ type serviceStream struct {
}
func (ss *serviceStream) Send(a typeurl.Any) (err error) {
err = errdefs.FromGRPC(ss.s.Send(protobuf.FromAny(a)))
err = errdefs.FromGRPC(ss.s.Send(typeurl.MarshalProto(a)))
if !errors.Is(err, io.EOF) {
err = errdefs.FromGRPC(err)
}

View File

@ -461,7 +461,7 @@ func (l *local) ListPids(ctx context.Context, r *api.ListPidsRequest, _ ...grpc.
Pid: p.Pid,
}
if p.Info != nil {
a, err := protobuf.MarshalAnyToProto(p.Info)
a, err := typeurl.MarshalAnyToProto(p.Info)
if err != nil {
return nil, fmt.Errorf("failed to marshal process %d info: %w", p.Pid, err)
}
@ -576,7 +576,7 @@ func (l *local) Checkpoint(ctx context.Context, r *api.CheckpointTaskRequest, _
return nil, err
}
// write the config to the content store
pbany := protobuf.FromAny(container.Spec)
pbany := typeurl.MarshalProto(container.Spec)
data, err := proto.Marshal(pbany)
if err != nil {
return nil, err

View File

@ -2,7 +2,7 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/containerd/typeurl)](https://pkg.go.dev/github.com/containerd/typeurl)
[![Build Status](https://github.com/containerd/typeurl/workflows/CI/badge.svg)](https://github.com/containerd/typeurl/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/containerd/typeurl/branch/master/graph/badge.svg)](https://codecov.io/gh/containerd/typeurl)
[![codecov](https://codecov.io/gh/containerd/typeurl/branch/main/graph/badge.svg)](https://codecov.io/gh/containerd/typeurl)
[![Go Report Card](https://goreportcard.com/badge/github.com/containerd/typeurl)](https://goreportcard.com/report/github.com/containerd/typeurl)
A Go package for managing the registration, marshaling, and unmarshaling of encoded types.
@ -13,8 +13,8 @@ This package helps when types are sent over a ttrpc/GRPC API and marshaled as a
**typeurl** is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE).
As a containerd sub-project, you will find the:
* [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md),
* [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS),
* and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md)
* [Project governance](https://github.com/containerd/project/blob/main/GOVERNANCE.md),
* [Maintainers](https://github.com/containerd/project/blob/main/MAINTAINERS),
* and [Contributing guidelines](https://github.com/containerd/project/blob/main/CONTRIBUTING.md)
information in our [`containerd/project`](https://github.com/containerd/project) repository.

View File

@ -27,6 +27,7 @@ import (
gogoproto "github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/anypb"
)
var (
@ -122,6 +123,9 @@ func TypeURL(v interface{}) (string, error) {
// Is returns true if the type of the Any is the same as v.
func Is(any Any, v interface{}) bool {
if any == nil {
return false
}
// call to check that v is a pointer
tryDereference(v)
url, err := TypeURL(v)
@ -193,6 +197,31 @@ func UnmarshalToByTypeURL(typeURL string, value []byte, out interface{}) error {
return err
}
// MarshalProto converts typeurl.Any to google.golang.org/protobuf/types/known/anypb.Any.
func MarshalProto(from Any) *anypb.Any {
if from == nil {
return nil
}
if pbany, ok := from.(*anypb.Any); ok {
return pbany
}
return &anypb.Any{
TypeUrl: from.GetTypeUrl(),
Value: from.GetValue(),
}
}
// MarshalAnyToProto converts an arbitrary interface to google.golang.org/protobuf/types/known/anypb.Any.
func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
anyType, err := MarshalAny(from)
if err != nil {
return nil, err
}
return MarshalProto(anyType), nil
}
func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error) {
t, err := getTypeByUrl(typeURL)
if err != nil {

4
vendor/modules.txt vendored
View File

@ -185,8 +185,8 @@ github.com/containerd/plugin/registry
# github.com/containerd/ttrpc v1.2.5
## explicit; go 1.19
github.com/containerd/ttrpc
# github.com/containerd/typeurl/v2 v2.1.1
## explicit; go 1.13
# github.com/containerd/typeurl/v2 v2.2.0
## explicit; go 1.21
github.com/containerd/typeurl/v2
# github.com/containernetworking/cni v1.2.2
## explicit; go 1.21