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

Interpret a cpu quota of zero as default value #569

Merged
merged 2 commits into from
Dec 28, 2021
Merged
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
27 changes: 27 additions & 0 deletions crates/integration_test/src/tests/cgroups/cpu/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ fn test_cpu_quota_valid_set() -> TestResult {
})
}

/// Tests if the cpu quota is the defalt value (max) if a cpu quota of zero has been specified
fn test_cpu_quota_zero_default_set() -> TestResult {
let cpu_quota = 0;
let cpu = test_result!(LinuxCpuBuilder::default()
.quota(cpu_quota)
.build()
.context("build cpu spec"));

let spec = test_result!(create_spec("test_cpu_quota_zero_default_set", cpu));
test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
test_result!(check_cpu_max(
"test_cpu_quota_zero_default_set",
i64::MAX,
DEFAULT_PERIOD
));
TestResult::Passed
})
}

/// Tests if the cpu quota is the default value (max) if a negative cpu quota has been specified
fn test_cpu_quota_negative_default_set() -> TestResult {
let cpu_quota = -9999;
Expand Down Expand Up @@ -342,6 +362,12 @@ pub fn get_test_group<'a>() -> TestGroup<'a> {
Box::new(test_cpu_quota_valid_set),
);

let test_cpu_quota_zero_default_set = ConditionalTest::new(
"test_cpu_quota_zero_default_set",
Box::new(can_run),
Box::new(test_cpu_quota_zero_default_set),
);

let test_cpu_quota_negative_default_set = ConditionalTest::new(
"test_cpu_quota_negative_value_default_set",
Box::new(can_run),
Expand Down Expand Up @@ -371,6 +397,7 @@ pub fn get_test_group<'a>() -> TestGroup<'a> {
Box::new(test_cpu_weight_zero_ignored),
Box::new(test_cpu_weight_too_high_maximum_set),
Box::new(test_cpu_quota_valid_set),
Box::new(test_cpu_quota_zero_default_set),
Box::new(test_cpu_quota_negative_default_set),
Box::new(test_cpu_period_valid_set),
Box::new(test_cpu_period_unspecified_unchanged),
Expand Down
8 changes: 4 additions & 4 deletions crates/libcgroups/src/v2/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ impl Cpu {
let cpu_max_file = path.join(CGROUP_CPU_MAX);
let new_cpu_max: Option<Cow<str>> = match (cpu.quota(), cpu.period()) {
(None, Some(period)) => Self::create_period_only_value(&cpu_max_file, period)?,
(Some(quota), None) if quota >= 0 => Some(quota.to_string().into()),
(Some(quota), None) if quota < 0 => Some(UNRESTRICTED_QUOTA.into()),
(Some(quota), Some(period)) if quota >= 0 => {
(Some(quota), None) if quota > 0 => Some(quota.to_string().into()),
(Some(quota), None) if quota <= 0 => Some(UNRESTRICTED_QUOTA.into()),
(Some(quota), Some(period)) if quota > 0 => {
Some(format!("{} {}", quota, period).into())
}
(Some(quota), Some(period)) if quota < 0 => {
(Some(quota), Some(period)) if quota <= 0 => {
Some(format!("{} {}", UNRESTRICTED_QUOTA, period).into())
}
_ => None,
Expand Down