Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parameters for cpu quota handling #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/provider/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,27 @@ func resourceDockerContainer() *schema.Resource {
ValidateDiagFunc: validateStringMatchesPattern(`^\d+([,-]\d+)*$`),
},

"cpus": {
Type: schema.TypeFloat,
Description: "Specify how much of the available CPU resources a container can use.",
Optional: true,
ValidateDiagFunc: validateIntegerGeqThan(0),
},

"cpu_period": {
Type: schema.TypeInt,
Description: "Specify the CPU CFS scheduler period, which is used alongside `cpu-quota`.",
Optional: true,
ValidateDiagFunc: validateIntegerGeqThan(0),
},

"cpu_quota": {
Type: schema.TypeInt,
Description: "Impose a CPU CFS quota on the container. The number of microseconds per `cpu-period` that the container is limited to before throttled.",
Optional: true,
ValidateDiagFunc: validateIntegerGeqThan(0),
},

"log_driver": {
Type: schema.TypeString,
Description: "The logging driver to use for the container.",
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 @@ -320,6 +320,27 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
hostConfig.CpusetCpus = v.(string)
}

if v, ok := d.GetOk("cpus"); ok {
hostConfig.CPUPeriod = 100000
hostConfig.CPUQuota = int64(v.(float32) * 100000)

if _, ook := d.GetOk("cpu_period"); ook {
log.Printf("[WARN] Value for cpus is set, ignore cpu_period setting")
}

if _, ook := d.GetOk("cpu_quota"); ook {
log.Printf("[WARN] Value for cpus is set, ignore cpu_quota setting")
}
} else {
if vv, ook := d.GetOk("cpu_period"); ook {
hostConfig.CPUPeriod = int64(vv.(int))
}

if vv, ook := d.GetOk("cpu_quota"); ook {
hostConfig.CPUQuota = int64(vv.(int))
}
}

if v, ok := d.GetOk("log_opts"); ok {
hostConfig.LogConfig.Config = mapTypeMapValsToString(v.(map[string]interface{}))
}
Expand Down