Skip to content

Commit 8dc68b7

Browse files
free6kbuchdag
andauthored
feat: add health status to RuntimeContainer struct (#442)
Co-authored-by: Nicolas Duchon <[email protected]>
1 parent d6476e5 commit 8dc68b7

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,12 @@ type SwarmNode struct {
289289
}
290290

291291
type State struct {
292-
Running bool
292+
Running bool
293+
Health Health
294+
}
295+
296+
type Health struct {
297+
Status string
293298
}
294299

295300
// Accessible from the root in templates as .Docker

internal/context/context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ type Volume struct {
8282

8383
type State struct {
8484
Running bool
85+
Health Health
86+
}
87+
88+
type Health struct {
89+
Status string
8590
}
8691

8792
type RuntimeContainer struct {

internal/generator/generator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (g *generator) generateFromEvents() {
278278
time.Sleep(10 * time.Second)
279279
break
280280
}
281-
if event.Status == "start" || event.Status == "stop" || event.Status == "die" {
281+
if event.Status == "start" || event.Status == "stop" || event.Status == "die" || strings.Index(event.Status, "health_status:") != -1 {
282282
log.Printf("Received event %s for container %s", event.Status, event.ID[:12])
283283
// fanout event to all watchers
284284
for _, watcher := range watchers {
@@ -401,6 +401,9 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
401401
},
402402
State: context.State{
403403
Running: container.State.Running,
404+
Health: context.Health{
405+
Status: container.State.Health.Status,
406+
},
404407
},
405408
Name: strings.TrimLeft(container.Name, "/"),
406409
Hostname: container.Config.Hostname,

internal/generator/generator_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func TestGenerateFromEvents(t *testing.T) {
8888
Pid: 400,
8989
ExitCode: 0,
9090
StartedAt: time.Now(),
91+
Health: docker.Health{
92+
Status: "healthy",
93+
FailingStreak: 5,
94+
Log: []docker.HealthCheck{},
95+
},
9196
},
9297
Image: "0ff407d5a7d9ed36acdf3e75de8cc127afecc9af234d05486be2981cdc01a38d",
9398
NetworkSettings: &docker.NetworkSettings{

templates/nginx.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ upstream {{ $host }} {
1919
{{ $addrLen := len $value.Addresses }}
2020
{{ $network := index $value.Networks 0 }}
2121

22+
{{ if $value.State.Health.Status }}
23+
{{ if ne $value.State.Health.Status "healthy" }}
24+
{{ continue }}
25+
{{ end }}
26+
{{ end }}
27+
2228
{{/* If only 1 port exposed, use that */}}
2329
{{ if eq $addrLen 1 }}
2430
{{ with $address := index $value.Addresses 0 }}

0 commit comments

Comments
 (0)