Skip to content

Added the health status from healthcheck to RuntimeContainer struct #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ type SwarmNode struct {
}

type State struct {
Running bool
Running bool
Health Health
}

type Health struct {
Status string
}

// Accessible from the root in templates as .Docker
Expand Down
5 changes: 5 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ type Volume struct {

type State struct {
Running bool
Health Health
}

type Health struct {
Status string
}

type RuntimeContainer struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (g *generator) generateFromEvents() {
time.Sleep(10 * time.Second)
break
}
if event.Status == "start" || event.Status == "stop" || event.Status == "die" {
if event.Status == "start" || event.Status == "stop" || event.Status == "die" || strings.Index(event.Status, "health_status:") != -1 {
log.Printf("Received event %s for container %s", event.Status, event.ID[:12])
// fanout event to all watchers
for _, watcher := range watchers {
Expand Down Expand Up @@ -401,6 +401,9 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
},
State: context.State{
Running: container.State.Running,
Health: context.Health{
Status: container.State.Health.Status,
},
},
Name: strings.TrimLeft(container.Name, "/"),
Hostname: container.Config.Hostname,
Expand Down
5 changes: 5 additions & 0 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestGenerateFromEvents(t *testing.T) {
Pid: 400,
ExitCode: 0,
StartedAt: time.Now(),
Health: docker.Health{
Status: "healthy",
FailingStreak: 5,
Log: []docker.HealthCheck{},
},
},
Image: "0ff407d5a7d9ed36acdf3e75de8cc127afecc9af234d05486be2981cdc01a38d",
NetworkSettings: &docker.NetworkSettings{
Expand Down
6 changes: 6 additions & 0 deletions templates/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ upstream {{ $host }} {
{{ $addrLen := len $value.Addresses }}
{{ $network := index $value.Networks 0 }}

{{ if $value.State.Health.Status }}
{{ if ne $value.State.Health.Status "healthy" }}
{{ continue }}
{{ end }}
{{ end }}

{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ with $address := index $value.Addresses 0 }}
Expand Down