github.com/ojongerius/docker@v1.11.2/daemon/events/filter.go (about)

     1  package events
     2  
     3  import (
     4  	"github.com/docker/docker/reference"
     5  	"github.com/docker/engine-api/types/events"
     6  	"github.com/docker/engine-api/types/filters"
     7  )
     8  
     9  // Filter can filter out docker events from a stream
    10  type Filter struct {
    11  	filter filters.Args
    12  }
    13  
    14  // NewFilter creates a new Filter
    15  func NewFilter(filter filters.Args) *Filter {
    16  	return &Filter{filter: filter}
    17  }
    18  
    19  // Include returns true when the event ev is included by the filters
    20  func (ef *Filter) Include(ev events.Message) bool {
    21  	return ef.filter.ExactMatch("event", ev.Action) &&
    22  		ef.filter.ExactMatch("type", ev.Type) &&
    23  		ef.matchContainer(ev) &&
    24  		ef.matchVolume(ev) &&
    25  		ef.matchNetwork(ev) &&
    26  		ef.matchImage(ev) &&
    27  		ef.matchLabels(ev.Actor.Attributes)
    28  }
    29  
    30  func (ef *Filter) matchLabels(attributes map[string]string) bool {
    31  	if !ef.filter.Include("label") {
    32  		return true
    33  	}
    34  	return ef.filter.MatchKVList("label", attributes)
    35  }
    36  
    37  func (ef *Filter) matchContainer(ev events.Message) bool {
    38  	return ef.fuzzyMatchName(ev, events.ContainerEventType)
    39  }
    40  
    41  func (ef *Filter) matchVolume(ev events.Message) bool {
    42  	return ef.fuzzyMatchName(ev, events.VolumeEventType)
    43  }
    44  
    45  func (ef *Filter) matchNetwork(ev events.Message) bool {
    46  	return ef.fuzzyMatchName(ev, events.NetworkEventType)
    47  }
    48  
    49  func (ef *Filter) fuzzyMatchName(ev events.Message, eventType string) bool {
    50  	return ef.filter.FuzzyMatch(eventType, ev.Actor.ID) ||
    51  		ef.filter.FuzzyMatch(eventType, ev.Actor.Attributes["name"])
    52  }
    53  
    54  // matchImage matches against both event.Actor.ID (for image events)
    55  // and event.Actor.Attributes["image"] (for container events), so that any container that was created
    56  // from an image will be included in the image events. Also compare both
    57  // against the stripped repo name without any tags.
    58  func (ef *Filter) matchImage(ev events.Message) bool {
    59  	id := ev.Actor.ID
    60  	nameAttr := "image"
    61  	var imageName string
    62  
    63  	if ev.Type == events.ImageEventType {
    64  		nameAttr = "name"
    65  	}
    66  
    67  	if n, ok := ev.Actor.Attributes[nameAttr]; ok {
    68  		imageName = n
    69  	}
    70  	return ef.filter.ExactMatch("image", id) ||
    71  		ef.filter.ExactMatch("image", imageName) ||
    72  		ef.filter.ExactMatch("image", stripTag(id)) ||
    73  		ef.filter.ExactMatch("image", stripTag(imageName))
    74  }
    75  
    76  func stripTag(image string) string {
    77  	ref, err := reference.ParseNamed(image)
    78  	if err != nil {
    79  		return image
    80  	}
    81  	return ref.Name()
    82  }