Skip to content

Commit 867cda4

Browse files
committed
Added the health status from healthcheck to RuntimeContainer struct
1 parent cfd2934 commit 867cda4

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

README.md

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

284284
type State struct {
285-
Running bool
285+
Running bool
286+
Health Health
287+
}
288+
289+
type Health struct {
290+
Status string
286291
}
287292

288293
// 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
@@ -81,6 +81,11 @@ type Volume struct {
8181

8282
type State struct {
8383
Running bool
84+
Health Health
85+
}
86+
87+
type Health struct {
88+
Status string
8489
}
8590

8691
type RuntimeContainer struct {

internal/generator/generator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (g *generator) generateFromEvents() {
275275
time.Sleep(10 * time.Second)
276276
break
277277
}
278-
if event.Status == "start" || event.Status == "stop" || event.Status == "die" {
278+
if event.Status == "start" || event.Status == "stop" || event.Status == "die" || strings.Index(event.Status, "health_status:") != -1 {
279279
log.Printf("Received event %s for container %s", event.Status, event.ID[:12])
280280
// fanout event to all watchers
281281
for _, watcher := range watchers {
@@ -389,6 +389,9 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
389389
},
390390
State: context.State{
391391
Running: container.State.Running,
392+
Health: context.Health{
393+
Status: container.State.Health.Status,
394+
},
392395
},
393396
Name: strings.TrimLeft(container.Name, "/"),
394397
Hostname: container.Config.Hostname,

internal/generator/generator_test.go

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

templates/nginx.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ upstream {{ $host }} {
1313

1414
{{ $addrLen := len $value.Addresses }}
1515
{{ $network := index $value.Networks 0 }}
16+
17+
{{ if $value.State.Health.Status }}
18+
{{ if ne $value.State.Health.Status "healthy" }}
19+
{{ continue }}
20+
{{ end }}
21+
{{ end }}
1622

1723
{{/* If only 1 port exposed, use that */}}
1824
{{ if eq $addrLen 1 }}

test.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
server {
2+
listen 80 default_server;
3+
server_name _; # This is just an invalid value which will never trigger on a real hostname.
4+
error_log /proc/self/fd/2;
5+
access_log /proc/self/fd/1;
6+
return 503;
7+
}

0 commit comments

Comments
 (0)