github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/daemon/events/filter.go (about) 1 package events 2 3 import ( 4 "github.com/docker/docker/api/types/filters" 5 "github.com/docker/docker/pkg/jsonmessage" 6 "github.com/docker/docker/reference" 7 ) 8 9 // Filter can filter out docker events from a stream 10 type Filter struct { 11 filter filters.Args 12 getLabels func(id string) map[string]string 13 } 14 15 // NewFilter creates a new Filter 16 func NewFilter(filter filters.Args, getLabels func(id string) map[string]string) *Filter { 17 return &Filter{filter: filter, getLabels: getLabels} 18 } 19 20 // Include returns true when the event ev is included by the filters 21 func (ef *Filter) Include(ev *jsonmessage.JSONMessage) bool { 22 return ef.filter.ExactMatch("event", ev.Status) && 23 ef.filter.ExactMatch("container", ev.ID) && 24 ef.isImageIncluded(ev.ID, ev.From) && 25 ef.isLabelFieldIncluded(ev.ID) 26 } 27 28 func (ef *Filter) isLabelFieldIncluded(id string) bool { 29 if !ef.filter.Include("label") { 30 return true 31 } 32 return ef.filter.MatchKVList("label", ef.getLabels(id)) 33 } 34 35 // The image filter will be matched against both event.ID (for image events) 36 // and event.From (for container events), so that any container that was created 37 // from an image will be included in the image events. Also compare both 38 // against the stripped repo name without any tags. 39 func (ef *Filter) isImageIncluded(eventID string, eventFrom string) bool { 40 return ef.filter.ExactMatch("image", eventID) || 41 ef.filter.ExactMatch("image", eventFrom) || 42 ef.filter.ExactMatch("image", stripTag(eventID)) || 43 ef.filter.ExactMatch("image", stripTag(eventFrom)) 44 } 45 46 func stripTag(image string) string { 47 ref, err := reference.ParseNamed(image) 48 if err != nil { 49 return image 50 } 51 return ref.Name() 52 }