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