diff --git a/daemon/cluster/executor/container/health_test.go b/daemon/cluster/executor/container/health_test.go index da84a7e9cf..6d0eb27d6c 100644 --- a/daemon/cluster/executor/container/health_test.go +++ b/daemon/cluster/executor/container/health_test.go @@ -37,22 +37,11 @@ func TestHealthStates(t *testing.T) { Annotations: api.Annotations{Name: "name"}, } - c := &container.Container{ - ID: "id", - Name: "name", - Config: &containertypes.Config{ - Image: "image_name", - Labels: map[string]string{ - "com.docker.swarm.task.id": "id", - }, - }, - } - daemon := &daemon.Daemon{ EventsService: e, } - controller, err := newController(daemon, nil, nil, task, nil, nil) + ctrlr, err := newController(daemon, nil, nil, task, nil, nil) if err != nil { t.Fatalf("create controller fail %v", err) } @@ -62,7 +51,7 @@ func TestHealthStates(t *testing.T) { // fire checkHealth go func() { - err := controller.checkHealth(ctx) + err := ctrlr.checkHealth(ctx) select { case errChan <- err: case <-ctx.Done(): @@ -72,7 +61,17 @@ func TestHealthStates(t *testing.T) { // send an event and expect to get expectedErr // if expectedErr is nil, shouldn't get any error logAndExpect := func(msg eventtypes.Action, expectedErr error) { - daemon.LogContainerEvent(c, msg) + ctr := &container.Container{ + ID: "id", + Name: "name", + Config: &containertypes.Config{ + Image: "image_name", + Labels: map[string]string{ + "com.docker.swarm.task.id": "id", + }, + }, + } + daemon.LogContainerEvent(ctr, msg) timer := time.NewTimer(1 * time.Second) defer timer.Stop() diff --git a/daemon/events.go b/daemon/events.go index dc8f81d879..40e19fb8c9 100644 --- a/daemon/events.go +++ b/daemon/events.go @@ -24,7 +24,9 @@ func (daemon *Daemon) LogContainerEvent(container *container.Container, action e // LogContainerEventWithAttributes generates an event related to a container with specific given attributes. func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action events.Action, attributes map[string]string) { - copyAttributes(attributes, container.Config.Labels) + if container.Config.Labels != nil { + maps.Copy(attributes, container.Config.Labels) + } if container.Config.Image != "" { attributes["image"] = container.Config.Image } @@ -90,14 +92,6 @@ func (daemon *Daemon) UnsubscribeFromEvents(listener chan any) { 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 - } - maps.Copy(attributes, labels) -} - // ProcessClusterNotifications gets changes from store and add them to event list func (daemon *Daemon) ProcessClusterNotifications(ctx context.Context, watchStream chan *swarmapi.WatchMessage) { for {