From bed25bf419d282b6fe4be6f6e7588a32f70f6eb6 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 2 Feb 2020 14:39:28 +0900 Subject: [PATCH] feat: supports to update docker_container (#236) According to the docs * https://docs.docker.com/engine/api/v1.40/#operation/ContainerUpdate * https://godoc.org/github.com/docker/docker/client#Client.ContainerUpdate --- docker/resource_docker_container.go | 8 ----- docker/resource_docker_container_funcs.go | 44 ++++++++++++++++++++++- docker/resource_docker_container_test.go | 24 +++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/docker/resource_docker_container.go b/docker/resource_docker_container.go index 768e4b0b..c853bd23 100644 --- a/docker/resource_docker_container.go +++ b/docker/resource_docker_container.go @@ -179,7 +179,6 @@ func resourceDockerContainer() *schema.Resource { "restart": { Type: schema.TypeString, Optional: true, - ForceNew: true, Default: "no", ValidateFunc: validateStringMatchesPattern(`^(no|on-failure|always|unless-stopped)$`), }, @@ -187,7 +186,6 @@ func resourceDockerContainer() *schema.Resource { "max_retry_count": { Type: schema.TypeInt, Optional: true, - ForceNew: true, }, "working_dir": { Type: schema.TypeString, @@ -568,14 +566,12 @@ func resourceDockerContainer() *schema.Resource { "memory": { Type: schema.TypeInt, Optional: true, - ForceNew: true, ValidateFunc: validateIntegerGeqThan(0), }, "memory_swap": { Type: schema.TypeInt, Optional: true, - ForceNew: true, ValidateFunc: validateIntegerGeqThan(-1), }, @@ -590,14 +586,12 @@ func resourceDockerContainer() *schema.Resource { "cpu_shares": { Type: schema.TypeInt, Optional: true, - ForceNew: true, ValidateFunc: validateIntegerGeqThan(0), }, "cpu_set": { Type: schema.TypeString, Optional: true, - ForceNew: true, ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`), }, @@ -1338,7 +1332,6 @@ func resourceDockerContainerV1() *schema.Resource { "memory": { Type: schema.TypeInt, Optional: true, - ForceNew: true, ValidateFunc: validateIntegerGeqThan(0), }, @@ -1366,7 +1359,6 @@ func resourceDockerContainerV1() *schema.Resource { "cpu_set": { Type: schema.TypeString, Optional: true, - ForceNew: true, ValidateFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`), }, diff --git a/docker/resource_docker_container_funcs.go b/docker/resource_docker_container_funcs.go index fe43693c..01fe3c11 100644 --- a/docker/resource_docker_container_funcs.go +++ b/docker/resource_docker_container_funcs.go @@ -682,7 +682,49 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error } func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) error { - // TODO call resourceDockerContainerRead here + attrs := []string{ + "restart", "max_retry_count", "cpu_shares", "memory", "cpu_set", "memory_swap", + } + for _, attr := range attrs { + if d.HasChange(attr) { + + // TODO update ulimits + // Updating ulimits seems not to work well. + // It succeeds to run `DockerClient.ContainerUpdate` with `ulimit` but actually `ulimit` aren't changed. + // https://github.com/terraform-providers/terraform-provider-docker/pull/236#discussion_r373819536 + // ulimits := []*units.Ulimit{} + // if v, ok := d.GetOk("ulimit"); ok { + // ulimits = ulimitsToDockerUlimits(v.(*schema.Set)) + // } + + updateConfig := container.UpdateConfig{ + RestartPolicy: container.RestartPolicy{ + Name: d.Get("restart").(string), + MaximumRetryCount: d.Get("max_retry_count").(int), + }, + Resources: container.Resources{ + CPUShares: int64(d.Get("cpu_shares").(int)), + Memory: int64(d.Get("memory").(int)) * 1024 * 1024, + CpusetCpus: d.Get("cpu_set").(string), + // Ulimits: ulimits, + }, + } + + if ms, ok := d.GetOk("memory_swap"); ok { + a := int64(ms.(int)) + if a > 0 { + a = a * 1024 * 1024 + } + updateConfig.Resources.MemorySwap = a + } + client := meta.(*ProviderConfig).DockerClient + _, err := client.ContainerUpdate(context.Background(), d.Id(), updateConfig) + if err != nil { + return fmt.Errorf("Unable to update a container: %w", err) + } + break + } + } return nil } diff --git a/docker/resource_docker_container_test.go b/docker/resource_docker_container_test.go index 82b9710f..e8a70de7 100644 --- a/docker/resource_docker_container_test.go +++ b/docker/resource_docker_container_test.go @@ -66,6 +66,12 @@ func TestAccDockerContainer_basic(t *testing.T) { testAccContainerRunning(resourceName, &c), ), }, + { + Config: testAccDockerContainerUpdateConfig, + Check: resource.ComposeTestCheckFunc( + testAccContainerRunning(resourceName, &c), + ), + }, { ResourceName: "docker_container.foo", ImportState: true, @@ -1580,6 +1586,24 @@ resource "docker_container" "foo" { } ` +const testAccDockerContainerUpdateConfig = ` +resource "docker_image" "foo" { + name = "nginx:latest" +} + +resource "docker_container" "foo" { + name = "tf-test" + image = "${docker_image.foo.latest}" + + restart = "on-failure" + max_retry_count = 5 + cpu_shares = 32 + cpu_set = "0-1" + memory = 512 + memory_swap = 2048 +} +` + const testAccDockerContainerWith2BridgeNetworkConfig = ` resource "docker_network" "tftest" { name = "tftest-contnw"