mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-26 01:29:36 -05:00
add basic runtime constraints to docker_container
This commit is contained in:
parent
311b164d56
commit
38eec5fbc7
3 changed files with 56 additions and 0 deletions
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
`
|
||||
|
|
|
|||
Loading…
Reference in a new issue