2015-09-23 14:51:43 -04:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2015-12-15 16:40:11 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2015-09-23 14:51:43 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-12-04 16:55:15 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2015-09-23 14:51:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Filter can filter out docker events from a stream
|
|
|
|
type Filter struct {
|
|
|
|
filter filters.Args
|
|
|
|
getLabels func(id string) map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilter creates a new Filter
|
|
|
|
func NewFilter(filter filters.Args, getLabels func(id string) map[string]string) *Filter {
|
|
|
|
return &Filter{filter: filter, getLabels: getLabels}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include returns true when the event ev is included by the filters
|
|
|
|
func (ef *Filter) Include(ev *jsonmessage.JSONMessage) bool {
|
2015-11-25 20:27:11 -05:00
|
|
|
return ef.filter.ExactMatch("event", ev.Status) &&
|
|
|
|
ef.filter.ExactMatch("container", ev.ID) &&
|
2015-09-23 14:51:43 -04:00
|
|
|
ef.isImageIncluded(ev.ID, ev.From) &&
|
|
|
|
ef.isLabelFieldIncluded(ev.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ef *Filter) isLabelFieldIncluded(id 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
|
|
|
|
}
|
|
|
|
return ef.filter.MatchKVList("label", ef.getLabels(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The image filter will be matched against both event.ID (for image events)
|
|
|
|
// and event.From (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) isImageIncluded(eventID string, eventFrom string) bool {
|
2015-11-25 20:27:11 -05:00
|
|
|
return ef.filter.ExactMatch("image", eventID) ||
|
|
|
|
ef.filter.ExactMatch("image", eventFrom) ||
|
|
|
|
ef.filter.ExactMatch("image", stripTag(eventID)) ||
|
|
|
|
ef.filter.ExactMatch("image", stripTag(eventFrom))
|
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
|
|
|
}
|