mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-12 20:39:30 +00:00
kata-types: do alignment of oci-spec for kata-types
Fixes #9766 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
@@ -18,7 +18,7 @@ byte-unit = "5.0.3"
|
||||
glob = "0.3.0"
|
||||
lazy_static = "1.4.0"
|
||||
num_cpus = "1.13.1"
|
||||
regex = "1.5.6"
|
||||
regex = "1.10.5"
|
||||
serde = { version = "1.0.100", features = ["derive"] }
|
||||
slog = "2.5.2"
|
||||
slog-scope = "4.4.0"
|
||||
@@ -28,7 +28,7 @@ toml = "0.5.8"
|
||||
serde-enum-str = "0.4"
|
||||
sysinfo = "0.30.5"
|
||||
|
||||
oci = { path = "../oci" }
|
||||
oci-spec = { version = "0.6.8", features = ["runtime"] }
|
||||
safe-path = { path = "../safe-path" }
|
||||
|
||||
[dev-dependencies]
|
||||
|
@@ -3,11 +3,10 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
use oci_spec::runtime as oci;
|
||||
use std::convert::TryFrom;
|
||||
use std::str::FromStr;
|
||||
|
||||
use oci::LinuxCpu;
|
||||
|
||||
/// A set of CPU ids.
|
||||
pub type CpuSet = crate::utils::u32_set::U32Set;
|
||||
|
||||
@@ -69,15 +68,17 @@ impl LinuxContainerCpuResources {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&LinuxCpu> for LinuxContainerCpuResources {
|
||||
impl TryFrom<&oci::LinuxCpu> for LinuxContainerCpuResources {
|
||||
type Error = Error;
|
||||
|
||||
// Unhandled fields: realtime_runtime, realtime_period, mems
|
||||
fn try_from(value: &LinuxCpu) -> Result<Self, Self::Error> {
|
||||
let period = value.period.unwrap_or(0);
|
||||
let quota = value.quota.unwrap_or(-1);
|
||||
let cpuset = CpuSet::from_str(&value.cpus).map_err(Error::InvalidCpuSet)?;
|
||||
let nodeset = NumaNodeSet::from_str(&value.mems).map_err(Error::InvalidNodeSet)?;
|
||||
fn try_from(value: &oci::LinuxCpu) -> Result<Self, Self::Error> {
|
||||
let period = value.period().unwrap_or(0);
|
||||
let quota = value.quota().unwrap_or(-1);
|
||||
let value_cpus = value.cpus().as_ref().map_or("", |cpus| cpus);
|
||||
let cpuset = CpuSet::from_str(value_cpus).map_err(Error::InvalidCpuSet)?;
|
||||
let value_mems = value.mems().as_ref().map_or("", |mems| mems);
|
||||
let nodeset = NumaNodeSet::from_str(value_mems).map_err(Error::InvalidNodeSet)?;
|
||||
|
||||
// If quota is -1, it means the CPU resource request is unconstrained. In that case,
|
||||
// we don't currently assign additional CPUs.
|
||||
@@ -88,7 +89,7 @@ impl TryFrom<&LinuxCpu> for LinuxContainerCpuResources {
|
||||
};
|
||||
|
||||
Ok(LinuxContainerCpuResources {
|
||||
shares: value.shares.unwrap_or(0),
|
||||
shares: value.shares().unwrap_or(0),
|
||||
period,
|
||||
quota,
|
||||
cpuset,
|
||||
@@ -170,16 +171,14 @@ mod tests {
|
||||
assert!(resources.nodeset.is_empty());
|
||||
assert!(resources.calculated_vcpu_time_ms.is_none());
|
||||
|
||||
let oci = oci::LinuxCpu {
|
||||
shares: Some(2048),
|
||||
quota: Some(1001),
|
||||
period: Some(100),
|
||||
realtime_runtime: None,
|
||||
realtime_period: None,
|
||||
cpus: "1,2,3".to_string(),
|
||||
mems: "1".to_string(),
|
||||
};
|
||||
let resources = LinuxContainerCpuResources::try_from(&oci).unwrap();
|
||||
let mut linux_cpu = oci::LinuxCpu::default();
|
||||
linux_cpu.set_shares(Some(2048));
|
||||
linux_cpu.set_quota(Some(1001));
|
||||
linux_cpu.set_period(Some(100));
|
||||
linux_cpu.set_cpus(Some("1,2,3".to_string()));
|
||||
linux_cpu.set_mems(Some("1".to_string()));
|
||||
|
||||
let resources = LinuxContainerCpuResources::try_from(&linux_cpu).unwrap();
|
||||
assert_eq!(resources.shares(), 2048);
|
||||
assert_eq!(resources.period(), 100);
|
||||
assert_eq!(resources.quota(), 1001);
|
||||
@@ -188,16 +187,12 @@ mod tests {
|
||||
assert_eq!(resources.cpuset().len(), 3);
|
||||
assert_eq!(resources.nodeset().len(), 1);
|
||||
|
||||
let oci = oci::LinuxCpu {
|
||||
shares: Some(2048),
|
||||
quota: None,
|
||||
period: None,
|
||||
realtime_runtime: None,
|
||||
realtime_period: None,
|
||||
cpus: "1".to_string(),
|
||||
mems: "1-2".to_string(),
|
||||
};
|
||||
let resources = LinuxContainerCpuResources::try_from(&oci).unwrap();
|
||||
let mut linux_cpu = oci::LinuxCpu::default();
|
||||
linux_cpu.set_shares(Some(2048));
|
||||
linux_cpu.set_cpus(Some("1".to_string()));
|
||||
linux_cpu.set_mems(Some("1-2".to_string()));
|
||||
|
||||
let resources = LinuxContainerCpuResources::try_from(&linux_cpu).unwrap();
|
||||
assert_eq!(resources.shares(), 2048);
|
||||
assert_eq!(resources.period(), 0);
|
||||
assert_eq!(resources.quota(), -1);
|
||||
@@ -217,16 +212,14 @@ mod tests {
|
||||
assert!(sandbox.cpuset().is_empty());
|
||||
assert!(sandbox.nodeset().is_empty());
|
||||
|
||||
let oci = oci::LinuxCpu {
|
||||
shares: Some(2048),
|
||||
quota: Some(1001),
|
||||
period: Some(100),
|
||||
realtime_runtime: None,
|
||||
realtime_period: None,
|
||||
cpus: "1,2,3".to_string(),
|
||||
mems: "1".to_string(),
|
||||
};
|
||||
let resources = LinuxContainerCpuResources::try_from(&oci).unwrap();
|
||||
let mut linux_cpu = oci::LinuxCpu::default();
|
||||
linux_cpu.set_shares(Some(2048));
|
||||
linux_cpu.set_quota(Some(1001));
|
||||
linux_cpu.set_period(Some(100));
|
||||
linux_cpu.set_cpus(Some("1,2,3".to_string()));
|
||||
linux_cpu.set_mems(Some("1".to_string()));
|
||||
|
||||
let resources = LinuxContainerCpuResources::try_from(&linux_cpu).unwrap();
|
||||
sandbox.merge(&resources);
|
||||
assert_eq!(sandbox.shares(), 1024);
|
||||
assert_eq!(sandbox.get_vcpus(), 11);
|
||||
@@ -234,16 +227,12 @@ mod tests {
|
||||
assert_eq!(sandbox.cpuset().len(), 3);
|
||||
assert_eq!(sandbox.nodeset().len(), 1);
|
||||
|
||||
let oci = oci::LinuxCpu {
|
||||
shares: Some(2048),
|
||||
quota: None,
|
||||
period: None,
|
||||
realtime_runtime: None,
|
||||
realtime_period: None,
|
||||
cpus: "1,4".to_string(),
|
||||
mems: "1-2".to_string(),
|
||||
};
|
||||
let resources = LinuxContainerCpuResources::try_from(&oci).unwrap();
|
||||
let mut linux_cpu = oci::LinuxCpu::default();
|
||||
linux_cpu.set_shares(Some(2048));
|
||||
linux_cpu.set_cpus(Some("1,4".to_string()));
|
||||
linux_cpu.set_mems(Some("1-2".to_string()));
|
||||
|
||||
let resources = LinuxContainerCpuResources::try_from(&linux_cpu).unwrap();
|
||||
sandbox.merge(&resources);
|
||||
|
||||
assert_eq!(sandbox.shares(), 1024);
|
||||
|
@@ -8,8 +8,8 @@ use std::path::Path;
|
||||
|
||||
use crate::annotations;
|
||||
use crate::container::ContainerType;
|
||||
use oci_spec::runtime as oci;
|
||||
use std::str::FromStr;
|
||||
|
||||
// K8S_EMPTY_DIR is the K8s specific path for `empty-dir` volumes
|
||||
const K8S_EMPTY_DIR: &str = "kubernetes.io~empty-dir";
|
||||
// K8S_CONFIGMAP is the K8s specific path for `configmap` volumes
|
||||
@@ -62,9 +62,11 @@ pub fn container_type(spec: &oci::Spec) -> ContainerType {
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
if let Some(v) = spec.annotations.get(k.to_owned()) {
|
||||
if let Ok(t) = ContainerType::from_str(v) {
|
||||
return t;
|
||||
if let Some(annotations) = spec.annotations() {
|
||||
if let Some(v) = annotations.get(k.to_owned()) {
|
||||
if let Ok(t) = ContainerType::from_str(v) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,8 +82,10 @@ pub fn container_name(spec: &oci::Spec) -> String {
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
if let Some(v) = spec.annotations.get(k.to_owned()) {
|
||||
return v.clone();
|
||||
if let Some(annotations) = spec.annotations() {
|
||||
if let Some(v) = annotations.get(k.to_owned()) {
|
||||
return v.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,9 +106,11 @@ pub fn container_type_with_id(spec: &oci::Spec) -> (ContainerType, Option<String
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
if let Some(id) = spec.annotations.get(k.to_owned()) {
|
||||
sid = Some(id.to_string());
|
||||
break;
|
||||
if let Some(annotations) = spec.annotations() {
|
||||
if let Some(id) = annotations.get(k.to_owned()) {
|
||||
sid = Some(id.to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,96 +333,108 @@ mod tests {
|
||||
);
|
||||
|
||||
// crio sandbox
|
||||
spec.annotations = [(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::SANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::SANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodSandbox, None)
|
||||
);
|
||||
|
||||
// cri containerd sandbox
|
||||
spec.annotations = [(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::POD_SANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::POD_SANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodSandbox, None)
|
||||
);
|
||||
|
||||
// docker shim sandbox
|
||||
spec.annotations = [(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::PODSANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::PODSANDBOX.to_string(),
|
||||
)]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodSandbox, None)
|
||||
);
|
||||
|
||||
// crio container
|
||||
spec.annotations = [
|
||||
(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::crio::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[
|
||||
(
|
||||
annotations::crio::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::crio::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodContainer, Some(sid.clone()))
|
||||
);
|
||||
|
||||
// cri containerd container
|
||||
spec.annotations = [
|
||||
(
|
||||
annotations::cri_containerd::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::POD_CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::cri_containerd::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[
|
||||
(
|
||||
annotations::cri_containerd::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::POD_CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::cri_containerd::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodContainer, Some(sid.clone()))
|
||||
);
|
||||
|
||||
// docker shim container
|
||||
spec.annotations = [
|
||||
(
|
||||
annotations::dockershim::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::dockershim::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
spec.set_annotations(Some(
|
||||
[
|
||||
(
|
||||
annotations::dockershim::CONTAINER_TYPE_LABEL_KEY.to_string(),
|
||||
container::CONTAINER.to_string(),
|
||||
),
|
||||
(
|
||||
annotations::dockershim::SANDBOX_ID_LABEL_KEY.to_string(),
|
||||
sid.clone(),
|
||||
),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
));
|
||||
assert_eq!(
|
||||
container_type_with_id(&spec),
|
||||
(ContainerType::PodContainer, Some(sid))
|
||||
|
Reference in New Issue
Block a user