Skip to content

Commit

Permalink
K8s kubelet reserved-cpus support
Browse files Browse the repository at this point in the history
  • Loading branch information
james-masson authored and yeazelm committed Jun 13, 2024
1 parent 618ec7a commit 241b187
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/kubernetes-1.23/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -182,3 +186,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.24/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -181,3 +185,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.25/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -182,3 +186,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.26/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -182,3 +186,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.27/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -183,3 +187,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.28/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -181,3 +185,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.29/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -181,3 +185,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
7 changes: 7 additions & 0 deletions packages/kubernetes-1.30/kubelet-config
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ kubeReserved:
{{/if}}
{{/if}}
ephemeral-storage: "{{default "1Gi" settings.kubernetes.kube-reserved.ephemeral-storage}}"
{{#unless settings.kubernetes.reserved-cpus}}
kubeReservedCgroup: "/runtime"
{{/unless}}
{{#if settings.kubernetes.system-reserved}}
systemReserved:
{{#each settings.kubernetes.system-reserved}}
{{@key}}: "{{this}}"
{{/each}}
{{#unless settings.kubernetes.reserved-cpus}}
systemReservedCgroup: "/system"
{{/unless}}
{{/if}}
cpuCFSQuota: {{default true settings.kubernetes.cpu-cfs-quota-enforced}}
cpuManagerPolicy: {{default "none" settings.kubernetes.cpu-manager-policy}}
Expand Down Expand Up @@ -181,3 +185,6 @@ reservedMemory:
{{/each}}
{{/if}}
{{/if}}
{{#if settings.kubernetes.reserved-cpus}}
reservedSystemCPUs: {{settings.kubernetes.reserved-cpus}}
{{/if}}
3 changes: 3 additions & 0 deletions sources/models/modeled-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub mod error {
input: String,
source: std::num::ParseIntError,
},

#[snafu(display("Invalid Kernel CpuSet value '{}'", input))]
InvalidKernelCpuSetValue { input: String },
}

/// Creates a `ValidationError` with a consistent message for strings with regex validations
Expand Down
75 changes: 75 additions & 0 deletions sources/models/modeled-types/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,3 +1178,78 @@ mod test_positive_integer {
assert!(NonNegativeInteger::try_from(-1).is_err());
}
}

// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^=

/// KernelCpuSetValue represents a string that contains a valid Kernel CpuSet Value from
/// here: https://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS. This matches the
/// logic from https://github.com/kubernetes/utils/blob/d93618cff8a22d3aea7bf78d9d528fd859720c2d/cpuset/cpuset.go#L203
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KernelCpuSetValue {
inner: String,
}

lazy_static! {
pub(crate) static ref KERNAL_CPU_SET_VALUE: Regex =
Regex::new(r"^([0-9]+(-[0-9]+)?,?)*([0-9]+(-[0-9]+)?)+$").unwrap();
}

impl TryFrom<&str> for KernelCpuSetValue {
type Error = error::Error;

fn try_from(input: &str) -> Result<Self, Self::Error> {
ensure!(
!input.is_empty(),
error::InvalidKernelCpuSetValueSnafu { input }
);
ensure!(
KERNAL_CPU_SET_VALUE.is_match(input),
error::InvalidKernelCpuSetValueSnafu { input }
);
Ok(KernelCpuSetValue {
inner: input.to_string(),
})
}
}

string_impls_for!(KernelCpuSetValue, "KernelCpuSetValue");

#[cfg(test)]
mod test_kernel_cpu_set_value {
use super::KernelCpuSetValue;
use std::convert::TryFrom;

#[test]
fn good_tokens() {
for ok in &[
"1",
"0,1,2,3",
"1-4",
"1,6-9",
"1-4,6",
"1-3,6-9,10-15",
"100,101",
"1,2,3,4,5,6,7,8,9,10",
] {
KernelCpuSetValue::try_from(*ok).unwrap();
}
}

#[test]
fn bad_names() {
for err in &[
"",
"100.0",
"0...4",
"1..5",
"ten",
"1-",
"-3",
"9,-10",
"1,2,3,4,5,6,7,8,9,10,",
&"a".repeat(23),
] {
KernelCpuSetValue::try_from(*err).unwrap_err();
}
}
}
15 changes: 8 additions & 7 deletions sources/models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ use std::net::IpAddr;
use crate::de::{deserialize_limit, deserialize_mirrors, deserialize_node_taints};
use modeled_types::{
BootConfigKey, BootConfigValue, BootstrapContainerMode, CpuManagerPolicy, CredentialProvider,
DNSDomain, EtcHostsEntries, Identifier, IntegerPercent, KubernetesAuthenticationMode,
KubernetesBootstrapToken, KubernetesCloudProvider, KubernetesClusterDnsIp,
KubernetesClusterName, KubernetesDurationValue, KubernetesLabelKey, KubernetesLabelValue,
KubernetesQuantityValue, KubernetesReservedResourceKey, KubernetesTaintValue,
KubernetesThresholdValue, OciDefaultsCapability, OciDefaultsResourceLimitType,
PemCertificateString, SingleLineString, TopologyManagerPolicy, TopologyManagerScope, Url,
ValidBase64, ValidLinuxHostname,
DNSDomain, EtcHostsEntries, Identifier, IntegerPercent, KernelCpuSetValue,
KubernetesAuthenticationMode, KubernetesBootstrapToken, KubernetesCloudProvider,
KubernetesClusterDnsIp, KubernetesClusterName, KubernetesDurationValue, KubernetesLabelKey,
KubernetesLabelValue, KubernetesQuantityValue, KubernetesReservedResourceKey,
KubernetesTaintValue, KubernetesThresholdValue, OciDefaultsCapability,
OciDefaultsResourceLimitType, PemCertificateString, SingleLineString, TopologyManagerPolicy,
TopologyManagerScope, Url, ValidBase64, ValidLinuxHostname,
};

#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -137,6 +137,7 @@ struct KubernetesSettings {
shutdown_grace_period_for_critical_pods: KubernetesDurationValue,
memory_manager_reserved_memory: HashMap<Identifier, KubernetesMemoryReservation>,
memory_manager_policy: KubernetesMemoryManagerPolicy,
reserved_cpus: KernelCpuSetValue,

// Settings where we generate a value based on the runtime environment. The user can specify a
// value to override the generated one, but typically would not.
Expand Down

0 comments on commit 241b187

Please sign in to comment.