Skip to content

Commit

Permalink
feat: support setting cpu shares
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackskyliner committed Jul 31, 2023
1 parent 54685a5 commit c910c11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ resource "docker_image" "ubuntu" {
- `domainname` (String) Domain name of the container.
- `entrypoint` (List of String) The command to use as the Entrypoint for the container. The Entrypoint allows you to configure a container to run as an executable. For example, to run `/usr/bin/myprogram` when starting a container, set the entrypoint to be `"/usr/bin/myprogra"]`.
- `env` (Set of String) Environment variables to set in the form of `KEY=VALUE`, e.g. `DEBUG=0`
- `cpus` (String) Specify how much of the available CPU resources a container can use. e.g a value of 1.5 means the container is guaranteed at most one and a half of the CPUs
- `gpus` (String) GPU devices to add to the container. Currently, only the value `all` is supported. Passing any other value will result in unexpected behavior.
- `group_add` (Set of String) Additional groups for the container user
- `healthcheck` (Block List, Max: 1) A test to perform to check that the container is healthy (see [below for nested schema](#nestedblock--healthcheck))
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,12 @@ func resourceDockerContainer() *schema.Resource {
Optional: true,
ForceNew: true,
},
"cpus": {
Type: schema.TypeString,
Description: "Specify how much of the available CPU resources a container can use. e.g a value of 1.5 means the container is guaranteed at most one and a half of the CPUs",
Optional: true,
ForceNew: true,
},
"cgroupns_mode": {
Type: schema.TypeString,
Description: "Cgroup namespace mode to use for the container. Possible values are: `private`, `host`.",
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"strings"
"time"
"math/big"

"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -312,6 +313,23 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
hostConfig.ShmSize = int64(v.(int)) * 1024 * 1024
}

if v, ok := d.GetOk("cpus"); ok {
if client.ClientVersion() >= "1.28" {
cpu, ok := new(big.Rat).SetString(v.(string))
if !ok {
return diag.Errorf("Error setting cpus: Failed to parse %v as a rational number", v.(string))
}
nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
if !nano.IsInt() {
return diag.Errorf("Error setting cpus: value is too precise")
}

hostConfig.NanoCPUs = nano.Num().Int64()
} else {
log.Printf("[WARN] Setting CPUs count/quota requires docker version 1.28 or higher")
}
}

if v, ok := d.GetOk("cpu_shares"); ok {
hostConfig.CPUShares = int64(v.(int))
}
Expand Down Expand Up @@ -727,6 +745,9 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me
d.Set("memory_swap", container.HostConfig.MemorySwap)
}
d.Set("shm_size", container.HostConfig.ShmSize/1024/1024)
if container.HostConfig.NanoCPUs > 0 {
d.Set("cpus", container.HostConfig.NanoCPUs)
}
d.Set("cpu_shares", container.HostConfig.CPUShares)
d.Set("cpu_set", container.HostConfig.CpusetCpus)
d.Set("log_driver", container.HostConfig.LogConfig.Type)
Expand Down

0 comments on commit c910c11

Please sign in to comment.