github.com/olljanat/moby@v1.13.1/daemon/events/filter.go (about)

     1  package events
     2  
     3  import (
     4  	"github.com/docker/docker/api/types/events"
     5  	"github.com/docker/docker/api/types/filters"
     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  }
    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.matchEvent(ev) &&
    22  		ef.filter.ExactMatch("type", ev.Type) &&
    23  		ef.matchDaemon(ev) &&
    24  		ef.matchContainer(ev) &&
    25  		ef.matchPlugin(ev) &&
    26  		ef.matchVolume(ev) &&
    27  		ef.matchNetwork(ev) &&
    28  		ef.matchImage(ev) &&
    29  		ef.matchLabels(ev.Actor.Attributes)
    30  }
    31  
    32  func (ef *Filter) matchEvent(ev events.Message) bool {
    33  	// #25798 if an event filter contains either health_status, exec_create or exec_start without a colon
    34  	// Let's to a FuzzyMatch instead of an ExactMatch.
    35  	if ef.filterContains("event", map[string]struct{}{"health_status": {}, "exec_create": {}, "exec_start": {}}) {
    36  		return ef.filter.FuzzyMatch("event", ev.Action)
    37  	}
    38  	return ef.filter.ExactMatch("event", ev.Action)
    39  }
    40  
    41  func (ef *Filter) filterContains(field string, values map[string]struct{}) bool {
    42  	for _, v := range ef.filter.Get(field) {
    43  		if _, ok := values[v]; ok {
    44  			return true
    45  		}
    46  	}
    47  	return false
    48  }
    49  
    50  func (ef *Filter) matchLabels(attributes map[string]string) bool {
    51  	if !ef.filter.Include("label") {
    52  		return true
    53  	}
    54  	return ef.filter.MatchKVList("label", attributes)
    55  }
    56  
    57  func (ef *Filter) matchDaemon(ev events.Message) bool {
    58  	return ef.fuzzyMatchName(ev, events.DaemonEventType)
    59  }
    60  
    61  func (ef *Filter) matchContainer(ev events.Message) bool {
    62  	return ef.fuzzyMatchName(ev, events.ContainerEventType)
    63  }
    64  
    65  func (ef *Filter) matchPlugin(ev events.Message) bool {
    66  	return ef.fuzzyMatchName(ev, events.PluginEventType)
    67  }
    68  
    69  func (ef *Filter) matchVolume(ev events.Message) bool {
    70  	return ef.fuzzyMatchName(ev, events.VolumeEventType)
    71  }
    72  
    73  func (ef *Filter) matchNetwork(ev events.Message) bool {
    74  	return ef.fuzzyMatchName(ev, events.NetworkEventType)
    75  }
    76  
    77  func (ef *Filter) fuzzyMatchName(ev events.Message, eventType string) bool {
    78  	return ef.filter.FuzzyMatch(eventType, ev.Actor.ID) ||
    79  		ef.filter.FuzzyMatch(eventType, ev.Actor.Attributes["name"])
    80  }
    81  
    82  // matchImage matches against both event.Actor.ID (for image events)
    83  // and event.Actor.Attributes["image"] (for container events), so that any container that was created
    84  // from an image will be included in the image events. Also compare both
    85  // against the stripped repo name without any tags.
    86  func (ef *Filter) matchImage(ev events.Message) bool {
    87  	id := ev.Actor.ID
    88  	nameAttr := "image"
    89  	var imageName string
    90  
    91  	if ev.Type == events.ImageEventType {
    92  		nameAttr = "name"
    93  	}
    94  
    95  	if n, ok := ev.Actor.Attributes[nameAttr]; ok {
    96  		imageName = n
    97  	}
    98  	return ef.filter.ExactMatch("image", id) ||
    99  		ef.filter.ExactMatch("image", imageName) ||
   100  		ef.filter.ExactMatch("image", stripTag(id)) ||
   101  		ef.filter.ExactMatch("image", stripTag(imageName))
   102  }
   103  
   104  func stripTag(image string) string {
   105  	ref, err := reference.ParseNamed(image)
   106  	if err != nil {
   107  		return image
   108  	}
   109  	return ref.Name()
   110  }