From 38eec5fbc7babc5905ae60e33988b0c96e14fdd2 Mon Sep 17 00:00:00 2001 From: ryane Date: Tue, 27 Oct 2015 19:53:49 -0400 Subject: [PATCH] add basic runtime constraints to docker_container --- resource_docker_container.go | 18 ++++++++++++++++++ resource_docker_container_funcs.go | 24 ++++++++++++++++++++++++ resource_docker_container_test.go | 14 ++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/resource_docker_container.go b/resource_docker_container.go index 7d2fa34c..48eac9a4 100644 --- a/resource_docker_container.go +++ b/resource_docker_container.go @@ -171,6 +171,24 @@ func resourceDockerContainer() *schema.Resource { Optional: true, ForceNew: true, }, + + "memory": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + + "memory_swap": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + + "cpu_shares": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, }, } } diff --git a/resource_docker_container_funcs.go b/resource_docker_container_funcs.go index 800f0f8a..0f1a9d9e 100644 --- a/resource_docker_container_funcs.go +++ b/resource_docker_container_funcs.go @@ -120,6 +120,30 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err hostConfig.Links = stringSetToStringSlice(v.(*schema.Set)) } + if v, ok := d.GetOk("memory"); ok { + memory := int64(v.(int)) + if memory > 0 { + hostConfig.Memory = memory * 1024 * 1024 + } + } + + if v, ok := d.GetOk("memory_swap"); ok { + swap := int64(v.(int)) + if swap != 0 { + if swap > 0 { // only convert positive #s to bytes + swap = swap * 1024 * 1024 + } + hostConfig.MemorySwap = swap + } + } + + if v, ok := d.GetOk("cpu_shares"); ok { + shares := int64(v.(int)) + if shares > 0 { + hostConfig.CPUShares = shares + } + } + creationTime = time.Now() if err := client.StartContainer(retContainer.ID, hostConfig); err != nil { return fmt.Errorf("Unable to start container: %s", err) diff --git a/resource_docker_container_test.go b/resource_docker_container_test.go index 0d0fe734..1402f129 100644 --- a/resource_docker_container_test.go +++ b/resource_docker_container_test.go @@ -44,6 +44,17 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) } + if c.HostConfig.Memory != (128 * 1024 * 1024) { + return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory) + } + + if c.HostConfig.MemorySwap != (128 * 1024 * 1024) { + return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.Memory) + } + + if c.HostConfig.CPUShares != 512 { + return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) + } return nil } @@ -115,5 +126,8 @@ resource "docker_container" "foo" { entrypoint = ["/bin/bash", "-c", "ping localhost"] restart = "on-failure" max_retry_count = 5 + memory = 128 + memory_swap = 128 + cpu_shares = 512 } `