Replace events/convert with typeurl
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
typesPrefix = "types.containerd.io/"
|
||||
)
|
||||
|
||||
// MarshalEvent marshal the event into an any, namespacing the type url to the
|
||||
// containerd types.
|
||||
func MarshalEvent(event Event) (*types.Any, error) {
|
||||
pb, ok := event.(proto.Message)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("%T not a protobuf", event)
|
||||
}
|
||||
|
||||
url := typesPrefix + proto.MessageName(pb)
|
||||
val, err := proto.Marshal(pb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.Any{
|
||||
TypeUrl: url,
|
||||
Value: val,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DynamEvent acts as a holder type for unmarshaling events where the type is
|
||||
// not previously known.
|
||||
type DynamicEvent struct {
|
||||
Event
|
||||
}
|
||||
|
||||
// UnmarshalEvent provides an event object based on the provided any.
|
||||
//
|
||||
// Use with DynamicEvent (or protobuf/types.DynamicAny) if the type is not
|
||||
// known before hand.
|
||||
func UnmarshalEvent(any *types.Any, event Event) error {
|
||||
switch v := event.(type) {
|
||||
case proto.Message:
|
||||
if err := types.UnmarshalAny(any, v); err != nil {
|
||||
return errors.Wrapf(err, "failed to unmarshal event %v", any)
|
||||
}
|
||||
case *DynamicEvent:
|
||||
var da types.DynamicAny
|
||||
|
||||
if err := types.UnmarshalAny(any, &da); err != nil {
|
||||
return errors.Wrapf(err, "failed to unmarshal event %v", any)
|
||||
}
|
||||
v.Event = da.Message
|
||||
default:
|
||||
return errors.Errorf("unsupported event type: %T", event)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// Is returns true if the event in any will unmarashal into the provided event.
|
||||
func Is(any *types.Any, event Event) bool {
|
||||
pb, ok := event.(proto.Message)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return types.Is(any, pb)
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
events "github.com/containerd/containerd/api/services/events/v1"
|
||||
)
|
||||
|
||||
func TestMarshalEvent(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
event Event
|
||||
url string
|
||||
}{
|
||||
{
|
||||
event: &events.TaskStart{},
|
||||
url: "types.containerd.io/containerd.services.events.v1.TaskStart",
|
||||
},
|
||||
|
||||
{
|
||||
event: &events.NamespaceUpdate{},
|
||||
url: "types.containerd.io/containerd.services.events.v1.NamespaceUpdate",
|
||||
},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%T", testcase.event), func(t *testing.T) {
|
||||
a, err := MarshalEvent(testcase.event)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if a.TypeUrl != testcase.url {
|
||||
t.Fatalf("unexpected url: %v != %v", a.TypeUrl, testcase.url)
|
||||
}
|
||||
|
||||
var de DynamicEvent
|
||||
if err := UnmarshalEvent(a, &de); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(de.Event, testcase.event) {
|
||||
t.Fatalf("round trip failed %v != %v", de.Event, testcase.event)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMarshalEvent(b *testing.B) {
|
||||
ev := &events.TaskStart{}
|
||||
expected, err := MarshalEvent(ev)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
a, err := MarshalEvent(ev)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if a.TypeUrl != expected.TypeUrl {
|
||||
b.Fatalf("incorrect type url: %v != %v", a, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/containerd/containerd/api/services/events/v1"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/containerd/typeurl"
|
||||
goevents "github.com/docker/go-events"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -35,7 +36,7 @@ func (s *eventSink) Write(evt goevents.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
eventData, err := MarshalEvent(e.event)
|
||||
eventData, err := typeurl.MarshalAny(e.event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user