add basic runtime constraints to docker_container

This commit is contained in:
ryane 2015-10-27 19:53:49 -04:00
parent 311b164d56
commit 38eec5fbc7
3 changed files with 56 additions and 0 deletions

View file

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

View file

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

View file

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