Skip to content

Commit c032f78

Browse files
authored
feat: supports container rename (#3390)
1 parent b3177e9 commit c032f78

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
lines changed

assets/models/Container.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class GroupedContainers {
2323

2424
export class Container {
2525
private _stat: Ref<Stat>;
26+
private _name: string;
2627
private readonly _statsHistory: Ref<Stat[]>;
2728
private readonly movingAverageStat: Ref<Stat>;
28-
private readonly _name: string;
2929

3030
constructor(
3131
public readonly id: string,
@@ -76,6 +76,10 @@ export class Container {
7676
return this.group;
7777
}
7878

79+
set name(name: string) {
80+
this._name = name;
81+
}
82+
7983
get name() {
8084
return this.isSwarm
8185
? this.labels["com.docker.swarm.task.name"]

assets/stores/container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export const useContainerStore = defineStore("container", () => {
124124
const existing = allContainersById.value[c.id];
125125
existing.state = c.state;
126126
existing.health = c.health;
127+
existing.name = c.name;
127128
});
128129

129130
containers.value = [

internal/docker/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,10 @@ func (d *httpClient) ContainerEvents(ctx context.Context, messages chan<- Contai
309309
case message := <-dockerMessages:
310310
if message.Type == events.ContainerEventType && len(message.Actor.ID) > 0 {
311311
messages <- ContainerEvent{
312-
ActorID: message.Actor.ID[:12],
313-
Name: string(message.Action),
314-
Host: d.host.ID,
312+
ActorID: message.Actor.ID[:12],
313+
Name: string(message.Action),
314+
Host: d.host.ID,
315+
ActorAttributes: message.Actor.Attributes,
315316
}
316317
}
317318
}

internal/docker/container_store.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ func (s *ContainerStore) init() {
230230
return c, true
231231
}
232232
})
233+
234+
case "rename":
235+
s.containers.Compute(event.ActorID, func(c *Container, loaded bool) (*Container, bool) {
236+
if loaded {
237+
log.Debug().Str("id", event.ActorID).Str("name", event.ActorAttributes["name"]).Msg("container renamed")
238+
c.Name = event.ActorAttributes["name"]
239+
return c, false
240+
} else {
241+
return c, true
242+
}
243+
})
233244
}
234245
s.subscribers.Range(func(c context.Context, events chan<- ContainerEvent) bool {
235246
select {

internal/docker/types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type ContainerStat struct {
3535

3636
// ContainerEvent represents events that are triggered
3737
type ContainerEvent struct {
38-
ActorID string `json:"actorId"`
39-
Name string `json:"name"`
40-
Host string `json:"host"`
38+
Name string `json:"name"`
39+
Host string `json:"host"`
40+
ActorID string `json:"actorId"`
41+
ActorAttributes map[string]string `json:"actorAttributes,omitempty"`
4142
}
4243

4344
type LogPosition string

internal/web/__snapshots__/web.snapshot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ data: []
145145

146146

147147
event: container-event
148-
data: {"actorId":"1234","name":"start","host":"localhost"}
148+
data: {"name":"start","host":"localhost","actorId":"1234"}
149149

150150
/* snapshot: Test_handler_streamLogs_error_finding_container */
151151
HTTP/1.1 404 Not Found
@@ -189,7 +189,7 @@ data: {"m":"INFO Testing logs...","ts":0,"id":4256192898,"l":"info","s":"stdout"
189189

190190

191191
event: container-event
192-
data: {"actorId":"123456","name":"container-stopped","host":"localhost"}
192+
data: {"name":"container-stopped","host":"localhost","actorId":"123456"}
193193

194194
/* snapshot: Test_handler_streamLogs_happy_container_stopped */
195195
HTTP/1.1 200 OK
@@ -202,7 +202,7 @@ Content-Type: text/event-stream
202202
X-Accel-Buffering: no
203203

204204
event: container-event
205-
data: {"actorId":"123456","name":"container-stopped","host":"localhost"}
205+
data: {"name":"container-stopped","host":"localhost","actorId":"123456"}
206206

207207
/* snapshot: Test_handler_streamLogs_happy_with_id */
208208
HTTP/1.1 200 OK
@@ -219,4 +219,4 @@ id: 1589396137772
219219

220220

221221
event: container-event
222-
data: {"actorId":"123456","name":"container-stopped","host":"localhost"}
222+
data: {"name":"container-stopped","host":"localhost","actorId":"123456"}

internal/web/events.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
5959
return
6060
}
6161
switch event.Name {
62-
case "start", "die", "destroy":
63-
if event.Name == "start" {
64-
log.Debug().Str("container", event.ActorID).Msg("container started")
62+
case "start", "die", "destroy", "rename":
63+
if event.Name == "start" || event.Name == "rename" {
64+
log.Debug().Str("action", event.Name).Str("id", event.ActorID).Msg("container event")
6565

6666
if containers, err := h.multiHostService.ListContainersForHost(event.Host); err == nil {
6767
if err := sseWriter.Event("containers-changed", containers); err != nil {

0 commit comments

Comments
 (0)