2018-02-05 21:05:59 +00:00
package daemon // import "github.com/docker/docker/daemon"
2015-11-02 23:25:26 +00:00
2015-11-12 19:55:17 +00:00
import (
2017-04-02 22:21:56 +00:00
"context"
"strconv"
2015-12-21 22:55:23 +00:00
"strings"
2016-04-11 18:52:34 +00:00
"time"
2015-12-21 22:55:23 +00:00
2023-09-13 15:41:45 +00:00
"github.com/containerd/log"
2016-09-06 18:18:12 +00:00
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
2015-11-12 19:55:17 +00:00
"github.com/docker/docker/container"
2016-04-11 18:52:34 +00:00
daemonevents "github.com/docker/docker/daemon/events"
2021-04-06 00:24:47 +00:00
"github.com/docker/docker/libnetwork"
2017-04-02 22:21:56 +00:00
gogotypes "github.com/gogo/protobuf/types"
2022-04-21 21:33:07 +00:00
swarmapi "github.com/moby/swarmkit/v2/api"
2017-04-02 22:21:56 +00:00
)
2016-01-07 22:14:05 +00:00
// LogContainerEvent generates an event related to a container with only the default attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogContainerEvent ( container * container . Container , action events . Action ) {
2016-01-07 22:14:05 +00:00
daemon . LogContainerEventWithAttributes ( container , action , map [ string ] string { } )
}
// LogContainerEventWithAttributes generates an event related to a container with specific given attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogContainerEventWithAttributes ( container * container . Container , action events . Action , attributes map [ string ] string ) {
2016-01-07 22:14:05 +00:00
copyAttributes ( attributes , container . Config . Labels )
2015-12-21 22:55:23 +00:00
if container . Config . Image != "" {
attributes [ "image" ] = container . Config . Image
}
attributes [ "name" ] = strings . TrimLeft ( container . Name , "/" )
2023-08-26 15:24:29 +00:00
daemon . EventsService . Log ( action , events . ContainerEventType , events . Actor {
2015-12-21 22:55:23 +00:00
ID : container . ID ,
Attributes : attributes ,
2023-08-26 15:24:29 +00:00
} )
2015-12-21 22:55:23 +00:00
}
2016-07-18 15:02:12 +00:00
// LogPluginEvent generates an event related to a plugin with only the default attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogPluginEvent ( pluginID , refName string , action events . Action ) {
2023-08-26 15:24:29 +00:00
daemon . EventsService . Log ( action , events . PluginEventType , events . Actor {
2016-07-18 15:02:12 +00:00
ID : pluginID ,
2023-08-26 15:41:54 +00:00
Attributes : map [ string ] string { "name" : refName } ,
2023-08-26 15:24:29 +00:00
} )
2016-07-18 15:02:12 +00:00
}
2015-12-21 22:55:23 +00:00
// LogVolumeEvent generates an event related to a volume.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogVolumeEvent ( volumeID string , action events . Action , attributes map [ string ] string ) {
2023-08-26 15:24:29 +00:00
daemon . EventsService . Log ( action , events . VolumeEventType , events . Actor {
2015-12-21 22:55:23 +00:00
ID : volumeID ,
Attributes : attributes ,
2023-08-26 15:24:29 +00:00
} )
2015-12-21 22:55:23 +00:00
}
// LogNetworkEvent generates an event related to a network with only the default attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogNetworkEvent ( nw * libnetwork . Network , action events . Action ) {
2015-12-21 22:55:23 +00:00
daemon . LogNetworkEventWithAttributes ( nw , action , map [ string ] string { } )
}
// LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogNetworkEventWithAttributes ( nw * libnetwork . Network , action events . Action , attributes map [ string ] string ) {
2015-12-21 22:55:23 +00:00
attributes [ "name" ] = nw . Name ( )
attributes [ "type" ] = nw . Type ( )
2023-08-26 15:24:29 +00:00
daemon . EventsService . Log ( action , events . NetworkEventType , events . Actor {
2015-12-22 04:35:30 +00:00
ID : nw . ID ( ) ,
2015-12-21 22:55:23 +00:00
Attributes : attributes ,
2023-08-26 15:24:29 +00:00
} )
2015-12-21 22:55:23 +00:00
}
2016-05-08 23:11:34 +00:00
// LogDaemonEventWithAttributes generates an event related to the daemon itself with specific given attributes.
2023-08-26 13:24:46 +00:00
func ( daemon * Daemon ) LogDaemonEventWithAttributes ( action events . Action , attributes map [ string ] string ) {
2016-05-08 23:11:34 +00:00
if daemon . EventsService != nil {
2023-09-10 00:05:05 +00:00
if name := hostName ( context . TODO ( ) ) ; name != "" {
2022-06-07 00:47:48 +00:00
attributes [ "name" ] = name
2016-05-08 23:15:33 +00:00
}
2022-06-07 00:47:48 +00:00
daemon . EventsService . Log ( action , events . DaemonEventType , events . Actor {
2022-03-02 10:43:33 +00:00
ID : daemon . id ,
2016-05-08 23:11:34 +00:00
Attributes : attributes ,
2022-06-07 00:47:48 +00:00
} )
2016-05-08 23:11:34 +00:00
}
}
2016-04-11 18:52:34 +00:00
// 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 { } ) {
2023-08-26 15:24:29 +00:00
return daemon . EventsService . SubscribeTopic ( since , until , daemonevents . NewFilter ( filter ) )
2016-04-11 18:52:34 +00:00
}
// 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 )
}
2015-12-21 22:55:23 +00:00
// copyAttributes guarantees that labels are not mutated by event triggers.
2016-01-07 22:14:05 +00:00
func copyAttributes ( attributes , labels map [ string ] string ) {
2015-12-21 22:55:23 +00:00
if labels == nil {
2016-01-07 22:14:05 +00:00
return
2015-12-21 22:55:23 +00:00
}
for k , v := range labels {
attributes [ k ] = v
}
2015-11-02 23:25:26 +00:00
}
2017-04-02 22:21:56 +00:00
// 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 {
2023-06-23 00:33:17 +00:00
log . G ( ctx ) . Debug ( "cluster event channel has stopped" )
2017-04-02 22:21:56 +00:00
return
}
daemon . generateClusterEvent ( message )
}
}
}
func ( daemon * Daemon ) generateClusterEvent ( msg * swarmapi . WatchMessage ) {
for _ , event := range msg . Events {
if event . Object == nil {
2023-06-23 00:33:17 +00:00
log . G ( context . TODO ( ) ) . Errorf ( "event without object: %v" , event )
2017-04-02 22:21:56 +00:00
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 :
2023-08-26 15:39:04 +00:00
daemon . logNetworkEvent ( event . Action , v . Network )
2017-04-02 22:21:56 +00:00
case * swarmapi . Object_Secret :
2023-08-26 15:39:04 +00:00
daemon . logSecretEvent ( event . Action , v . Secret )
2017-07-10 03:41:25 +00:00
case * swarmapi . Object_Config :
2023-08-26 15:39:04 +00:00
daemon . logConfigEvent ( event . Action , v . Config )
2017-04-02 22:21:56 +00:00
default :
2023-06-23 00:33:17 +00:00
log . G ( context . TODO ( ) ) . Warnf ( "unrecognized event: %v" , event )
2017-04-02 22:21:56 +00:00
}
}
}
2023-08-26 15:39:04 +00:00
func ( daemon * Daemon ) logNetworkEvent ( action swarmapi . WatchActionKind , net * swarmapi . Network ) {
2023-08-26 15:36:43 +00:00
daemon . logClusterEvent ( action , net . ID , events . NetworkEventType , eventTimestamp ( net . Meta , action ) , map [ string ] string {
2017-04-02 22:21:56 +00:00
"name" : net . Spec . Annotations . Name ,
2023-08-26 15:36:43 +00:00
} )
2017-04-02 22:21:56 +00:00
}
2023-08-26 15:39:04 +00:00
func ( daemon * Daemon ) logSecretEvent ( action swarmapi . WatchActionKind , secret * swarmapi . Secret ) {
2023-08-26 15:36:43 +00:00
daemon . logClusterEvent ( action , secret . ID , events . SecretEventType , eventTimestamp ( secret . Meta , action ) , map [ string ] string {
2017-04-02 22:21:56 +00:00
"name" : secret . Spec . Annotations . Name ,
2023-08-26 15:36:43 +00:00
} )
2017-04-02 22:21:56 +00:00
}
2023-08-26 15:39:04 +00:00
func ( daemon * Daemon ) logConfigEvent ( action swarmapi . WatchActionKind , config * swarmapi . Config ) {
2023-08-26 15:36:43 +00:00
daemon . logClusterEvent ( action , config . ID , events . ConfigEventType , eventTimestamp ( config . Meta , action ) , map [ string ] string {
2017-07-10 03:41:25 +00:00
"name" : config . Spec . Annotations . Name ,
2023-08-26 15:36:43 +00:00
} )
2017-07-10 03:41:25 +00:00
}
2017-04-02 22:21:56 +00:00
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 ( ) )
}
}
}
2023-08-26 15:36:43 +00:00
daemon . logClusterEvent ( action , node . ID , events . NodeEventType , eventTime , attributes )
2017-04-02 22:21:56 +00:00
}
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.
2023-06-23 00:33:17 +00:00
log . G ( context . TODO ( ) ) . Errorf ( "service %s runtime changed from %T to %T" , service . Spec . Annotations . Name , oldService . Spec . Task . GetRuntime ( ) , service . Spec . Task . GetRuntime ( ) )
2017-04-02 22:21:56 +00:00
}
}
// 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.
2023-06-23 00:33:17 +00:00
log . G ( context . TODO ( ) ) . Errorf ( "service %s mode changed from %T to %T" , service . Spec . Annotations . Name , oldService . Spec . GetMode ( ) , service . Spec . GetMode ( ) )
2017-04-02 22:21:56 +00:00
}
}
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 ( ) )
}
}
}
2023-08-26 15:36:43 +00:00
daemon . logClusterEvent ( action , service . ID , events . ServiceEventType , eventTime , attributes )
2017-04-02 22:21:56 +00:00
}
2023-08-26 13:24:46 +00:00
var clusterEventAction = map [ swarmapi . WatchActionKind ] events . Action {
swarmapi . WatchActionKindCreate : events . ActionCreate ,
swarmapi . WatchActionKindUpdate : events . ActionUpdate ,
swarmapi . WatchActionKindRemove : events . ActionRemove ,
2023-08-26 15:24:29 +00:00
}
2017-04-02 22:21:56 +00:00
2023-08-26 15:36:43 +00:00
func ( daemon * Daemon ) logClusterEvent ( action swarmapi . WatchActionKind , id string , eventType events . Type , eventTime time . Time , attributes map [ string ] string ) {
2023-08-26 15:24:29 +00:00
daemon . EventsService . PublishMessage ( events . Message {
Action : clusterEventAction [ action ] ,
Type : eventType ,
Actor : events . Actor {
ID : id ,
Attributes : attributes ,
} ,
2017-04-02 22:21:56 +00:00
Scope : "swarm" ,
Time : eventTime . UTC ( ) . Unix ( ) ,
TimeNano : eventTime . UTC ( ) . UnixNano ( ) ,
2023-08-26 15:24:29 +00:00
} )
2017-04-02 22:21:56 +00:00
}
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
}