Require *T for typeurl interaction
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
c63b69672e
commit
4b9a8ee13e
@ -49,10 +49,10 @@ func init() {
|
||||
|
||||
// register TypeUrls for commonly marshaled external types
|
||||
major := strconv.Itoa(specs.VersionMajor)
|
||||
typeurl.Register(specs.Spec{}, "opencontainers/runtime-spec", major, "Spec")
|
||||
typeurl.Register(specs.Process{}, "opencontainers/runtime-spec", major, "Process")
|
||||
typeurl.Register(specs.LinuxResources{}, "opencontainers/runtime-spec", major, "LinuxResources")
|
||||
typeurl.Register(specs.WindowsResources{}, "opencontainers/runtime-spec", major, "WindowsResources")
|
||||
typeurl.Register(&specs.Spec{}, "opencontainers/runtime-spec", major, "Spec")
|
||||
typeurl.Register(&specs.Process{}, "opencontainers/runtime-spec", major, "Process")
|
||||
typeurl.Register(&specs.LinuxResources{}, "opencontainers/runtime-spec", major, "LinuxResources")
|
||||
typeurl.Register(&specs.WindowsResources{}, "opencontainers/runtime-spec", major, "WindowsResources")
|
||||
}
|
||||
|
||||
type clientOpts struct {
|
||||
|
@ -5,12 +5,6 @@ import (
|
||||
"github.com/containerd/containerd/typeurl"
|
||||
)
|
||||
|
||||
func init() {
|
||||
typeurl.Register(RuncOptions{}, "linux/runc/RuncOptions")
|
||||
typeurl.Register(CreateOptions{}, "linux/runc/CreateOptions")
|
||||
typeurl.Register(CheckpointOptions{}, "linux/runc/CheckpointOptions")
|
||||
}
|
||||
|
||||
func WithExit(r *tasks.CheckpointTaskRequest) error {
|
||||
a, err := typeurl.MarshalAny(&CheckpointOptions{
|
||||
Exit: true,
|
||||
|
@ -22,9 +22,9 @@ import (
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/mount"
|
||||
"github.com/containerd/containerd/runtime"
|
||||
"github.com/containerd/containerd/typeurl"
|
||||
"github.com/containerd/fifo"
|
||||
runc "github.com/containerd/go-runc"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -58,9 +58,11 @@ type initProcess struct {
|
||||
func newInitProcess(context context.Context, path, namespace string, r *shimapi.CreateTaskRequest) (*initProcess, error) {
|
||||
var options runcopts.CreateOptions
|
||||
if r.Options != nil {
|
||||
if err := proto.Unmarshal(r.Options.Value, &options); err != nil {
|
||||
v, err := typeurl.UnmarshalAny(r.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
options = *v.(*runcopts.CreateOptions)
|
||||
}
|
||||
for _, rm := range r.Rootfs {
|
||||
m := &mount.Mount{
|
||||
@ -266,9 +268,11 @@ func (p *initProcess) Stdin() io.Closer {
|
||||
func (p *initProcess) Checkpoint(context context.Context, r *shimapi.CheckpointTaskRequest) error {
|
||||
var options runcopts.CheckpointOptions
|
||||
if r.Options != nil {
|
||||
if err := proto.Unmarshal(r.Options.Value, &options); err != nil {
|
||||
v, err := typeurl.UnmarshalAny(r.Options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = *v.(*runcopts.CheckpointOptions)
|
||||
}
|
||||
var actions []runc.CheckpointAction
|
||||
if !options.Exit {
|
||||
|
@ -77,7 +77,7 @@ evloop:
|
||||
if err != nil {
|
||||
return UnknownExitStatus, err
|
||||
}
|
||||
if typeurl.Is(evt.Event, eventsapi.RuntimeEvent{}) {
|
||||
if typeurl.Is(evt.Event, &eventsapi.RuntimeEvent{}) {
|
||||
v, err := typeurl.UnmarshalAny(evt.Event)
|
||||
if err != nil {
|
||||
return UnknownExitStatus, err
|
||||
|
2
task.go
2
task.go
@ -160,7 +160,7 @@ func (t *task) Wait(ctx context.Context) (uint32, error) {
|
||||
if err != nil {
|
||||
return UnknownExitStatus, err
|
||||
}
|
||||
if typeurl.Is(evt.Event, eventsapi.RuntimeEvent{}) {
|
||||
if typeurl.Is(evt.Event, &eventsapi.RuntimeEvent{}) {
|
||||
v, err := typeurl.UnmarshalAny(evt.Event)
|
||||
if err != nil {
|
||||
return UnknownExitStatus, err
|
||||
|
@ -33,8 +33,8 @@ func Register(v interface{}, args ...string) {
|
||||
// TypeURL returns the type url for a registred type
|
||||
func TypeURL(v interface{}) (string, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
u, ok := registry[tryDereference(v)]
|
||||
mu.Unlock()
|
||||
if !ok {
|
||||
// fallback to the proto registry if it is a proto message
|
||||
pb, ok := v.(proto.Message)
|
||||
@ -46,7 +46,10 @@ func TypeURL(v interface{}) (string, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Is returns true if the type of the Any is the same as v
|
||||
func Is(any *types.Any, v interface{}) bool {
|
||||
// call to check that v is a pointer
|
||||
tryDereference(v)
|
||||
url, err := TypeURL(v)
|
||||
if err != nil {
|
||||
return false
|
||||
@ -54,11 +57,9 @@ func Is(any *types.Any, v interface{}) bool {
|
||||
return any.TypeUrl == url
|
||||
}
|
||||
|
||||
// MarshalAny marshals the value v into an any with the correct TypeUrl
|
||||
func MarshalAny(v interface{}) (*types.Any, error) {
|
||||
var (
|
||||
err error
|
||||
data []byte
|
||||
)
|
||||
var data []byte
|
||||
url, err := TypeURL(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -78,6 +79,7 @@ func MarshalAny(v interface{}) (*types.Any, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalAny unmarshals the any type into a concrete type
|
||||
func UnmarshalAny(any *types.Any) (interface{}, error) {
|
||||
t, err := getTypeByUrl(any.TypeUrl)
|
||||
if err != nil {
|
||||
@ -120,7 +122,8 @@ func getTypeByUrl(url string) (urlType, error) {
|
||||
func tryDereference(v interface{}) reflect.Type {
|
||||
t := reflect.TypeOf(v)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
// require check of pointer but dereference to register
|
||||
return t.Elem()
|
||||
}
|
||||
return t
|
||||
panic("v is not a pointer to a type")
|
||||
}
|
||||
|
@ -18,35 +18,7 @@ func clear() {
|
||||
registry = make(map[reflect.Type]string)
|
||||
}
|
||||
|
||||
func TestRegsiterValueGetValue(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
|
||||
url, err := TypeURL(test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterValueGetPointer(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
|
||||
url, err := TypeURL(&test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterPointerGetPointer(t *testing.T) {
|
||||
func TestRegisterPointerGetPointer(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(&test{}, "test")
|
||||
@ -60,24 +32,10 @@ func TestRegsiterPointerGetPointer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegsiterPointerGetValue(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
url, err := TypeURL(test{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if url != expected {
|
||||
t.Fatalf("expected %q but received %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
clear()
|
||||
expected := filepath.Join(Prefix, "test")
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@ -94,7 +52,7 @@ func TestMarshal(t *testing.T) {
|
||||
|
||||
func TestMarshalUnmarshal(t *testing.T) {
|
||||
clear()
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@ -122,7 +80,7 @@ func TestMarshalUnmarshal(t *testing.T) {
|
||||
|
||||
func TestIs(t *testing.T) {
|
||||
clear()
|
||||
Register(test{}, "test")
|
||||
Register(&test{}, "test")
|
||||
|
||||
v := &test{
|
||||
Name: "koye",
|
||||
@ -132,7 +90,7 @@ func TestIs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Is(any, test{}) {
|
||||
if !Is(any, &test{}) {
|
||||
t.Fatal("Is(any, test{}) should be true")
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func init() {
|
||||
Type: plugin.RuntimePlugin,
|
||||
Init: New,
|
||||
})
|
||||
typeurl.Register(RuntimeSpec{}, "windows/Spec")
|
||||
typeurl.Register(&RuntimeSpec{}, "windows/Spec")
|
||||
}
|
||||
|
||||
func New(ic *plugin.InitContext) (interface{}, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user