Use typeurl.Any instead of github.com/gogo/protobuf/types.Any

This commit upgrades github.com/containerd/typeurl to use typeurl.Any.
The interface hides gogo/protobuf/types.Any from containerd's Go client.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato
2022-03-22 00:40:39 +00:00
parent 551516a18d
commit 96b16b447d
42 changed files with 336 additions and 152 deletions

View File

@@ -20,6 +20,8 @@ import (
"fmt"
"time"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
bolt "go.etcd.io/bbolt"
@@ -151,7 +153,7 @@ func WriteTimestamps(bkt *bolt.Bucket, created, updated time.Time) error {
// WriteExtensions will write a KV map to the given bucket,
// where `K` is a string key and `V` is a protobuf's Any type that represents a generic extension.
func WriteExtensions(bkt *bolt.Bucket, extensions map[string]types.Any) error {
func WriteExtensions(bkt *bolt.Bucket, extensions map[string]typeurl.Any) error {
if len(extensions) == 0 {
return nil
}
@@ -162,8 +164,8 @@ func WriteExtensions(bkt *bolt.Bucket, extensions map[string]types.Any) error {
}
for name, ext := range extensions {
ext := ext
p, err := proto.Marshal(&ext)
ext := protobuf.FromAny(ext)
p, err := proto.Marshal(ext)
if err != nil {
return err
}
@@ -177,9 +179,9 @@ func WriteExtensions(bkt *bolt.Bucket, extensions map[string]types.Any) error {
}
// ReadExtensions will read back a map of extensions from the given bucket, previously written by WriteExtensions
func ReadExtensions(bkt *bolt.Bucket) (map[string]types.Any, error) {
func ReadExtensions(bkt *bolt.Bucket) (map[string]typeurl.Any, error) {
var (
extensions = make(map[string]types.Any)
extensions = make(map[string]typeurl.Any)
ebkt = bkt.Bucket(bucketKeyExtensions)
)
@@ -193,7 +195,7 @@ func ReadExtensions(bkt *bolt.Bucket) (map[string]types.Any, error) {
return err
}
extensions[string(k)] = t
extensions[string(k)] = &t
return nil
}); err != nil {
return nil, err
@@ -203,12 +205,13 @@ func ReadExtensions(bkt *bolt.Bucket) (map[string]types.Any, error) {
}
// WriteAny write a protobuf's Any type to the bucket
func WriteAny(bkt *bolt.Bucket, name []byte, any *types.Any) error {
if any == nil {
func WriteAny(bkt *bolt.Bucket, name []byte, any typeurl.Any) error {
pbany := protobuf.FromAny(any)
if pbany == nil {
return nil
}
data, err := proto.Marshal(any)
data, err := proto.Marshal(pbany)
if err != nil {
return err
}

View File

@@ -30,6 +30,7 @@ import (
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/metadata/boltutil"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
bolt "go.etcd.io/bbolt"
@@ -211,7 +212,7 @@ func (s *containerStore) Update(ctx context.Context, container containers.Contai
if strings.HasPrefix(path, "extensions.") {
if updated.Extensions == nil {
updated.Extensions = map[string]types.Any{}
updated.Extensions = map[string]typeurl.Any{}
}
key := strings.TrimPrefix(path, "extensions.")
updated.Extensions[key] = container.Extensions[key]

View File

@@ -30,10 +30,13 @@ import (
"github.com/containerd/containerd/filters"
"github.com/containerd/containerd/log/logtest"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/types"
"github.com/google/go-cmp/cmp"
specs "github.com/opencontainers/runtime-spec/specs-go"
bolt "go.etcd.io/bbolt"
"gotest.tools/v3/assert"
)
func init() {
@@ -47,7 +50,7 @@ func TestContainersList(t *testing.T) {
store := NewContainerStore(NewDB(db, nil, nil))
spec := &specs.Spec{}
encoded, err := typeurl.MarshalAny(spec)
encoded, err := protobuf.MarshalAnyToProto(spec)
if err != nil {
t.Fatal(err)
}
@@ -179,13 +182,13 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
store := NewContainerStore(NewDB(db, nil, nil))
spec := &specs.Spec{}
encoded, err := typeurl.MarshalAny(spec)
encoded, err := protobuf.MarshalAnyToProto(spec)
if err != nil {
t.Fatal(err)
}
spec.Annotations = map[string]string{"updated": "true"}
encodedUpdated, err := typeurl.MarshalAny(spec)
encodedUpdated, err := protobuf.MarshalAnyToProto(spec)
if err != nil {
t.Fatal(err)
}
@@ -467,8 +470,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"),
},
@@ -479,8 +482,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("world"),
},
@@ -491,8 +494,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("world"),
},
@@ -506,8 +509,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"),
},
@@ -518,8 +521,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("world"),
},
@@ -531,8 +534,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"),
},
@@ -546,8 +549,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"),
},
@@ -557,8 +560,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Labels: map[string]string{
"foo": "one",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("world"),
},
@@ -570,8 +573,8 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("world"),
},
@@ -585,21 +588,21 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
Extensions: map[string]typeurl.Any{
// leaves hello in place.
"hello": {
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"),
},
},
},
input: containers.Container{
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("universe"), // this will be ignored
},
"bar": {
"bar": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("foo"), // this will be added
},
@@ -611,12 +614,12 @@ func TestContainersCreateUpdateDelete(t *testing.T) {
Runtime: containers.RuntimeInfo{
Name: "testruntime",
},
Extensions: map[string]types.Any{
"hello": {
Extensions: map[string]typeurl.Any{
"hello": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("hello"), // remains as world
},
"bar": {
"bar": &types.Any{
TypeUrl: "test.update.extensions",
Value: []byte("foo"), // this will be added
},
@@ -702,10 +705,26 @@ func checkContainerTimestamps(t *testing.T, c *containers.Container, now time.Ti
}
}
func checkContainersEqual(t *testing.T, a, b *containers.Container, format string, args ...interface{}) {
if !reflect.DeepEqual(a, b) {
t.Fatalf("containers not equal \n\t%v != \n\t%v: "+format, append([]interface{}{a, b}, args...)...)
// isNil returns true if the given parameter is nil or typed nil.
func isNil(x interface{}) bool {
if x == nil {
return true
}
v := reflect.ValueOf(x)
return v.Kind() == reflect.Ptr && v.IsNil()
}
func checkContainersEqual(t *testing.T, a, b *containers.Container, format string, args ...interface{}) {
// Ignore the difference of nil and typed nil.
opt := cmp.FilterValues(
func(x, y interface{}) bool {
return isNil(x) && isNil(y)
},
cmp.Comparer(func(_, _ interface{}) bool {
return true
}),
)
assert.DeepEqual(t, a, b, opt)
}
func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) {