
When using events, it was found to be fairly unwieldy with a number of extra packages. For the most part, when interacting with the events service, we want types of the same version of the service. This has been accomplished by moving all events types into the events package. In addition, several fixes to the way events are marshaled have been included. Specifically, we defer to the protobuf type registration system to assemble events and type urls, with a little bit sheen on top of add a containerd.io oriented namespace. This has resulted in much cleaner event consumption and has removed the reliance on error prone type urls, in favor of concrete types. Signed-off-by: Stephen J Day <stephen.day@docker.com>
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
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)
|
|
}
|