events.go 4.9 KB

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