[sandbox] Migrate from gogo to Any

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko 2022-03-29 14:11:48 -07:00
parent 85a49e4ee7
commit d0b32c0539
5 changed files with 56 additions and 41 deletions

View File

@ -28,7 +28,7 @@ import (
"github.com/containerd/containerd/metadata/boltutil" "github.com/containerd/containerd/metadata/boltutil"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
api "github.com/containerd/containerd/sandbox" api "github.com/containerd/containerd/sandbox"
"github.com/gogo/protobuf/types" "github.com/containerd/typeurl"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
@ -114,7 +114,7 @@ func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpat
continue continue
} else if strings.HasPrefix(path, "extensions.") { } else if strings.HasPrefix(path, "extensions.") {
if updated.Extensions == nil { if updated.Extensions == nil {
updated.Extensions = map[string]types.Any{} updated.Extensions = map[string]typeurl.Any{}
} }
key := strings.TrimPrefix(path, "extensions.") key := strings.TrimPrefix(path, "extensions.")

View File

@ -21,7 +21,9 @@ import (
"testing" "testing"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/protobuf"
api "github.com/containerd/containerd/sandbox" api "github.com/containerd/containerd/sandbox"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/types" "github.com/gogo/protobuf/types"
) )
@ -35,9 +37,9 @@ func TestSandboxCreate(t *testing.T) {
ID: "1", ID: "1",
Labels: map[string]string{"a": "1", "b": "2"}, Labels: map[string]string{"a": "1", "b": "2"},
Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}}, Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}},
Extensions: map[string]types.Any{ Extensions: map[string]typeurl.Any{
"ext1": {TypeUrl: "url/1", Value: []byte{1, 2, 3}}, "ext1": &types.Any{TypeUrl: "url/1", Value: []byte{1, 2, 3}},
"ext2": {TypeUrl: "url/2", Value: []byte{3, 2, 1}}, "ext2": &types.Any{TypeUrl: "url/2", Value: []byte{3, 2, 1}},
}, },
Runtime: api.RuntimeOpts{ Runtime: api.RuntimeOpts{
Name: "test", Name: "test",
@ -91,8 +93,8 @@ func TestSandboxUpdate(t *testing.T) {
ID: "2", ID: "2",
Labels: map[string]string{"lbl1": "existing"}, Labels: map[string]string{"lbl1": "existing"},
Spec: &types.Any{TypeUrl: "1", Value: []byte{1}}, // will replace Spec: &types.Any{TypeUrl: "1", Value: []byte{1}}, // will replace
Extensions: map[string]types.Any{ Extensions: map[string]typeurl.Any{
"ext2": {TypeUrl: "url2", Value: []byte{4, 5, 6}}, // will append `ext1` "ext2": &types.Any{TypeUrl: "url2", Value: []byte{4, 5, 6}}, // will append `ext1`
}, },
Runtime: api.RuntimeOpts{Name: "test"}, // no change Runtime: api.RuntimeOpts{Name: "test"}, // no change
}); err != nil { }); err != nil {
@ -105,8 +107,8 @@ func TestSandboxUpdate(t *testing.T) {
ID: "2", ID: "2",
Labels: map[string]string{"lbl1": "new"}, Labels: map[string]string{"lbl1": "new"},
Spec: &expectedSpec, Spec: &expectedSpec,
Extensions: map[string]types.Any{ Extensions: map[string]typeurl.Any{
"ext1": {TypeUrl: "url1", Value: []byte{1, 2}}, "ext1": &types.Any{TypeUrl: "url1", Value: []byte{1, 2}},
}, },
}, "labels.lbl1", "extensions.ext1", "spec") }, "labels.lbl1", "extensions.ext1", "spec")
if err != nil { if err != nil {
@ -119,9 +121,9 @@ func TestSandboxUpdate(t *testing.T) {
Labels: map[string]string{ Labels: map[string]string{
"lbl1": "new", "lbl1": "new",
}, },
Extensions: map[string]types.Any{ Extensions: map[string]typeurl.Any{
"ext1": {TypeUrl: "url1", Value: []byte{1, 2}}, "ext1": &types.Any{TypeUrl: "url1", Value: []byte{1, 2}},
"ext2": {TypeUrl: "url2", Value: []byte{4, 5, 6}}, "ext2": &types.Any{TypeUrl: "url2", Value: []byte{4, 5, 6}},
}, },
Runtime: api.RuntimeOpts{Name: "test"}, Runtime: api.RuntimeOpts{Name: "test"},
} }
@ -154,14 +156,14 @@ func TestSandboxList(t *testing.T) {
ID: "1", ID: "1",
Labels: map[string]string{"test": "1"}, Labels: map[string]string{"test": "1"},
Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}}, Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}},
Extensions: map[string]types.Any{"ext": {}}, Extensions: map[string]typeurl.Any{"ext": &types.Any{}},
Runtime: api.RuntimeOpts{Name: "test"}, Runtime: api.RuntimeOpts{Name: "test"},
}, },
{ {
ID: "2", ID: "2",
Labels: map[string]string{"test": "2"}, Labels: map[string]string{"test": "2"},
Spec: &types.Any{TypeUrl: "2", Value: []byte{3, 2, 1}}, Spec: &types.Any{TypeUrl: "2", Value: []byte{3, 2, 1}},
Extensions: map[string]types.Any{"ext": { Extensions: map[string]typeurl.Any{"ext": &types.Any{
TypeUrl: "test", TypeUrl: "test",
Value: []byte{9}, Value: []byte{9},
}}, }},
@ -201,14 +203,14 @@ func TestSandboxListWithFilter(t *testing.T) {
ID: "1", ID: "1",
Labels: map[string]string{"test": "1"}, Labels: map[string]string{"test": "1"},
Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}}, Spec: &types.Any{TypeUrl: "1", Value: []byte{1, 2, 3}},
Extensions: map[string]types.Any{"ext": {}}, Extensions: map[string]typeurl.Any{"ext": &types.Any{}},
Runtime: api.RuntimeOpts{Name: "test"}, Runtime: api.RuntimeOpts{Name: "test"},
}, },
{ {
ID: "2", ID: "2",
Labels: map[string]string{"test": "2"}, Labels: map[string]string{"test": "2"},
Spec: &types.Any{TypeUrl: "2", Value: []byte{3, 2, 1}}, Spec: &types.Any{TypeUrl: "2", Value: []byte{3, 2, 1}},
Extensions: map[string]types.Any{"ext": { Extensions: map[string]typeurl.Any{"ext": &types.Any{
TypeUrl: "test", TypeUrl: "test",
Value: []byte{9}, Value: []byte{9},
}}, }},
@ -284,7 +286,7 @@ func assertEqualInstances(t *testing.T, x, y api.Sandbox) {
t.Fatalf("runtime names are not equal: %q != %q", x.Runtime.Name, y.Runtime.Name) t.Fatalf("runtime names are not equal: %q != %q", x.Runtime.Name, y.Runtime.Name)
} }
if !reflect.DeepEqual(x.Runtime.Options, y.Runtime.Options) { if !reflect.DeepEqual(protobuf.FromAny(x.Runtime.Options), protobuf.FromAny(y.Runtime.Options)) {
t.Fatalf("runtime options are not equal: %+v != %+v", x.Runtime.Options, y.Runtime.Options) t.Fatalf("runtime options are not equal: %+v != %+v", x.Runtime.Options, y.Runtime.Options)
} }
} }

View File

@ -217,7 +217,7 @@ func WithSandboxSpec(s *oci.Spec, opts ...oci.SpecOpts) NewSandboxOpts {
func WithSandboxExtension(name string, ext interface{}) NewSandboxOpts { func WithSandboxExtension(name string, ext interface{}) NewSandboxOpts {
return func(ctx context.Context, client *Client, s *api.Sandbox) error { return func(ctx context.Context, client *Client, s *api.Sandbox) error {
if s.Extensions == nil { if s.Extensions == nil {
s.Extensions = make(map[string]types.Any) s.Extensions = make(map[string]typeurl.Any)
} }
any, err := typeurl.MarshalAny(ext) any, err := typeurl.MarshalAny(ext)
@ -225,7 +225,7 @@ func WithSandboxExtension(name string, ext interface{}) NewSandboxOpts {
return errors.Wrap(err, "failed to marshal sandbox extension") return errors.Wrap(err, "failed to marshal sandbox extension")
} }
s.Extensions[name] = *any s.Extensions[name] = any
return err return err
} }
} }

View File

@ -18,38 +18,51 @@ package sandbox
import ( import (
"github.com/containerd/containerd/api/types" "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/typeurl"
gogo_types "github.com/gogo/protobuf/types"
) )
// ToProto will map Sandbox struct to it's protobuf definition // ToProto will map Sandbox struct to it's protobuf definition
func ToProto(s *Sandbox) types.Sandbox { func ToProto(sandbox *Sandbox) types.Sandbox {
extensions := make(map[string]gogo_types.Any)
for k, v := range sandbox.Extensions {
extensions[k] = *protobuf.FromAny(v)
}
return types.Sandbox{ return types.Sandbox{
SandboxID: s.ID, SandboxID: sandbox.ID,
Runtime: types.Sandbox_Runtime{ Runtime: types.Sandbox_Runtime{
Name: s.Runtime.Name, Name: sandbox.Runtime.Name,
Options: s.Runtime.Options, Options: protobuf.FromAny(sandbox.Runtime.Options),
}, },
Labels: s.Labels, Labels: sandbox.Labels,
CreatedAt: s.CreatedAt, CreatedAt: sandbox.CreatedAt,
UpdatedAt: s.UpdatedAt, UpdatedAt: sandbox.UpdatedAt,
Extensions: s.Extensions, Extensions: extensions,
Spec: s.Spec, Spec: protobuf.FromAny(sandbox.Spec),
} }
} }
// FromProto map protobuf sandbox definition to Sandbox struct // FromProto map protobuf sandbox definition to Sandbox struct
func FromProto(p *types.Sandbox) Sandbox { func FromProto(sandboxpb *types.Sandbox) Sandbox {
runtime := RuntimeOpts{ runtime := RuntimeOpts{
Name: p.Runtime.Name, Name: sandboxpb.Runtime.Name,
Options: p.Runtime.Options, Options: sandboxpb.Runtime.Options,
}
extensions := make(map[string]typeurl.Any)
for k, v := range sandboxpb.Extensions {
v := v
extensions[k] = &v
} }
return Sandbox{ return Sandbox{
ID: p.SandboxID, ID: sandboxpb.SandboxID,
Labels: p.Labels, Labels: sandboxpb.Labels,
Runtime: runtime, Runtime: runtime,
Spec: p.Spec, Spec: sandboxpb.Spec,
CreatedAt: p.CreatedAt, CreatedAt: sandboxpb.CreatedAt,
UpdatedAt: p.UpdatedAt, UpdatedAt: sandboxpb.UpdatedAt,
Extensions: p.Extensions, Extensions: extensions,
} }
} }

View File

@ -20,7 +20,7 @@ import (
"context" "context"
"time" "time"
"github.com/gogo/protobuf/types" "github.com/containerd/typeurl"
) )
// Sandbox is an object stored in metadata database // Sandbox is an object stored in metadata database
@ -32,19 +32,19 @@ type Sandbox struct {
// Runtime shim to use for this sandbox // Runtime shim to use for this sandbox
Runtime RuntimeOpts Runtime RuntimeOpts
// Spec carries the runtime specification used to implement the sandbox // Spec carries the runtime specification used to implement the sandbox
Spec *types.Any Spec typeurl.Any
// CreatedAt is the time at which the sandbox was created // CreatedAt is the time at which the sandbox was created
CreatedAt time.Time CreatedAt time.Time
// UpdatedAt is the time at which the sandbox was updated // UpdatedAt is the time at which the sandbox was updated
UpdatedAt time.Time UpdatedAt time.Time
// Extensions stores client-specified metadata // Extensions stores client-specified metadata
Extensions map[string]types.Any Extensions map[string]typeurl.Any
} }
// RuntimeOpts holds runtime specific information // RuntimeOpts holds runtime specific information
type RuntimeOpts struct { type RuntimeOpts struct {
Name string Name string
Options *types.Any Options typeurl.Any
} }
// Store is a storage interface for sandbox metadata objects // Store is a storage interface for sandbox metadata objects