Initial commit
This commit is contained in:
126
vendor/github.com/hyperhq/hypercli/daemon/events/events.go
generated
vendored
Normal file
126
vendor/github.com/hyperhq/hypercli/daemon/events/events.go
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hyperhq/hypercli/pkg/pubsub"
|
||||
eventtypes "github.com/docker/engine-api/types/events"
|
||||
)
|
||||
|
||||
const (
|
||||
eventsLimit = 64
|
||||
bufferSize = 1024
|
||||
)
|
||||
|
||||
// Events is pubsub channel for events generated by the engine.
|
||||
type Events struct {
|
||||
mu sync.Mutex
|
||||
events []eventtypes.Message
|
||||
pub *pubsub.Publisher
|
||||
}
|
||||
|
||||
// New returns new *Events instance
|
||||
func New() *Events {
|
||||
return &Events{
|
||||
events: make([]eventtypes.Message, 0, eventsLimit),
|
||||
pub: pubsub.NewPublisher(100*time.Millisecond, bufferSize),
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe adds new listener to events, returns slice of 64 stored
|
||||
// last events, a channel in which you can expect new events (in form
|
||||
// of interface{}, so you need type assertion), and a function to call
|
||||
// to stop the stream of events.
|
||||
func (e *Events) Subscribe() ([]eventtypes.Message, chan interface{}, func()) {
|
||||
e.mu.Lock()
|
||||
current := make([]eventtypes.Message, len(e.events))
|
||||
copy(current, e.events)
|
||||
l := e.pub.Subscribe()
|
||||
e.mu.Unlock()
|
||||
|
||||
cancel := func() {
|
||||
e.Evict(l)
|
||||
}
|
||||
return current, l, cancel
|
||||
}
|
||||
|
||||
// SubscribeTopic adds new listener to events, returns slice of 64 stored
|
||||
// last events, a channel in which you can expect new events (in form
|
||||
// of interface{}, so you need type assertion).
|
||||
func (e *Events) SubscribeTopic(since, sinceNano int64, ef *Filter) ([]eventtypes.Message, chan interface{}) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
var buffered []eventtypes.Message
|
||||
topic := func(m interface{}) bool {
|
||||
return ef.Include(m.(eventtypes.Message))
|
||||
}
|
||||
|
||||
if since != -1 {
|
||||
for i := len(e.events) - 1; i >= 0; i-- {
|
||||
ev := e.events[i]
|
||||
if ev.Time < since || ((ev.Time == since) && (ev.TimeNano < sinceNano)) {
|
||||
break
|
||||
}
|
||||
if ef.filter.Len() == 0 || topic(ev) {
|
||||
buffered = append([]eventtypes.Message{ev}, buffered...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ch chan interface{}
|
||||
if ef.filter.Len() > 0 {
|
||||
ch = e.pub.SubscribeTopic(topic)
|
||||
} else {
|
||||
// Subscribe to all events if there are no filters
|
||||
ch = e.pub.Subscribe()
|
||||
}
|
||||
|
||||
return buffered, ch
|
||||
}
|
||||
|
||||
// Evict evicts listener from pubsub
|
||||
func (e *Events) Evict(l chan interface{}) {
|
||||
e.pub.Evict(l)
|
||||
}
|
||||
|
||||
// Log broadcasts event to listeners. Each listener has 100 millisecond for
|
||||
// receiving event or it will be skipped.
|
||||
func (e *Events) Log(action, eventType string, actor eventtypes.Actor) {
|
||||
now := time.Now().UTC()
|
||||
jm := eventtypes.Message{
|
||||
Action: action,
|
||||
Type: eventType,
|
||||
Actor: actor,
|
||||
Time: now.Unix(),
|
||||
TimeNano: now.UnixNano(),
|
||||
}
|
||||
|
||||
// fill deprecated fields for container and images
|
||||
switch eventType {
|
||||
case eventtypes.ContainerEventType:
|
||||
jm.ID = actor.ID
|
||||
jm.Status = action
|
||||
jm.From = actor.Attributes["image"]
|
||||
case eventtypes.ImageEventType:
|
||||
jm.ID = actor.ID
|
||||
jm.Status = action
|
||||
}
|
||||
|
||||
e.mu.Lock()
|
||||
if len(e.events) == cap(e.events) {
|
||||
// discard oldest event
|
||||
copy(e.events, e.events[1:])
|
||||
e.events[len(e.events)-1] = jm
|
||||
} else {
|
||||
e.events = append(e.events, jm)
|
||||
}
|
||||
e.mu.Unlock()
|
||||
e.pub.Publish(jm)
|
||||
}
|
||||
|
||||
// SubscribersCount returns number of event listeners
|
||||
func (e *Events) SubscribersCount() int {
|
||||
return e.pub.Len()
|
||||
}
|
||||
152
vendor/github.com/hyperhq/hypercli/daemon/events/events_test.go
generated
vendored
Normal file
152
vendor/github.com/hyperhq/hypercli/daemon/events/events_test.go
generated
vendored
Normal file
@@ -0,0 +1,152 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/engine-api/types/events"
|
||||
)
|
||||
|
||||
func TestEventsLog(t *testing.T) {
|
||||
e := New()
|
||||
_, l1, _ := e.Subscribe()
|
||||
_, l2, _ := e.Subscribe()
|
||||
defer e.Evict(l1)
|
||||
defer e.Evict(l2)
|
||||
count := e.SubscribersCount()
|
||||
if count != 2 {
|
||||
t.Fatalf("Must be 2 subscribers, got %d", count)
|
||||
}
|
||||
actor := events.Actor{
|
||||
ID: "cont",
|
||||
Attributes: map[string]string{"image": "image"},
|
||||
}
|
||||
e.Log("test", events.ContainerEventType, actor)
|
||||
select {
|
||||
case msg := <-l1:
|
||||
jmsg, ok := msg.(events.Message)
|
||||
if !ok {
|
||||
t.Fatalf("Unexpected type %T", msg)
|
||||
}
|
||||
if len(e.events) != 1 {
|
||||
t.Fatalf("Must be only one event, got %d", len(e.events))
|
||||
}
|
||||
if jmsg.Status != "test" {
|
||||
t.Fatalf("Status should be test, got %s", jmsg.Status)
|
||||
}
|
||||
if jmsg.ID != "cont" {
|
||||
t.Fatalf("ID should be cont, got %s", jmsg.ID)
|
||||
}
|
||||
if jmsg.From != "image" {
|
||||
t.Fatalf("From should be image, got %s", jmsg.From)
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("Timeout waiting for broadcasted message")
|
||||
}
|
||||
select {
|
||||
case msg := <-l2:
|
||||
jmsg, ok := msg.(events.Message)
|
||||
if !ok {
|
||||
t.Fatalf("Unexpected type %T", msg)
|
||||
}
|
||||
if len(e.events) != 1 {
|
||||
t.Fatalf("Must be only one event, got %d", len(e.events))
|
||||
}
|
||||
if jmsg.Status != "test" {
|
||||
t.Fatalf("Status should be test, got %s", jmsg.Status)
|
||||
}
|
||||
if jmsg.ID != "cont" {
|
||||
t.Fatalf("ID should be cont, got %s", jmsg.ID)
|
||||
}
|
||||
if jmsg.From != "image" {
|
||||
t.Fatalf("From should be image, got %s", jmsg.From)
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("Timeout waiting for broadcasted message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsLogTimeout(t *testing.T) {
|
||||
e := New()
|
||||
_, l, _ := e.Subscribe()
|
||||
defer e.Evict(l)
|
||||
|
||||
c := make(chan struct{})
|
||||
go func() {
|
||||
actor := events.Actor{
|
||||
ID: "image",
|
||||
}
|
||||
e.Log("test", events.ImageEventType, actor)
|
||||
close(c)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-c:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Timeout publishing message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogEvents(t *testing.T) {
|
||||
e := New()
|
||||
|
||||
for i := 0; i < eventsLimit+16; i++ {
|
||||
action := fmt.Sprintf("action_%d", i)
|
||||
id := fmt.Sprintf("cont_%d", i)
|
||||
from := fmt.Sprintf("image_%d", i)
|
||||
|
||||
actor := events.Actor{
|
||||
ID: id,
|
||||
Attributes: map[string]string{"image": from},
|
||||
}
|
||||
e.Log(action, events.ContainerEventType, actor)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
current, l, _ := e.Subscribe()
|
||||
for i := 0; i < 10; i++ {
|
||||
num := i + eventsLimit + 16
|
||||
action := fmt.Sprintf("action_%d", num)
|
||||
id := fmt.Sprintf("cont_%d", num)
|
||||
from := fmt.Sprintf("image_%d", num)
|
||||
|
||||
actor := events.Actor{
|
||||
ID: id,
|
||||
Attributes: map[string]string{"image": from},
|
||||
}
|
||||
e.Log(action, events.ContainerEventType, actor)
|
||||
}
|
||||
if len(e.events) != eventsLimit {
|
||||
t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
|
||||
}
|
||||
|
||||
var msgs []events.Message
|
||||
for len(msgs) < 10 {
|
||||
m := <-l
|
||||
jm, ok := (m).(events.Message)
|
||||
if !ok {
|
||||
t.Fatalf("Unexpected type %T", m)
|
||||
}
|
||||
msgs = append(msgs, jm)
|
||||
}
|
||||
if len(current) != eventsLimit {
|
||||
t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
|
||||
}
|
||||
first := current[0]
|
||||
if first.Status != "action_16" {
|
||||
t.Fatalf("First action is %s, must be action_16", first.Status)
|
||||
}
|
||||
last := current[len(current)-1]
|
||||
if last.Status != "action_79" {
|
||||
t.Fatalf("Last action is %s, must be action_79", last.Status)
|
||||
}
|
||||
|
||||
firstC := msgs[0]
|
||||
if firstC.Status != "action_80" {
|
||||
t.Fatalf("First action is %s, must be action_80", firstC.Status)
|
||||
}
|
||||
lastC := msgs[len(msgs)-1]
|
||||
if lastC.Status != "action_89" {
|
||||
t.Fatalf("Last action is %s, must be action_89", lastC.Status)
|
||||
}
|
||||
}
|
||||
82
vendor/github.com/hyperhq/hypercli/daemon/events/filter.go
generated
vendored
Normal file
82
vendor/github.com/hyperhq/hypercli/daemon/events/filter.go
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/hyperhq/hypercli/reference"
|
||||
"github.com/docker/engine-api/types/events"
|
||||
"github.com/docker/engine-api/types/filters"
|
||||
)
|
||||
|
||||
// Filter can filter out docker events from a stream
|
||||
type Filter struct {
|
||||
filter filters.Args
|
||||
}
|
||||
|
||||
// NewFilter creates a new Filter
|
||||
func NewFilter(filter filters.Args) *Filter {
|
||||
return &Filter{filter: filter}
|
||||
}
|
||||
|
||||
// Include returns true when the event ev is included by the filters
|
||||
func (ef *Filter) Include(ev events.Message) bool {
|
||||
return ef.filter.ExactMatch("event", ev.Action) &&
|
||||
ef.filter.ExactMatch("type", ev.Type) &&
|
||||
ef.matchContainer(ev) &&
|
||||
ef.matchVolume(ev) &&
|
||||
ef.matchNetwork(ev) &&
|
||||
ef.matchImage(ev) &&
|
||||
ef.matchLabels(ev.Actor.Attributes)
|
||||
}
|
||||
|
||||
func (ef *Filter) matchLabels(attributes map[string]string) bool {
|
||||
if !ef.filter.Include("label") {
|
||||
return true
|
||||
}
|
||||
return ef.filter.MatchKVList("label", attributes)
|
||||
}
|
||||
|
||||
func (ef *Filter) matchContainer(ev events.Message) bool {
|
||||
return ef.fuzzyMatchName(ev, events.ContainerEventType)
|
||||
}
|
||||
|
||||
func (ef *Filter) matchVolume(ev events.Message) bool {
|
||||
return ef.fuzzyMatchName(ev, events.VolumeEventType)
|
||||
}
|
||||
|
||||
func (ef *Filter) matchNetwork(ev events.Message) bool {
|
||||
return ef.fuzzyMatchName(ev, events.NetworkEventType)
|
||||
}
|
||||
|
||||
func (ef *Filter) fuzzyMatchName(ev events.Message, eventType string) bool {
|
||||
return ef.filter.FuzzyMatch(eventType, ev.Actor.ID) ||
|
||||
ef.filter.FuzzyMatch(eventType, ev.Actor.Attributes["name"])
|
||||
}
|
||||
|
||||
// matchImage matches against both event.Actor.ID (for image events)
|
||||
// and event.Actor.Attributes["image"] (for container events), so that any container that was created
|
||||
// from an image will be included in the image events. Also compare both
|
||||
// against the stripped repo name without any tags.
|
||||
func (ef *Filter) matchImage(ev events.Message) bool {
|
||||
id := ev.Actor.ID
|
||||
nameAttr := "image"
|
||||
var imageName string
|
||||
|
||||
if ev.Type == events.ImageEventType {
|
||||
nameAttr = "name"
|
||||
}
|
||||
|
||||
if n, ok := ev.Actor.Attributes[nameAttr]; ok {
|
||||
imageName = n
|
||||
}
|
||||
return ef.filter.ExactMatch("image", id) ||
|
||||
ef.filter.ExactMatch("image", imageName) ||
|
||||
ef.filter.ExactMatch("image", stripTag(id)) ||
|
||||
ef.filter.ExactMatch("image", stripTag(imageName))
|
||||
}
|
||||
|
||||
func stripTag(image string) string {
|
||||
ref, err := reference.ParseNamed(image)
|
||||
if err != nil {
|
||||
return image
|
||||
}
|
||||
return ref.Name()
|
||||
}
|
||||
Reference in New Issue
Block a user