Move events exchange into subpackage

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-11-07 09:53:42 -05:00
parent 526d15bd86
commit a70b95b202
7 changed files with 33 additions and 31 deletions

View File

@@ -1,12 +1,13 @@
package events
package exchange
import (
"context"
"strings"
"time"
events "github.com/containerd/containerd/api/services/events/v1"
v1 "github.com/containerd/containerd/api/services/events/v1"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events"
"github.com/containerd/containerd/filters"
"github.com/containerd/containerd/identifiers"
"github.com/containerd/containerd/log"
@@ -34,7 +35,7 @@ func NewExchange() *Exchange {
//
// This is useful when an event is forwaded on behalf of another namespace or
// when the event is propagated on behalf of another publisher.
func (e *Exchange) Forward(ctx context.Context, envelope *events.Envelope) (err error) {
func (e *Exchange) Forward(ctx context.Context, envelope *v1.Envelope) (err error) {
if err := validateEnvelope(envelope); err != nil {
return err
}
@@ -59,11 +60,11 @@ func (e *Exchange) Forward(ctx context.Context, envelope *events.Envelope) (err
// Publish packages and sends an event. The caller will be considered the
// initial publisher of the event. This means the timestamp will be calculated
// at this point and this method may read from the calling context.
func (e *Exchange) Publish(ctx context.Context, topic string, event Event) (err error) {
func (e *Exchange) Publish(ctx context.Context, topic string, event events.Event) (err error) {
var (
namespace string
encoded *types.Any
envelope events.Envelope
envelope v1.Envelope
)
namespace, err = namespaces.NamespaceRequired(ctx)
@@ -108,9 +109,9 @@ func (e *Exchange) Publish(ctx context.Context, topic string, event Event) (err
// Zero or more filters may be provided as strings. Only events that match
// *any* of the provided filters will be sent on the channel. The filters use
// the standard containerd filters package syntax.
func (e *Exchange) Subscribe(ctx context.Context, fs ...string) (ch <-chan *events.Envelope, errs <-chan error) {
func (e *Exchange) Subscribe(ctx context.Context, fs ...string) (ch <-chan *v1.Envelope, errs <-chan error) {
var (
evch = make(chan *events.Envelope)
evch = make(chan *v1.Envelope)
errq = make(chan error, 1)
channel = goevents.NewChannel(0)
queue = goevents.NewQueue(channel)
@@ -150,7 +151,7 @@ func (e *Exchange) Subscribe(ctx context.Context, fs ...string) (ch <-chan *even
for {
select {
case ev := <-channel.C:
env, ok := ev.(*events.Envelope)
env, ok := ev.(*v1.Envelope)
if !ok {
// TODO(stevvooe): For the most part, we are well protected
// from this condition. Both Forward and Publish protect
@@ -204,7 +205,7 @@ func validateTopic(topic string) error {
return nil
}
func validateEnvelope(envelope *events.Envelope) error {
func validateEnvelope(envelope *v1.Envelope) error {
if err := namespaces.Validate(envelope.Namespace); err != nil {
return errors.Wrapf(err, "event envelope has invalid namespace")
}

View File

@@ -1,4 +1,4 @@
package events
package exchange
import (
"context"
@@ -8,8 +8,9 @@ import (
"testing"
"time"
events "github.com/containerd/containerd/api/services/events/v1"
v1 "github.com/containerd/containerd/api/services/events/v1"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
@@ -17,10 +18,10 @@ import (
func TestExchangeBasic(t *testing.T) {
ctx := namespaces.WithNamespace(context.Background(), t.Name())
testevents := []Event{
&events.ContainerCreate{ID: "asdf"},
&events.ContainerCreate{ID: "qwer"},
&events.ContainerCreate{ID: "zxcv"},
testevents := []events.Event{
&v1.ContainerCreate{ID: "asdf"},
&v1.ContainerCreate{ID: "qwer"},
&v1.ContainerCreate{ID: "zxcv"},
}
exchange := NewExchange()
@@ -55,7 +56,7 @@ func TestExchangeBasic(t *testing.T) {
wg.Wait()
for _, subscriber := range []struct {
eventq <-chan *events.Envelope
eventq <-chan *v1.Envelope
errq <-chan error
cancel func()
}{
@@ -79,7 +80,7 @@ func TestExchangeBasic(t *testing.T) {
if err != nil {
t.Fatal(err)
}
received = append(received, ev.(*events.ContainerCreate))
received = append(received, ev.(*v1.ContainerCreate))
case err := <-subscriber.errq:
if err != nil {
t.Fatal(err)
@@ -117,7 +118,7 @@ func TestExchangeValidateTopic(t *testing.T) {
},
} {
t.Run(testcase.input, func(t *testing.T) {
event := &events.ContainerCreate{ID: t.Name()}
event := &v1.ContainerCreate{ID: t.Name()}
if err := exchange.Publish(ctx, testcase.input, event); errors.Cause(err) != testcase.err {
if err == nil {
t.Fatalf("expected error %v, received nil", testcase.err)
@@ -131,7 +132,7 @@ func TestExchangeValidateTopic(t *testing.T) {
t.Fatal(err)
}
envelope := events.Envelope{
envelope := v1.Envelope{
Timestamp: time.Now().UTC(),
Namespace: namespace,
Topic: testcase.input,