diff --git a/client.go b/client.go index 7acda0841..81fb81103 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/linux/runcopts/options.go b/linux/runcopts/options.go index 123f3deae..b45be84cc 100644 --- a/linux/runcopts/options.go +++ b/linux/runcopts/options.go @@ -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, diff --git a/linux/shim/init.go b/linux/shim/init.go index 2e9aa6c93..d8388b39c 100644 --- a/linux/shim/init.go +++ b/linux/shim/init.go @@ -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 { diff --git a/process.go b/process.go index 7825f36ff..97ab94007 100644 --- a/process.go +++ b/process.go @@ -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 diff --git a/task.go b/task.go index 19a63b1e9..f55f60fda 100644 --- a/task.go +++ b/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 diff --git a/typeurl/types.go b/typeurl/types.go index 6184d2fb1..a93a5481b 100644 --- a/typeurl/types.go +++ b/typeurl/types.go @@ -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") } diff --git a/typeurl/types_test.go b/typeurl/types_test.go index 9f9bd3e2f..1535eeec1 100644 --- a/typeurl/types_test.go +++ b/typeurl/types_test.go @@ -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") } } diff --git a/windows/runtime.go b/windows/runtime.go index 289f1c940..675513a55 100644 --- a/windows/runtime.go +++ b/windows/runtime.go @@ -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) {