diff --git a/assets/models/Container.ts b/assets/models/Container.ts index 642a0bac..ab853e2f 100644 --- a/assets/models/Container.ts +++ b/assets/models/Container.ts @@ -23,9 +23,9 @@ export class GroupedContainers { export class Container { private _stat: Ref; + private _name: string; private readonly _statsHistory: Ref; private readonly movingAverageStat: Ref; - private readonly _name: string; constructor( public readonly id: string, @@ -76,6 +76,10 @@ export class Container { return this.group; } + set name(name: string) { + this._name = name; + } + get name() { return this.isSwarm ? this.labels["com.docker.swarm.task.name"] diff --git a/assets/stores/container.ts b/assets/stores/container.ts index 535d26bf..d53a36e7 100644 --- a/assets/stores/container.ts +++ b/assets/stores/container.ts @@ -124,6 +124,7 @@ export const useContainerStore = defineStore("container", () => { const existing = allContainersById.value[c.id]; existing.state = c.state; existing.health = c.health; + existing.name = c.name; }); containers.value = [ diff --git a/internal/docker/client.go b/internal/docker/client.go index 69e6e7a6..c3e5ff9a 100644 --- a/internal/docker/client.go +++ b/internal/docker/client.go @@ -309,9 +309,10 @@ func (d *httpClient) ContainerEvents(ctx context.Context, messages chan<- Contai case message := <-dockerMessages: if message.Type == events.ContainerEventType && len(message.Actor.ID) > 0 { messages <- ContainerEvent{ - ActorID: message.Actor.ID[:12], - Name: string(message.Action), - Host: d.host.ID, + ActorID: message.Actor.ID[:12], + Name: string(message.Action), + Host: d.host.ID, + ActorAttributes: message.Actor.Attributes, } } } diff --git a/internal/docker/container_store.go b/internal/docker/container_store.go index da07b0fc..cbe933a7 100644 --- a/internal/docker/container_store.go +++ b/internal/docker/container_store.go @@ -230,6 +230,17 @@ func (s *ContainerStore) init() { return c, true } }) + + case "rename": + s.containers.Compute(event.ActorID, func(c *Container, loaded bool) (*Container, bool) { + if loaded { + log.Debug().Str("id", event.ActorID).Str("name", event.ActorAttributes["name"]).Msg("container renamed") + c.Name = event.ActorAttributes["name"] + return c, false + } else { + return c, true + } + }) } s.subscribers.Range(func(c context.Context, events chan<- ContainerEvent) bool { select { diff --git a/internal/docker/types.go b/internal/docker/types.go index a0196718..93374521 100644 --- a/internal/docker/types.go +++ b/internal/docker/types.go @@ -35,9 +35,10 @@ type ContainerStat struct { // ContainerEvent represents events that are triggered type ContainerEvent struct { - ActorID string `json:"actorId"` - Name string `json:"name"` - Host string `json:"host"` + Name string `json:"name"` + Host string `json:"host"` + ActorID string `json:"actorId"` + ActorAttributes map[string]string `json:"actorAttributes,omitempty"` } type LogPosition string diff --git a/internal/web/__snapshots__/web.snapshot b/internal/web/__snapshots__/web.snapshot index 7577634d..6a92e61f 100644 --- a/internal/web/__snapshots__/web.snapshot +++ b/internal/web/__snapshots__/web.snapshot @@ -145,7 +145,7 @@ data: [] event: container-event -data: {"actorId":"1234","name":"start","host":"localhost"} +data: {"name":"start","host":"localhost","actorId":"1234"} /* snapshot: Test_handler_streamLogs_error_finding_container */ HTTP/1.1 404 Not Found @@ -189,7 +189,7 @@ data: {"m":"INFO Testing logs...","ts":0,"id":4256192898,"l":"info","s":"stdout" event: container-event -data: {"actorId":"123456","name":"container-stopped","host":"localhost"} +data: {"name":"container-stopped","host":"localhost","actorId":"123456"} /* snapshot: Test_handler_streamLogs_happy_container_stopped */ HTTP/1.1 200 OK @@ -202,7 +202,7 @@ Content-Type: text/event-stream X-Accel-Buffering: no event: container-event -data: {"actorId":"123456","name":"container-stopped","host":"localhost"} +data: {"name":"container-stopped","host":"localhost","actorId":"123456"} /* snapshot: Test_handler_streamLogs_happy_with_id */ HTTP/1.1 200 OK @@ -219,4 +219,4 @@ id: 1589396137772 event: container-event -data: {"actorId":"123456","name":"container-stopped","host":"localhost"} \ No newline at end of file +data: {"name":"container-stopped","host":"localhost","actorId":"123456"} \ No newline at end of file diff --git a/internal/web/events.go b/internal/web/events.go index ed024c66..bbf248d5 100644 --- a/internal/web/events.go +++ b/internal/web/events.go @@ -59,9 +59,9 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) { return } switch event.Name { - case "start", "die", "destroy": - if event.Name == "start" { - log.Debug().Str("container", event.ActorID).Msg("container started") + case "start", "die", "destroy", "rename": + if event.Name == "start" || event.Name == "rename" { + log.Debug().Str("action", event.Name).Str("id", event.ActorID).Msg("container event") if containers, err := h.multiHostService.ListContainersForHost(event.Host); err == nil { if err := sseWriter.Event("containers-changed", containers); err != nil {