github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/daemon/events.go (about)

     1  package daemon
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/container"
     7  	"github.com/docker/engine-api/types/events"
     8  	"github.com/docker/libnetwork"
     9  )
    10  
    11  // LogContainerEvent generates an event related to a container with only the default attributes.
    12  func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) {
    13  	daemon.LogContainerEventWithAttributes(container, action, map[string]string{})
    14  }
    15  
    16  // LogContainerEventWithAttributes generates an event related to a container with specific given attributes.
    17  func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action string, attributes map[string]string) {
    18  	copyAttributes(attributes, container.Config.Labels)
    19  	if container.Config.Image != "" {
    20  		attributes["image"] = container.Config.Image
    21  	}
    22  	attributes["name"] = strings.TrimLeft(container.Name, "/")
    23  
    24  	actor := events.Actor{
    25  		ID:         container.ID,
    26  		Attributes: attributes,
    27  	}
    28  	daemon.EventsService.Log(action, events.ContainerEventType, actor)
    29  }
    30  
    31  // LogImageEvent generates an event related to a container with only the default attributes.
    32  func (daemon *Daemon) LogImageEvent(imageID, refName, action string) {
    33  	daemon.LogImageEventWithAttributes(imageID, refName, action, map[string]string{})
    34  }
    35  
    36  // LogImageEventWithAttributes generates an event related to a container with specific given attributes.
    37  func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
    38  	img, err := daemon.GetImage(imageID)
    39  	if err == nil && img.Config != nil {
    40  		// image has not been removed yet.
    41  		// it could be missing if the event is `delete`.
    42  		copyAttributes(attributes, img.Config.Labels)
    43  	}
    44  	if refName != "" {
    45  		attributes["name"] = refName
    46  	}
    47  	actor := events.Actor{
    48  		ID:         imageID,
    49  		Attributes: attributes,
    50  	}
    51  
    52  	daemon.EventsService.Log(action, events.ImageEventType, actor)
    53  }
    54  
    55  // LogVolumeEvent generates an event related to a volume.
    56  func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
    57  	actor := events.Actor{
    58  		ID:         volumeID,
    59  		Attributes: attributes,
    60  	}
    61  	daemon.EventsService.Log(action, events.VolumeEventType, actor)
    62  }
    63  
    64  // LogNetworkEvent generates an event related to a network with only the default attributes.
    65  func (daemon *Daemon) LogNetworkEvent(nw libnetwork.Network, action string) {
    66  	daemon.LogNetworkEventWithAttributes(nw, action, map[string]string{})
    67  }
    68  
    69  // LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
    70  func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) {
    71  	attributes["name"] = nw.Name()
    72  	attributes["type"] = nw.Type()
    73  	actor := events.Actor{
    74  		ID:         nw.ID(),
    75  		Attributes: attributes,
    76  	}
    77  	daemon.EventsService.Log(action, events.NetworkEventType, actor)
    78  }
    79  
    80  // copyAttributes guarantees that labels are not mutated by event triggers.
    81  func copyAttributes(attributes, labels map[string]string) {
    82  	if labels == nil {
    83  		return
    84  	}
    85  	for k, v := range labels {
    86  		attributes[k] = v
    87  	}
    88  }