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>
59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/containerd/containerd/api/services/events/v1"
|
|
"github.com/containerd/containerd/namespaces"
|
|
goevents "github.com/docker/go-events"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type sinkEvent struct {
|
|
ctx context.Context
|
|
event Event
|
|
}
|
|
|
|
type eventSink struct {
|
|
ns string
|
|
ch chan *events.Envelope
|
|
}
|
|
|
|
func (s *eventSink) Write(evt goevents.Event) error {
|
|
e, ok := evt.(*sinkEvent)
|
|
if !ok {
|
|
return errors.New("event is not a sink event")
|
|
}
|
|
topic := getTopic(e.ctx)
|
|
|
|
ns, _ := namespaces.Namespace(e.ctx)
|
|
if ns != "" && ns != s.ns {
|
|
// ignore events not intended for this ns
|
|
return nil
|
|
}
|
|
|
|
eventData, err := MarshalEvent(e.event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
"topic": topic,
|
|
"type": eventData.TypeUrl,
|
|
"ns": ns,
|
|
}).Debug("event")
|
|
|
|
s.ch <- &events.Envelope{
|
|
Timestamp: time.Now(),
|
|
Topic: topic,
|
|
Event: eventData,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *eventSink) Close() error {
|
|
return nil
|
|
}
|