mirror of
https://github.com/kata-containers/kata-containers.git
synced 2026-07-01 22:50:54 +00:00
runtime-rs: supplement static sizing from sandbox annotations
When static sandbox resource management is enabled, CRI CPU/memory sizing may live only in sandbox annotations and be missing from the OCI spec. Let's fill missing sizing fields from annotations before applying static VM sizing so runtime-rs follows the expected Kubernetes behavior for constrained pods. Signed-off-by: Fabiano Fidêncio <ffidencio@nvidia.com> Assisted-by: Codex <codex@openai.com>
This commit is contained in:
@@ -132,6 +132,22 @@ impl InitialSizeManager {
|
||||
})
|
||||
}
|
||||
|
||||
// Merge sizing values from sandbox annotations when the current source
|
||||
// (typically the OCI spec) does not carry CRI sandbox sizing keys.
|
||||
pub fn supplement_from_annotations(&mut self, annotation: &HashMap<String, String>) -> Result<()> {
|
||||
let from_annotation =
|
||||
InitialSize::try_from(annotation).context("failed to construct static resource")?;
|
||||
|
||||
if self.resource.vcpu == 0.0 {
|
||||
self.resource.vcpu = from_annotation.vcpu;
|
||||
}
|
||||
if self.resource.mem_mb == 0 {
|
||||
self.resource.mem_mb = from_annotation.mem_mb;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn setup_config(&mut self, config: &mut TomlConfig) -> Result<()> {
|
||||
// update this data to the hypervisor config for later use by hypervisor
|
||||
let hypervisor_name = &config.runtime.hypervisor_name;
|
||||
@@ -500,4 +516,36 @@ mod tests {
|
||||
mgr.setup_config(&mut config).unwrap();
|
||||
assert_eq!(mgr.get_orig_toml_default_mem(), 256);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_supplement_from_annotations_fills_missing_spec_sizing() {
|
||||
let mut mgr = InitialSizeManager {
|
||||
resource: InitialSize {
|
||||
vcpu: 0.0,
|
||||
mem_mb: 0,
|
||||
orig_toml_default_mem: 0,
|
||||
},
|
||||
};
|
||||
|
||||
let ann = HashMap::from([
|
||||
(
|
||||
cri_containerd::SANDBOX_CPU_PERIOD_KEY.to_string(),
|
||||
"100000".to_string(),
|
||||
),
|
||||
(
|
||||
cri_containerd::SANDBOX_CPU_QUOTA_KEY.to_string(),
|
||||
"120000".to_string(),
|
||||
),
|
||||
(
|
||||
cri_containerd::SANDBOX_MEM_KEY.to_string(),
|
||||
(256 * MIB).to_string(),
|
||||
),
|
||||
]);
|
||||
|
||||
mgr.supplement_from_annotations(&ann).unwrap();
|
||||
|
||||
const VCPU_TOLERANCE: f32 = 0.0001;
|
||||
assert!((mgr.resource.vcpu - 1.2).abs() < VCPU_TOLERANCE);
|
||||
assert_eq!(mgr.resource.mem_mb, 256);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,6 +216,13 @@ impl RuntimeHandlerManagerInner {
|
||||
.context("failed to construct static resource manager")?
|
||||
};
|
||||
|
||||
// For CRI sandboxes, sizing annotations are carried in PodSandboxConfig
|
||||
// and may be absent from the OCI sandbox spec. Fill any missing sizing
|
||||
// values from sandbox annotations before applying static sizing.
|
||||
initial_size_manager
|
||||
.supplement_from_annotations(&sandbox_config.annotations)
|
||||
.context("failed to supplement static resource manager from annotations")?;
|
||||
|
||||
initial_size_manager
|
||||
.setup_config(&mut config)
|
||||
.context("failed to setup static resource mgmt config")?;
|
||||
|
||||
Reference in New Issue
Block a user