Add container healthcheck

Signed-off-by: Boris HUISGEN <bhuisgen@hbis.fr>
This commit is contained in:
Boris HUISGEN 2018-10-08 15:02:13 +02:00
parent 15522080d6
commit 49e35c46b2
3 changed files with 131 additions and 0 deletions

View file

@ -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),
},
},
},
},
},
}
}

View file

@ -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),

View file

@ -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
}
}
`