forked from moby/moby
/
filter.go
52 lines (45 loc) · 1.62 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 ef.filter.ExactMatch("event", ev.Status) &&
ef.filter.ExactMatch("container", ev.ID) &&
ef.isImageIncluded(ev.ID, ev.From) &&
ef.isLabelFieldIncluded(ev.ID)
}
func (ef *Filter) isLabelFieldIncluded(id string) bool {
if !ef.filter.Include("label") {
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 {
return ef.filter.ExactMatch("image", eventID) ||
ef.filter.ExactMatch("image", eventFrom) ||
ef.filter.ExactMatch("image", stripTag(eventID)) ||
ef.filter.ExactMatch("image", stripTag(eventFrom))
}
func stripTag(image string) string {
ref, err := reference.ParseNamed(image)
if err != nil {
return image
}
return ref.Name()
}