From 49e35c46b2fb7121b3202f1ae7685d431b354fab Mon Sep 17 00:00:00 2001 From: Boris HUISGEN Date: Mon, 8 Oct 2018 15:02:13 +0200 Subject: [PATCH] Add container healthcheck Signed-off-by: Boris HUISGEN --- docker/resource_docker_container.go | 45 ++++++++++++++++ docker/resource_docker_container_funcs.go | 24 +++++++++ docker/resource_docker_container_test.go | 62 +++++++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/docker/resource_docker_container.go b/docker/resource_docker_container.go index f853ba75..27dd292a 100644 --- a/docker/resource_docker_container.go +++ b/docker/resource_docker_container.go @@ -450,6 +450,51 @@ func resourceDockerContainer() *schema.Resource { }, }, }, + + "healthcheck": &schema.Schema{ + Type: schema.TypeList, + Description: "A test to perform to check that the container is healthy", + MaxItems: 1, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "test": &schema.Schema{ + Type: schema.TypeList, + Description: "The test to perform as list", + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "interval": &schema.Schema{ + Type: schema.TypeString, + Description: "Time between running the check (ms|s|m|h)", + Optional: true, + Default: "0s", + ValidateFunc: validateDurationGeq0(), + }, + "timeout": &schema.Schema{ + Type: schema.TypeString, + Description: "Maximum time to allow one check to run (ms|s|m|h)", + Optional: true, + Default: "0s", + ValidateFunc: validateDurationGeq0(), + }, + "start_period": &schema.Schema{ + Type: schema.TypeString, + Description: "Start period for the container to initialize before counting retries towards unstable (ms|s|m|h)", + Optional: true, + Default: "0s", + ValidateFunc: validateDurationGeq0(), + }, + "retries": &schema.Schema{ + Type: schema.TypeInt, + Description: "Consecutive failures needed to report unhealthy", + Optional: true, + Default: 0, + ValidateFunc: validateIntegerGeqThan(0), + }, + }, + }, + }, }, } } diff --git a/docker/resource_docker_container_funcs.go b/docker/resource_docker_container_funcs.go index ca4ca9a4..9a67d97f 100644 --- a/docker/resource_docker_container_funcs.go +++ b/docker/resource_docker_container_funcs.go @@ -108,6 +108,30 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err config.Labels = mapTypeMapValsToString(v.(map[string]interface{})) } + if value, ok := d.GetOk("healthcheck"); ok { + config.Healthcheck = &container.HealthConfig{} + if len(value.([]interface{})) > 0 { + for _, rawHealthCheck := range value.([]interface{}) { + rawHealthCheck := rawHealthCheck.(map[string]interface{}) + if testCommand, ok := rawHealthCheck["test"]; ok { + config.Healthcheck.Test = stringListToStringSlice(testCommand.([]interface{})) + } + if rawInterval, ok := rawHealthCheck["interval"]; ok { + config.Healthcheck.Interval, _ = time.ParseDuration(rawInterval.(string)) + } + if rawTimeout, ok := rawHealthCheck["timeout"]; ok { + config.Healthcheck.Timeout, _ = time.ParseDuration(rawTimeout.(string)) + } + if rawStartPeriod, ok := rawHealthCheck["start_period"]; ok { + config.Healthcheck.StartPeriod, _ = time.ParseDuration(rawStartPeriod.(string)) + } + if rawRetries, ok := rawHealthCheck["retries"]; ok { + config.Healthcheck.Retries, _ = rawRetries.(int) + } + } + } + } + hostConfig := &container.HostConfig{ Privileged: d.Get("privileged").(bool), PublishAllPorts: d.Get("publish_all_ports").(bool), diff --git a/docker/resource_docker_container_test.go b/docker/resource_docker_container_test.go index 3d5a425c..a505c065 100644 --- a/docker/resource_docker_container_test.go +++ b/docker/resource_docker_container_test.go @@ -4,6 +4,7 @@ import ( "archive/tar" "bytes" "fmt" + "reflect" "strconv" "strings" "testing" @@ -613,6 +614,48 @@ func TestAccDockerContainer_multiple_ports(t *testing.T) { }) } +func TestAccDockerContainer_healthcheck(t *testing.T) { + var c types.ContainerJSON + + testCheck := func(*terraform.State) error { + if !reflect.DeepEqual(c.Config.Healthcheck.Test, []string{"CMD", "/bin/true"}) { + return fmt.Errorf("Container doesn't have a correct healthcheck test") + } + + if c.Config.Healthcheck.Interval != 30000000000 { + return fmt.Errorf("Container doesn't have a correct healthcheck interval") + } + + if c.Config.Healthcheck.Timeout != 5000000000 { + return fmt.Errorf("Container doesn't have a correct healthcheck timeout") + } + + if c.Config.Healthcheck.StartPeriod != 15000000000 { + return fmt.Errorf("Container doesn't have a correct healthcheck retries") + } + + if c.Config.Healthcheck.Retries != 10 { + return fmt.Errorf("Container doesn't have a correct healthcheck retries") + } + + return nil + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDockerContainerHealthcheckConfig, + Check: resource.ComposeTestCheckFunc( + testAccContainerRunning("docker_container.foo", &c), + testCheck, + ), + }, + }, + }) +} + func testAccContainerRunning(n string, container *types.ContainerJSON) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -885,3 +928,22 @@ resource "docker_container" "foo" { ] } ` +const testAccDockerContainerHealthcheckConfig = ` +resource "docker_image" "foo" { + name = "nginx:latest" + keep_locally = true +} + +resource "docker_container" "foo" { + name = "tf-test" + image = "${docker_image.foo.latest}" + + healthcheck { + test = ["CMD", "/bin/true"] + interval = "30s" + timeout = "5s" + start_period = "15s" + retries = 10 + } +} +`