From 3dea2f230a7e021a6801934c6517c3c2cd42c2e5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Aug 2023 17:24:29 +0200 Subject: [PATCH 1/6] daemon: inline some vars when producing events Also moves the clusterEventAction closer to where it's used. Signed-off-by: Sebastiaan van Stijn --- daemon/events.go | 55 ++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/daemon/events.go b/daemon/events.go index 56f4a8aad7..a3b73344f5 100644 --- a/daemon/events.go +++ b/daemon/events.go @@ -16,12 +16,6 @@ import ( swarmapi "github.com/moby/swarmkit/v2/api" ) -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{}) @@ -34,12 +28,10 @@ func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Conta attributes["image"] = container.Config.Image } attributes["name"] = strings.TrimLeft(container.Name, "/") - - actor := events.Actor{ + daemon.EventsService.Log(action, events.ContainerEventType, 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. @@ -50,20 +42,18 @@ func (daemon *Daemon) LogPluginEvent(pluginID, refName, action 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{ + daemon.EventsService.Log(action, events.PluginEventType, 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{ + daemon.EventsService.Log(action, events.VolumeEventType, 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. @@ -75,11 +65,10 @@ func (daemon *Daemon) LogNetworkEvent(nw *libnetwork.Network, action string) { func (daemon *Daemon) LogNetworkEventWithAttributes(nw *libnetwork.Network, action string, attributes map[string]string) { attributes["name"] = nw.Name() attributes["type"] = nw.Type() - actor := events.Actor{ + daemon.EventsService.Log(action, events.NetworkEventType, 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. @@ -97,8 +86,7 @@ func (daemon *Daemon) LogDaemonEventWithAttributes(action string, attributes map // 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) + return daemon.EventsService.SubscribeTopic(since, until, daemonevents.NewFilter(filter)) } // UnsubscribeFromEvents stops the event subscription for a client by closing the @@ -272,21 +260,24 @@ func (daemon *Daemon) logServiceEvent(action swarmapi.WatchActionKind, service * 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, - } +var clusterEventAction = map[swarmapi.WatchActionKind]string{ + swarmapi.WatchActionKindCreate: "create", + swarmapi.WatchActionKindUpdate: "update", + swarmapi.WatchActionKindRemove: "remove", +} - jm := events.Message{ - Action: clusterEventAction[action], - Type: eventType, - Actor: actor, +func (daemon *Daemon) logClusterEvent(action swarmapi.WatchActionKind, id, eventType string, attributes map[string]string, eventTime time.Time) { + daemon.EventsService.PublishMessage(events.Message{ + Action: clusterEventAction[action], + Type: eventType, + Actor: events.Actor{ + ID: id, + Attributes: attributes, + }, Scope: "swarm", Time: eventTime.UTC().Unix(), TimeNano: eventTime.UTC().UnixNano(), - } - daemon.EventsService.PublishMessage(jm) + }) } func eventTimestamp(meta swarmapi.Meta, action swarmapi.WatchActionKind) time.Time { From 8f64e2e92508edd3d56c7890a6171e86884e228f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Aug 2023 17:36:43 +0200 Subject: [PATCH 2/6] daemon: daemon.logClusterEvent: use events.Type for event-types Also swapping the order of arguments; putting the "attributes" arguments last, so that variables can be more cleanly inlined. Signed-off-by: Sebastiaan van Stijn --- daemon/events.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/daemon/events.go b/daemon/events.go index a3b73344f5..95eeba4290 100644 --- a/daemon/events.go +++ b/daemon/events.go @@ -145,27 +145,21 @@ func (daemon *Daemon) generateClusterEvent(msg *swarmapi.WatchMessage) { } func (daemon *Daemon) logNetworkEvent(action swarmapi.WatchActionKind, net *swarmapi.Network, oldNet *swarmapi.Network) { - attributes := map[string]string{ + daemon.logClusterEvent(action, net.ID, events.NetworkEventType, eventTimestamp(net.Meta, action), 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{ + daemon.logClusterEvent(action, secret.ID, events.SecretEventType, eventTimestamp(secret.Meta, action), 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{ + daemon.logClusterEvent(action, config.ID, events.ConfigEventType, eventTimestamp(config.Meta, action), 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) { @@ -210,7 +204,7 @@ func (daemon *Daemon) logNodeEvent(action swarmapi.WatchActionKind, node *swarma } } - daemon.logClusterEvent(action, node.ID, "node", attributes, eventTime) + daemon.logClusterEvent(action, node.ID, events.NodeEventType, eventTime, attributes) } func (daemon *Daemon) logServiceEvent(action swarmapi.WatchActionKind, service *swarmapi.Service, oldService *swarmapi.Service) { @@ -257,7 +251,7 @@ func (daemon *Daemon) logServiceEvent(action swarmapi.WatchActionKind, service * } } } - daemon.logClusterEvent(action, service.ID, "service", attributes, eventTime) + daemon.logClusterEvent(action, service.ID, events.ServiceEventType, eventTime, attributes) } var clusterEventAction = map[swarmapi.WatchActionKind]string{ @@ -266,7 +260,7 @@ var clusterEventAction = map[swarmapi.WatchActionKind]string{ swarmapi.WatchActionKindRemove: "remove", } -func (daemon *Daemon) logClusterEvent(action swarmapi.WatchActionKind, id, eventType string, attributes map[string]string, eventTime time.Time) { +func (daemon *Daemon) logClusterEvent(action swarmapi.WatchActionKind, id string, eventType events.Type, eventTime time.Time, attributes map[string]string) { daemon.EventsService.PublishMessage(events.Message{ Action: clusterEventAction[action], Type: eventType, From aa764e6009071f7e3ed3fbfe02d0b2a04cbab2a4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Aug 2023 17:39:04 +0200 Subject: [PATCH 3/6] daemon: logNetworkEvent, logSecretEvent, logConfigEvent rm unused args Signed-off-by: Sebastiaan van Stijn --- daemon/events.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/daemon/events.go b/daemon/events.go index 95eeba4290..4fda6d1ab6 100644 --- a/daemon/events.go +++ b/daemon/events.go @@ -133,30 +133,30 @@ func (daemon *Daemon) generateClusterEvent(msg *swarmapi.WatchMessage) { 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()) + daemon.logNetworkEvent(event.Action, v.Network) case *swarmapi.Object_Secret: - daemon.logSecretEvent(event.Action, v.Secret, event.OldObject.GetSecret()) + daemon.logSecretEvent(event.Action, v.Secret) case *swarmapi.Object_Config: - daemon.logConfigEvent(event.Action, v.Config, event.OldObject.GetConfig()) + daemon.logConfigEvent(event.Action, v.Config) default: log.G(context.TODO()).Warnf("unrecognized event: %v", event) } } } -func (daemon *Daemon) logNetworkEvent(action swarmapi.WatchActionKind, net *swarmapi.Network, oldNet *swarmapi.Network) { +func (daemon *Daemon) logNetworkEvent(action swarmapi.WatchActionKind, net *swarmapi.Network) { daemon.logClusterEvent(action, net.ID, events.NetworkEventType, eventTimestamp(net.Meta, action), map[string]string{ "name": net.Spec.Annotations.Name, }) } -func (daemon *Daemon) logSecretEvent(action swarmapi.WatchActionKind, secret *swarmapi.Secret, oldSecret *swarmapi.Secret) { +func (daemon *Daemon) logSecretEvent(action swarmapi.WatchActionKind, secret *swarmapi.Secret) { daemon.logClusterEvent(action, secret.ID, events.SecretEventType, eventTimestamp(secret.Meta, action), map[string]string{ "name": secret.Spec.Annotations.Name, }) } -func (daemon *Daemon) logConfigEvent(action swarmapi.WatchActionKind, config *swarmapi.Config, oldConfig *swarmapi.Config) { +func (daemon *Daemon) logConfigEvent(action swarmapi.WatchActionKind, config *swarmapi.Config) { daemon.logClusterEvent(action, config.ID, events.ConfigEventType, eventTimestamp(config.Meta, action), map[string]string{ "name": config.Spec.Annotations.Name, }) From ebe2347ac6a4a4f7d5958d421b5d3efd12be11e5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Aug 2023 17:41:54 +0200 Subject: [PATCH 4/6] daemon: remove LogPluginEventWithAttributes as it's not used Signed-off-by: Sebastiaan van Stijn --- daemon/events.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/daemon/events.go b/daemon/events.go index 4fda6d1ab6..1801d5b23a 100644 --- a/daemon/events.go +++ b/daemon/events.go @@ -36,15 +36,9 @@ func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Conta // 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 daemon.EventsService.Log(action, events.PluginEventType, events.Actor{ ID: pluginID, - Attributes: attributes, + Attributes: map[string]string{"name": refName}, }) } From 9ea50365d620e57a2157f49ebb0ddf04df72b58c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Aug 2023 22:23:29 +0200 Subject: [PATCH 5/6] daemon/events: use events-consts in tests, and fix vars that collided Signed-off-by: Sebastiaan van Stijn --- daemon/events/events_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/daemon/events/events_test.go b/daemon/events/events_test.go index 6a193fc084..98a30b7ff4 100644 --- a/daemon/events/events_test.go +++ b/daemon/events/events_test.go @@ -190,14 +190,14 @@ func TestLoadBufferedEvents(t *testing.T) { t.Fatal(err) } - events := &Events{ + evts := &Events{ events: []events.Message{*m1, *m2, *m3}, } since := time.Unix(s, sNano) until := time.Time{} - out := events.loadBufferedEvents(since, until, nil) + out := evts.loadBufferedEvents(since, until, nil) if len(out) != 1 { t.Fatalf("expected 1 message, got %d: %v", len(out), out) } @@ -236,19 +236,19 @@ func TestLoadBufferedEventsOnlyFromPast(t *testing.T) { t.Fatal(err) } - events := &Events{ + evts := &Events{ events: []events.Message{*m1, *m2, *m3}, } since := time.Unix(s, sNano) until := time.Unix(u, uNano) - out := events.loadBufferedEvents(since, until, nil) + out := evts.loadBufferedEvents(since, until, nil) if len(out) != 1 { t.Fatalf("expected 1 message, got %d: %v", len(out), out) } - if out[0].Type != "network" { + if out[0].Type != events.NetworkEventType { t.Fatalf("expected network event, got %s", out[0].Type) } } @@ -268,14 +268,14 @@ func TestIgnoreBufferedWhenNoTimes(t *testing.T) { t.Fatal(err) } - events := &Events{ + evts := &Events{ events: []events.Message{*m1, *m2, *m3}, } since := time.Time{} until := time.Time{} - out := events.loadBufferedEvents(since, until, nil) + out := evts.loadBufferedEvents(since, until, nil) if len(out) != 0 { t.Fatalf("expected 0 buffered events, got %q", out) } From 5a02ed5e84b256239dd7af76d87d3f0b8443a724 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Aug 2023 22:24:29 +0200 Subject: [PATCH 6/6] integration: use events-consts in tests Signed-off-by: Sebastiaan van Stijn --- integration/container/pause_test.go | 2 +- integration/system/event_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/container/pause_test.go b/integration/container/pause_test.go index 51785c4d98..054e394257 100644 --- a/integration/container/pause_test.go +++ b/integration/container/pause_test.go @@ -48,7 +48,7 @@ func TestPause(t *testing.T) { messages, errs := apiClient.Events(ctx, types.EventsOptions{ Since: since, Until: until, - Filters: filters.NewArgs(filters.Arg("container", cID)), + Filters: filters.NewArgs(filters.Arg(events.ContainerEventType, cID)), }) assert.Check(t, is.DeepEqual([]string{"pause", "unpause"}, getEventActions(t, messages, errs))) } diff --git a/integration/system/event_test.go b/integration/system/event_test.go index 6a40456e52..3b6ff67d48 100644 --- a/integration/system/event_test.go +++ b/integration/system/event_test.go @@ -60,7 +60,7 @@ func TestEventsExecDie(t *testing.T) { select { case m := <-msg: - assert.Equal(t, m.Type, "container") + assert.Equal(t, m.Type, events.ContainerEventType) assert.Equal(t, m.Actor.ID, cID) assert.Equal(t, m.Action, "exec_die") assert.Equal(t, m.Actor.Attributes["execID"], id.ID)