2015-09-23 14:51:43 -04:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2015-12-04 16:55:15 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types/events"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-09-23 14:51:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Filter can filter out docker events from a stream
|
|
|
|
type Filter struct {
|
2015-12-21 17:55:23 -05:00
|
|
|
filter filters.Args
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilter creates a new Filter
|
2015-12-21 17:55:23 -05:00
|
|
|
func NewFilter(filter filters.Args) *Filter {
|
|
|
|
return &Filter{filter: filter}
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include returns true when the event ev is included by the filters
|
2015-12-21 17:55:23 -05:00
|
|
|
func (ef *Filter) Include(ev events.Message) bool {
|
|
|
|
return ef.filter.ExactMatch("event", ev.Action) &&
|
2015-12-28 15:15:34 -05:00
|
|
|
ef.filter.ExactMatch("type", ev.Type) &&
|
2016-05-08 19:15:33 -04:00
|
|
|
ef.matchDaemon(ev) &&
|
2015-12-21 17:55:23 -05:00
|
|
|
ef.matchContainer(ev) &&
|
2015-12-28 15:15:34 -05:00
|
|
|
ef.matchVolume(ev) &&
|
|
|
|
ef.matchNetwork(ev) &&
|
|
|
|
ef.matchImage(ev) &&
|
|
|
|
ef.matchLabels(ev.Actor.Attributes)
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
func (ef *Filter) matchLabels(attributes map[string]string) bool {
|
2015-11-25 20:27:11 -05:00
|
|
|
if !ef.filter.Include("label") {
|
2015-09-23 14:51:43 -04:00
|
|
|
return true
|
|
|
|
}
|
2015-12-21 17:55:23 -05:00
|
|
|
return ef.filter.MatchKVList("label", attributes)
|
|
|
|
}
|
|
|
|
|
2016-05-08 19:15:33 -04:00
|
|
|
func (ef *Filter) matchDaemon(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.DaemonEventType)
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
func (ef *Filter) matchContainer(ev events.Message) bool {
|
2015-12-28 15:15:34 -05:00
|
|
|
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)
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
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
|
2015-09-23 14:51:43 -04:00
|
|
|
// from an image will be included in the image events. Also compare both
|
|
|
|
// against the stripped repo name without any tags.
|
2015-12-28 15:15:34 -05:00
|
|
|
func (ef *Filter) matchImage(ev events.Message) bool {
|
|
|
|
id := ev.Actor.ID
|
|
|
|
nameAttr := "image"
|
2015-12-21 17:55:23 -05:00
|
|
|
var imageName string
|
2015-12-28 15:15:34 -05:00
|
|
|
|
|
|
|
if ev.Type == events.ImageEventType {
|
|
|
|
nameAttr = "name"
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := ev.Actor.Attributes[nameAttr]; ok {
|
2015-12-21 17:55:23 -05:00
|
|
|
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))
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
func stripTag(image string) string {
|
|
|
|
ref, err := reference.ParseNamed(image)
|
|
|
|
if err != nil {
|
|
|
|
return image
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
2015-11-25 20:27:11 -05:00
|
|
|
return ref.Name()
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|