gopkg.in/docker/docker.v1@v1.13.1/daemon/events.go (about)

     1  package daemon
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/docker/docker/api/types/events"
     8  	"github.com/docker/docker/api/types/filters"
     9  	"github.com/docker/docker/container"
    10  	daemonevents "github.com/docker/docker/daemon/events"
    11  	"github.com/docker/libnetwork"
    12  )
    13  
    14  // LogContainerEvent generates an event related to a container with only the default attributes.
    15  func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) {
    16  	daemon.LogContainerEventWithAttributes(container, action, map[string]string{})
    17  }
    18  
    19  // LogContainerEventWithAttributes generates an event related to a container with specific given attributes.
    20  func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action string, attributes map[string]string) {
    21  	copyAttributes(attributes, container.Config.Labels)
    22  	if container.Config.Image != "" {
    23  		attributes["image"] = container.Config.Image
    24  	}
    25  	attributes["name"] = strings.TrimLeft(container.Name, "/")
    26  
    27  	actor := events.Actor{
    28  		ID:         container.ID,
    29  		Attributes: attributes,
    30  	}
    31  	daemon.EventsService.Log(action, events.ContainerEventType, actor)
    32  }
    33  
    34  // LogImageEvent generates an event related to an image with only the default attributes.
    35  func (daemon *Daemon) LogImageEvent(imageID, refName, action string) {
    36  	daemon.LogImageEventWithAttributes(imageID, refName, action, map[string]string{})
    37  }
    38  
    39  // LogImageEventWithAttributes generates an event related to an image with specific given attributes.
    40  func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
    41  	img, err := daemon.GetImage(imageID)
    42  	if err == nil && img.Config != nil {
    43  		// image has not been removed yet.
    44  		// it could be missing if the event is `delete`.
    45  		copyAttributes(attributes, img.Config.Labels)
    46  	}
    47  	if refName != "" {
    48  		attributes["name"] = refName
    49  	}
    50  	actor := events.Actor{
    51  		ID:         imageID,
    52  		Attributes: attributes,
    53  	}
    54  
    55  	daemon.EventsService.Log(action, events.ImageEventType, actor)
    56  }
    57  
    58  // LogPluginEvent generates an event related to a plugin with only the default attributes.
    59  func (daemon *Daemon) LogPluginEvent(pluginID, refName, action string) {
    60  	daemon.LogPluginEventWithAttributes(pluginID, refName, action, map[string]string{})
    61  }
    62  
    63  // LogPluginEventWithAttributes generates an event related to a plugin with specific given attributes.
    64  func (daemon *Daemon) LogPluginEventWithAttributes(pluginID, refName, action string, attributes map[string]string) {
    65  	attributes["name"] = refName
    66  	actor := events.Actor{
    67  		ID:         pluginID,
    68  		Attributes: attributes,
    69  	}
    70  	daemon.EventsService.Log(action, events.PluginEventType, actor)
    71  }
    72  
    73  // LogVolumeEvent generates an event related to a volume.
    74  func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
    75  	actor := events.Actor{
    76  		ID:         volumeID,
    77  		Attributes: attributes,
    78  	}
    79  	daemon.EventsService.Log(action, events.VolumeEventType, actor)
    80  }
    81  
    82  // LogNetworkEvent generates an event related to a network with only the default attributes.
    83  func (daemon *Daemon) LogNetworkEvent(nw libnetwork.Network, action string) {
    84  	daemon.LogNetworkEventWithAttributes(nw, action, map[string]string{})
    85  }
    86  
    87  // LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
    88  func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) {
    89  	attributes["name"] = nw.Name()
    90  	attributes["type"] = nw.Type()
    91  	actor := events.Actor{
    92  		ID:         nw.ID(),
    93  		Attributes: attributes,
    94  	}
    95  	daemon.EventsService.Log(action, events.NetworkEventType, actor)
    96  }
    97  
    98  // LogDaemonEventWithAttributes generates an event related to the daemon itself with specific given attributes.
    99  func (daemon *Daemon) LogDaemonEventWithAttributes(action string, attributes map[string]string) {
   100  	if daemon.EventsService != nil {
   101  		if info, err := daemon.SystemInfo(); err == nil && info.Name != "" {
   102  			attributes["name"] = info.Name
   103  		}
   104  		actor := events.Actor{
   105  			ID:         daemon.ID,
   106  			Attributes: attributes,
   107  		}
   108  		daemon.EventsService.Log(action, events.DaemonEventType, actor)
   109  	}
   110  }
   111  
   112  // SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
   113  func (daemon *Daemon) SubscribeToEvents(since, until time.Time, filter filters.Args) ([]events.Message, chan interface{}) {
   114  	ef := daemonevents.NewFilter(filter)
   115  	return daemon.EventsService.SubscribeTopic(since, until, ef)
   116  }
   117  
   118  // UnsubscribeFromEvents stops the event subscription for a client by closing the
   119  // channel where the daemon sends events to.
   120  func (daemon *Daemon) UnsubscribeFromEvents(listener chan interface{}) {
   121  	daemon.EventsService.Evict(listener)
   122  }
   123  
   124  // copyAttributes guarantees that labels are not mutated by event triggers.
   125  func copyAttributes(attributes, labels map[string]string) {
   126  	if labels == nil {
   127  		return
   128  	}
   129  	for k, v := range labels {
   130  		attributes[k] = v
   131  	}
   132  }