2018-02-05 16:05:59 -05:00
|
|
|
package events // import "github.com/docker/docker/daemon/events"
|
2015-09-23 14:51:43 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/events"
|
|
|
|
"github.com/docker/docker/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 {
|
2016-10-27 19:14:13 -04:00
|
|
|
return ef.matchEvent(ev) &&
|
2015-12-28 15:15:34 -05:00
|
|
|
ef.filter.ExactMatch("type", ev.Type) &&
|
2017-04-02 18:21:56 -04:00
|
|
|
ef.matchScope(ev.Scope) &&
|
2016-05-08 19:15:33 -04:00
|
|
|
ef.matchDaemon(ev) &&
|
2015-12-21 17:55:23 -05:00
|
|
|
ef.matchContainer(ev) &&
|
2016-07-18 11:02:12 -04:00
|
|
|
ef.matchPlugin(ev) &&
|
2015-12-28 15:15:34 -05:00
|
|
|
ef.matchVolume(ev) &&
|
|
|
|
ef.matchNetwork(ev) &&
|
|
|
|
ef.matchImage(ev) &&
|
2017-09-23 00:08:38 -04:00
|
|
|
ef.matchNode(ev) &&
|
|
|
|
ef.matchService(ev) &&
|
|
|
|
ef.matchSecret(ev) &&
|
|
|
|
ef.matchConfig(ev) &&
|
2015-12-28 15:15:34 -05:00
|
|
|
ef.matchLabels(ev.Actor.Attributes)
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 19:14:13 -04:00
|
|
|
func (ef *Filter) matchEvent(ev events.Message) bool {
|
|
|
|
// #25798 if an event filter contains either health_status, exec_create or exec_start without a colon
|
|
|
|
// Let's to a FuzzyMatch instead of an ExactMatch.
|
|
|
|
if ef.filterContains("event", map[string]struct{}{"health_status": {}, "exec_create": {}, "exec_start": {}}) {
|
|
|
|
return ef.filter.FuzzyMatch("event", ev.Action)
|
|
|
|
}
|
|
|
|
return ef.filter.ExactMatch("event", ev.Action)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ef *Filter) filterContains(field string, values map[string]struct{}) bool {
|
|
|
|
for _, v := range ef.filter.Get(field) {
|
|
|
|
if _, ok := values[v]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-02 18:21:56 -04:00
|
|
|
func (ef *Filter) matchScope(scope string) bool {
|
2017-09-26 07:39:56 -04:00
|
|
|
if !ef.filter.Contains("scope") {
|
2017-04-02 18:21:56 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ef.filter.ExactMatch("scope", scope)
|
|
|
|
}
|
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
func (ef *Filter) matchLabels(attributes map[string]string) bool {
|
2017-09-26 07:39:56 -04:00
|
|
|
if !ef.filter.Contains("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)
|
|
|
|
}
|
|
|
|
|
2016-07-18 11:02:12 -04:00
|
|
|
func (ef *Filter) matchPlugin(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.PluginEventType)
|
|
|
|
}
|
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-02 18:21:56 -04:00
|
|
|
func (ef *Filter) matchService(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.ServiceEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ef *Filter) matchNode(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.NodeEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ef *Filter) matchSecret(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.SecretEventType)
|
|
|
|
}
|
|
|
|
|
2017-07-09 23:41:25 -04:00
|
|
|
func (ef *Filter) matchConfig(ev events.Message) bool {
|
|
|
|
return ef.fuzzyMatchName(ev, events.ConfigEventType)
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(image)
|
2015-11-25 20:27:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return image
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
return reference.FamiliarName(ref)
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|