From d68389dc5233adc4c28b81f49f07aacff8287f55 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 3 Oct 2016 11:01:37 +0200 Subject: [PATCH] Add support for docker healthcheck - React to health_status events - Filter container that have a health status *and* that are not healthy Signed-off-by: Vincent Demeester --- provider/docker.go | 13 +++++++++++++ provider/docker_test.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/provider/docker.go b/provider/docker.go index 411f18696..50259d6bf 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -58,6 +58,7 @@ type dockerData struct { Name string Labels map[string]string // List of labels set to container or service NetworkSettings networkSettings + Health string } // NetworkSettings holds the networks data to the Docker provider @@ -214,6 +215,9 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po } eventHandler.Handle("start", startStopHandle) eventHandler.Handle("die", startStopHandle) + eventHandler.Handle("health_status: healthy", startStopHandle) + eventHandler.Handle("health_status: unhealthy", startStopHandle) + eventHandler.Handle("health_status: starting", startStopHandle) errChan := events.MonitorWithHandler(ctx, dockerClient, options, eventHandler) if err := <-errChan; err != nil { @@ -378,6 +382,11 @@ func (provider *Docker) containerFilter(container dockerData) bool { return false } + if container.Health != "" && container.Health != "healthy" { + log.Debugf("Filtering unhealthy or starting container %s", container.Name) + return false + } + return true } @@ -578,6 +587,10 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData { } + if container.State != nil && container.State.Health != nil { + dockerData.Health = container.State.Health.Status + } + return dockerData } diff --git a/provider/docker_test.go b/provider/docker_test.go index 45f5ccbd8..3d106bd71 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -569,12 +569,18 @@ func TestDockerGetLabel(t *testing.T) { }{ { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{}, }, expected: "Label not found:", }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "bar", @@ -608,6 +614,9 @@ func TestDockerGetLabels(t *testing.T) { }{ { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{}, }, expectedLabels: map[string]string{}, @@ -615,6 +624,9 @@ func TestDockerGetLabels(t *testing.T) { }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "fooz", @@ -628,6 +640,9 @@ func TestDockerGetLabels(t *testing.T) { }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "fooz",