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