events.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package daemon
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "time"
  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. swarmapi "github.com/docker/swarmkit/api"
  13. gogotypes "github.com/gogo/protobuf/types"
  14. "github.com/sirupsen/logrus"
  15. )
  16. var (
  17. clusterEventAction = map[swarmapi.WatchActionKind]string{
  18. swarmapi.WatchActionKindCreate: "create",
  19. swarmapi.WatchActionKindUpdate: "update",
  20. swarmapi.WatchActionKindRemove: "remove",
  21. }
  22. )
  23. // LogContainerEvent generates an event related to a container with only the default attributes.
  24. func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) {
  25. daemon.LogContainerEventWithAttributes(container, action, map[string]string{})
  26. }
  27. // LogContainerEventWithAttributes generates an event related to a container with specific given attributes.
  28. func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action string, attributes map[string]string) {
  29. copyAttributes(attributes, container.Config.Labels)
  30. if container.Config.Image != "" {
  31. attributes["image"] = container.Config.Image
  32. }
  33. attributes["name"] = strings.TrimLeft(container.Name, "/")
  34. actor := events.Actor{
  35. ID: container.ID,
  36. Attributes: attributes,
  37. }
  38. daemon.EventsService.Log(action, events.ContainerEventType, actor)
  39. }
  40. // LogImageEvent generates an event related to an image with only the default attributes.
  41. func (daemon *Daemon) LogImageEvent(imageID, refName, action string) {
  42. daemon.LogImageEventWithAttributes(imageID, refName, action, map[string]string{})
  43. }
  44. // LogImageEventWithAttributes generates an event related to an image with specific given attributes.
  45. func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
  46. img, err := daemon.GetImage(imageID)
  47. if err == nil && img.Config != nil {
  48. // image has not been removed yet.
  49. // it could be missing if the event is `delete`.
  50. copyAttributes(attributes, img.Config.Labels)
  51. }
  52. if refName != "" {
  53. attributes["name"] = refName
  54. }
  55. actor := events.Actor{
  56. ID: imageID,
  57. Attributes: attributes,
  58. }
  59. daemon.EventsService.Log(action, events.ImageEventType, actor)
  60. }
  61. // LogPluginEvent generates an event related to a plugin with only the default attributes.
  62. func (daemon *Daemon) LogPluginEvent(pluginID, refName, action string) {
  63. daemon.LogPluginEventWithAttributes(pluginID, refName, action, map[string]string{})
  64. }
  65. // LogPluginEventWithAttributes generates an event related to a plugin with specific given attributes.
  66. func (daemon *Daemon) LogPluginEventWithAttributes(pluginID, refName, action string, attributes map[string]string) {
  67. attributes["name"] = refName
  68. actor := events.Actor{
  69. ID: pluginID,
  70. Attributes: attributes,
  71. }
  72. daemon.EventsService.Log(action, events.PluginEventType, actor)
  73. }
  74. // LogVolumeEvent generates an event related to a volume.
  75. func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
  76. actor := events.Actor{
  77. ID: volumeID,
  78. Attributes: attributes,
  79. }
  80. daemon.EventsService.Log(action, events.VolumeEventType, actor)
  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. // LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
  87. func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) {
  88. attributes["name"] = nw.Name()
  89. attributes["type"] = nw.Type()
  90. actor := events.Actor{
  91. ID: nw.ID(),
  92. Attributes: attributes,
  93. }
  94. daemon.EventsService.Log(action, events.NetworkEventType, actor)
  95. }
  96. // LogDaemonEventWithAttributes generates an event related to the daemon itself with specific given attributes.
  97. func (daemon *Daemon) LogDaemonEventWithAttributes(action string, attributes map[string]string) {
  98. if daemon.EventsService != nil {
  99. if info, err := daemon.SystemInfo(); err == nil && info.Name != "" {
  100. attributes["name"] = info.Name
  101. }
  102. actor := events.Actor{
  103. ID: daemon.ID,
  104. Attributes: attributes,
  105. }
  106. daemon.EventsService.Log(action, events.DaemonEventType, actor)
  107. }
  108. }
  109. // SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
  110. func (daemon *Daemon) SubscribeToEvents(since, until time.Time, filter filters.Args) ([]events.Message, chan interface{}) {
  111. ef := daemonevents.NewFilter(filter)
  112. return daemon.EventsService.SubscribeTopic(since, until, ef)
  113. }
  114. // UnsubscribeFromEvents stops the event subscription for a client by closing the
  115. // channel where the daemon sends events to.
  116. func (daemon *Daemon) UnsubscribeFromEvents(listener chan interface{}) {
  117. daemon.EventsService.Evict(listener)
  118. }
  119. // copyAttributes guarantees that labels are not mutated by event triggers.
  120. func copyAttributes(attributes, labels map[string]string) {
  121. if labels == nil {
  122. return
  123. }
  124. for k, v := range labels {
  125. attributes[k] = v
  126. }
  127. }
  128. // ProcessClusterNotifications gets changes from store and add them to event list
  129. func (daemon *Daemon) ProcessClusterNotifications(ctx context.Context, watchStream chan *swarmapi.WatchMessage) {
  130. for {
  131. select {
  132. case <-ctx.Done():
  133. return
  134. case message, ok := <-watchStream:
  135. if !ok {
  136. logrus.Debug("cluster event channel has stopped")
  137. return
  138. }
  139. daemon.generateClusterEvent(message)
  140. }
  141. }
  142. }
  143. func (daemon *Daemon) generateClusterEvent(msg *swarmapi.WatchMessage) {
  144. for _, event := range msg.Events {
  145. if event.Object == nil {
  146. logrus.Errorf("event without object: %v", event)
  147. continue
  148. }
  149. switch v := event.Object.GetObject().(type) {
  150. case *swarmapi.Object_Node:
  151. daemon.logNodeEvent(event.Action, v.Node, event.OldObject.GetNode())
  152. case *swarmapi.Object_Service:
  153. daemon.logServiceEvent(event.Action, v.Service, event.OldObject.GetService())
  154. case *swarmapi.Object_Network:
  155. daemon.logNetworkEvent(event.Action, v.Network, event.OldObject.GetNetwork())
  156. case *swarmapi.Object_Secret:
  157. daemon.logSecretEvent(event.Action, v.Secret, event.OldObject.GetSecret())
  158. case *swarmapi.Object_Config:
  159. daemon.logConfigEvent(event.Action, v.Config, event.OldObject.GetConfig())
  160. default:
  161. logrus.Warnf("unrecognized event: %v", event)
  162. }
  163. }
  164. }
  165. func (daemon *Daemon) logNetworkEvent(action swarmapi.WatchActionKind, net *swarmapi.Network, oldNet *swarmapi.Network) {
  166. attributes := map[string]string{
  167. "name": net.Spec.Annotations.Name,
  168. }
  169. eventTime := eventTimestamp(net.Meta, action)
  170. daemon.logClusterEvent(action, net.ID, "network", attributes, eventTime)
  171. }
  172. func (daemon *Daemon) logSecretEvent(action swarmapi.WatchActionKind, secret *swarmapi.Secret, oldSecret *swarmapi.Secret) {
  173. attributes := map[string]string{
  174. "name": secret.Spec.Annotations.Name,
  175. }
  176. eventTime := eventTimestamp(secret.Meta, action)
  177. daemon.logClusterEvent(action, secret.ID, "secret", attributes, eventTime)
  178. }
  179. func (daemon *Daemon) logConfigEvent(action swarmapi.WatchActionKind, config *swarmapi.Config, oldConfig *swarmapi.Config) {
  180. attributes := map[string]string{
  181. "name": config.Spec.Annotations.Name,
  182. }
  183. eventTime := eventTimestamp(config.Meta, action)
  184. daemon.logClusterEvent(action, config.ID, "config", attributes, eventTime)
  185. }
  186. func (daemon *Daemon) logNodeEvent(action swarmapi.WatchActionKind, node *swarmapi.Node, oldNode *swarmapi.Node) {
  187. name := node.Spec.Annotations.Name
  188. if name == "" && node.Description != nil {
  189. name = node.Description.Hostname
  190. }
  191. attributes := map[string]string{
  192. "name": name,
  193. }
  194. eventTime := eventTimestamp(node.Meta, action)
  195. // In an update event, display the changes in attributes
  196. if action == swarmapi.WatchActionKindUpdate && oldNode != nil {
  197. if node.Spec.Availability != oldNode.Spec.Availability {
  198. attributes["availability.old"] = strings.ToLower(oldNode.Spec.Availability.String())
  199. attributes["availability.new"] = strings.ToLower(node.Spec.Availability.String())
  200. }
  201. if node.Role != oldNode.Role {
  202. attributes["role.old"] = strings.ToLower(oldNode.Role.String())
  203. attributes["role.new"] = strings.ToLower(node.Role.String())
  204. }
  205. if node.Status.State != oldNode.Status.State {
  206. attributes["state.old"] = strings.ToLower(oldNode.Status.State.String())
  207. attributes["state.new"] = strings.ToLower(node.Status.State.String())
  208. }
  209. // This handles change within manager role
  210. if node.ManagerStatus != nil && oldNode.ManagerStatus != nil {
  211. // leader change
  212. if node.ManagerStatus.Leader != oldNode.ManagerStatus.Leader {
  213. if node.ManagerStatus.Leader {
  214. attributes["leader.old"] = "false"
  215. attributes["leader.new"] = "true"
  216. } else {
  217. attributes["leader.old"] = "true"
  218. attributes["leader.new"] = "false"
  219. }
  220. }
  221. if node.ManagerStatus.Reachability != oldNode.ManagerStatus.Reachability {
  222. attributes["reachability.old"] = strings.ToLower(oldNode.ManagerStatus.Reachability.String())
  223. attributes["reachability.new"] = strings.ToLower(node.ManagerStatus.Reachability.String())
  224. }
  225. }
  226. }
  227. daemon.logClusterEvent(action, node.ID, "node", attributes, eventTime)
  228. }
  229. func (daemon *Daemon) logServiceEvent(action swarmapi.WatchActionKind, service *swarmapi.Service, oldService *swarmapi.Service) {
  230. attributes := map[string]string{
  231. "name": service.Spec.Annotations.Name,
  232. }
  233. eventTime := eventTimestamp(service.Meta, action)
  234. if action == swarmapi.WatchActionKindUpdate && oldService != nil {
  235. // check image
  236. if x, ok := service.Spec.Task.GetRuntime().(*swarmapi.TaskSpec_Container); ok {
  237. containerSpec := x.Container
  238. if y, ok := oldService.Spec.Task.GetRuntime().(*swarmapi.TaskSpec_Container); ok {
  239. oldContainerSpec := y.Container
  240. if containerSpec.Image != oldContainerSpec.Image {
  241. attributes["image.old"] = oldContainerSpec.Image
  242. attributes["image.new"] = containerSpec.Image
  243. }
  244. } else {
  245. // This should not happen.
  246. logrus.Errorf("service %s runtime changed from %T to %T", service.Spec.Annotations.Name, oldService.Spec.Task.GetRuntime(), service.Spec.Task.GetRuntime())
  247. }
  248. }
  249. // check replicated count change
  250. if x, ok := service.Spec.GetMode().(*swarmapi.ServiceSpec_Replicated); ok {
  251. replicas := x.Replicated.Replicas
  252. if y, ok := oldService.Spec.GetMode().(*swarmapi.ServiceSpec_Replicated); ok {
  253. oldReplicas := y.Replicated.Replicas
  254. if replicas != oldReplicas {
  255. attributes["replicas.old"] = strconv.FormatUint(oldReplicas, 10)
  256. attributes["replicas.new"] = strconv.FormatUint(replicas, 10)
  257. }
  258. } else {
  259. // This should not happen.
  260. logrus.Errorf("service %s mode changed from %T to %T", service.Spec.Annotations.Name, oldService.Spec.GetMode(), service.Spec.GetMode())
  261. }
  262. }
  263. if service.UpdateStatus != nil {
  264. if oldService.UpdateStatus == nil {
  265. attributes["updatestate.new"] = strings.ToLower(service.UpdateStatus.State.String())
  266. } else if service.UpdateStatus.State != oldService.UpdateStatus.State {
  267. attributes["updatestate.old"] = strings.ToLower(oldService.UpdateStatus.State.String())
  268. attributes["updatestate.new"] = strings.ToLower(service.UpdateStatus.State.String())
  269. }
  270. }
  271. }
  272. daemon.logClusterEvent(action, service.ID, "service", attributes, eventTime)
  273. }
  274. func (daemon *Daemon) logClusterEvent(action swarmapi.WatchActionKind, id, eventType string, attributes map[string]string, eventTime time.Time) {
  275. actor := events.Actor{
  276. ID: id,
  277. Attributes: attributes,
  278. }
  279. jm := events.Message{
  280. Action: clusterEventAction[action],
  281. Type: eventType,
  282. Actor: actor,
  283. Scope: "swarm",
  284. Time: eventTime.UTC().Unix(),
  285. TimeNano: eventTime.UTC().UnixNano(),
  286. }
  287. daemon.EventsService.PublishMessage(jm)
  288. }
  289. func eventTimestamp(meta swarmapi.Meta, action swarmapi.WatchActionKind) time.Time {
  290. var eventTime time.Time
  291. switch action {
  292. case swarmapi.WatchActionKindCreate:
  293. eventTime, _ = gogotypes.TimestampFromProto(meta.CreatedAt)
  294. case swarmapi.WatchActionKindUpdate:
  295. eventTime, _ = gogotypes.TimestampFromProto(meta.UpdatedAt)
  296. case swarmapi.WatchActionKindRemove:
  297. // There is no timestamp from store message for remove operations.
  298. // Use current time.
  299. eventTime = time.Now()
  300. }
  301. return eventTime
  302. }