1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/events/filter.go
Tonis Tiigi 4352da7803 Update daemon and docker core to use new content addressable storage
Add distribution package for managing pulls and pushes. This is based on
the old code in the graph package, with major changes to work with the
new image/layer model.

Add v1 migration code.

Update registry, api/*, and daemon packages to use the reference
package's types where applicable.

Update daemon package to use image/layer/tag stores instead of the graph
package

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2015-11-24 09:40:25 -08:00

67 lines
1.9 KiB
Go

package events
import (
"github.com/docker/distribution/reference"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/parsers/filters"
)
// 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 {
return isFieldIncluded(ev.Status, ef.filter["event"]) &&
isFieldIncluded(ev.ID, ef.filter["container"]) &&
ef.isImageIncluded(ev.ID, ev.From) &&
ef.isLabelFieldIncluded(ev.ID)
}
func (ef *Filter) isLabelFieldIncluded(id string) bool {
if _, ok := ef.filter["label"]; !ok {
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 {
stripTag := func(image string) string {
ref, err := reference.ParseNamed(image)
if err != nil {
return image
}
return ref.Name()
}
return isFieldIncluded(eventID, ef.filter["image"]) ||
isFieldIncluded(eventFrom, ef.filter["image"]) ||
isFieldIncluded(stripTag(eventID), ef.filter["image"]) ||
isFieldIncluded(stripTag(eventFrom), ef.filter["image"])
}
func isFieldIncluded(field string, filter []string) bool {
if len(field) == 0 {
return true
}
if len(filter) == 0 {
return true
}
for _, v := range filter {
if v == field {
return true
}
}
return false
}