libs: update configuration and annotations

1. support annotation for runtime.name, hypervisor_name, agent_name.
2. fix parse memory from annotation

Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
Zhongtao Hu 2022-06-09 08:03:04 +08:00 committed by quanwei.zqw
parent f3335c99ce
commit 3f6123b4dd
8 changed files with 128 additions and 86 deletions

19
src/libs/Cargo.lock generated
View File

@ -46,6 +46,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "byte-unit"
version = "3.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "415301c9de11005d4b92193c0eb7ac7adc37e5a49e0ac9bed0a42343512744b8"
[[package]]
name = "byteorder"
version = "1.4.3"
@ -388,6 +394,7 @@ dependencies = [
name = "kata-types"
version = "0.1.0"
dependencies = [
"byte-unit",
"glob",
"lazy_static",
"num_cpus",
@ -670,9 +677,9 @@ dependencies = [
[[package]]
name = "protobuf"
version = "2.14.0"
version = "2.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e86d370532557ae7573551a1ec8235a0f8d6cb276c7c9e6aa490b511c447485"
checksum = "cf7e6d18738ecd0902d30d1ad232c9125985a3422929b16c65517b38adc14f96"
dependencies = [
"serde",
"serde_derive",
@ -680,18 +687,18 @@ dependencies = [
[[package]]
name = "protobuf-codegen"
version = "2.14.0"
version = "2.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de113bba758ccf2c1ef816b127c958001b7831136c9bc3f8e9ec695ac4e82b0c"
checksum = "aec1632b7c8f2e620343439a7dfd1f3c47b18906c4be58982079911482b5d707"
dependencies = [
"protobuf",
]
[[package]]
name = "protobuf-codegen-pure"
version = "2.14.0"
version = "2.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d1a4febc73bf0cada1d77c459a0c8e5973179f1cfd5b0f1ab789d45b17b6440"
checksum = "9f8122fdb18e55190c796b088a16bdb70cd7acdcd48f7a8b796b58c62e532cc6"
dependencies = [
"protobuf",
"protobuf-codegen",

View File

@ -11,6 +11,7 @@ license = "Apache-2.0"
edition = "2018"
[dependencies]
byte-unit = "3.1.4"
glob = "0.3.0"
lazy_static = "1.4.0"
num_cpus = "1.13.1"

View File

@ -14,6 +14,7 @@ use serde::Deserialize;
use crate::config::hypervisor::get_hypervisor_plugin;
use crate::config::TomlConfig;
use crate::sl;
/// CRI-containerd specific annotations.
pub mod cri_containerd;
@ -274,6 +275,13 @@ pub const KATA_ANNO_CFG_HYPERVISOR_MSIZE_9P: &str = "io.katacontainers.config.hy
// Runtime related annotations
/// Prefix for Runtime configurations.
pub const KATA_ANNO_CFG_RUNTIME_PREFIX: &str = "io.katacontainers.config.runtime.";
/// runtime name
pub const KATA_ANNO_CFG_RUNTIME_NAME: &str = "io.katacontainers.config.runtime.name";
/// hypervisor name
pub const KATA_ANNO_CFG_RUNTIME_HYPERVISOR: &str =
"io.katacontainers.config.runtime.hypervisor_name";
/// agent name
pub const KATA_ANNO_CFG_RUNTIME_AGENT: &str = "io.katacontainers.config.runtime.agent_name";
/// A sandbox annotation that determines if seccomp should be applied inside guest.
pub const KATA_ANNO_CFG_DISABLE_GUEST_SECCOMP: &str =
"io.katacontainers.config.runtime.disable_guest_seccomp";
@ -396,30 +404,24 @@ impl Annotation {
impl Annotation {
/// update config info by annotation
pub fn update_config_by_annotation(
&self,
config: &mut TomlConfig,
hypervisor_name: &str,
agent_name: &str,
) -> Result<()> {
if config.hypervisor.get_mut(hypervisor_name).is_none() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("hypervisor {} not found", hypervisor_name),
));
pub fn update_config_by_annotation(&self, config: &mut TomlConfig) -> Result<()> {
if let Some(hv) = self.annotations.get(KATA_ANNO_CFG_RUNTIME_HYPERVISOR) {
if config.hypervisor.get(hv).is_some() {
config.runtime.hypervisor_name = hv.to_string();
}
}
if let Some(ag) = self.annotations.get(KATA_ANNO_CFG_RUNTIME_AGENT) {
if config.agent.get(ag).is_some() {
config.runtime.agent_name = ag.to_string();
}
}
let hypervisor_name = &config.runtime.hypervisor_name;
let agent_name = &config.runtime.agent_name;
if config.agent.get_mut(agent_name).is_none() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("agent {} not found", agent_name),
));
}
let bool_err = io::Error::new(io::ErrorKind::InvalidData, "parse bool error".to_string());
let u32_err = io::Error::new(io::ErrorKind::InvalidData, "parse u32 error".to_string());
let u64_err = io::Error::new(io::ErrorKind::InvalidData, "parse u64 error".to_string());
let i32_err = io::Error::new(io::ErrorKind::InvalidData, "parse i32 error".to_string());
let mut hv = config.hypervisor.get_mut(hypervisor_name).unwrap();
let mut ag = config.agent.get_mut(agent_name).unwrap();
for (key, value) in &self.annotations {
@ -632,32 +634,40 @@ impl Annotation {
hv.machine_info.entropy_source = value.to_string();
}
// Hypervisor Memory related annotations
KATA_ANNO_CFG_HYPERVISOR_DEFAULT_MEMORY => match self.get_value::<u32>(key) {
Ok(r) => {
let mem = r.unwrap_or_default();
if mem
< get_hypervisor_plugin(hypervisor_name)
.unwrap()
.get_min_memory()
{
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Memory specified in annotation {} is less than minimum required {}",
mem,
get_hypervisor_plugin(hypervisor_name)
.unwrap()
.get_min_memory()
),
));
} else {
hv.memory_info.default_memory = mem;
KATA_ANNO_CFG_HYPERVISOR_DEFAULT_MEMORY => {
match byte_unit::Byte::from_str(value) {
Ok(mem_bytes) => {
let memory_size = mem_bytes
.get_adjusted_unit(byte_unit::ByteUnit::MiB)
.get_value()
as u32;
info!(sl!(), "get mem {} from annotations: {}", memory_size, value);
if memory_size
< get_hypervisor_plugin(hypervisor_name)
.unwrap()
.get_min_memory()
{
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"memory specified in annotation {} is less than minimum limitation {}",
memory_size,
get_hypervisor_plugin(hypervisor_name)
.unwrap()
.get_min_memory()
),
));
}
hv.memory_info.default_memory = memory_size;
}
Err(error) => {
error!(
sl!(),
"failed to parse byte from string {} error {:?}", value, error
);
}
}
Err(_e) => {
return Err(u32_err);
}
},
}
KATA_ANNO_CFG_HYPERVISOR_MEMORY_SLOTS => match self.get_value::<u32>(key) {
Ok(v) => {
hv.memory_info.memory_slots = v.unwrap_or_default();
@ -829,7 +839,21 @@ impl Annotation {
return Err(u32_err);
}
},
//update runtume config
//update runtime config
KATA_ANNO_CFG_RUNTIME_NAME => {
let runtime = vec!["virt-container", "linux-container", "wasm-container"];
if runtime.contains(&value.as_str()) {
config.runtime.name = value.to_string();
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"runtime specified in annotation {} is not in {:?}",
&value, &runtime
),
));
}
}
KATA_ANNO_CFG_DISABLE_GUEST_SECCOMP => match self.get_value::<bool>(key) {
Ok(r) => {
config.runtime.disable_guest_seccomp = r.unwrap_or_default();
@ -876,10 +900,7 @@ impl Annotation {
config.runtime.vfio_mode = value.to_string();
}
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Annotation {} not enabled", key),
));
warn!(sl!(), "Annotation {} not enabled", key);
}
}
}

View File

@ -21,6 +21,10 @@ pub struct Runtime {
#[serde(default)]
pub hypervisor_name: String,
/// Agent name
#[serde(default)]
pub agent_name: String,
/// If enabled, the runtime will log additional debug messages to the system log.
#[serde(default, rename = "enable_debug")]
pub debug: bool,

View File

@ -18,6 +18,7 @@ mod tests {
KATA_ANNO_CFG_HYPERVISOR_PATH, KATA_ANNO_CFG_HYPERVISOR_VHOSTUSER_STORE_PATH,
KATA_ANNO_CFG_HYPERVISOR_VIRTIO_FS_DAEMON, KATA_ANNO_CFG_HYPERVISOR_VIRTIO_FS_EXTRA_ARGS,
KATA_ANNO_CFG_HYPERVISOR_VIRTIO_MEM, KATA_ANNO_CFG_KERNEL_MODULES,
KATA_ANNO_CFG_RUNTIME_NAME,
};
use kata_types::config::KataConfig;
use kata_types::config::{QemuConfig, TomlConfig};
@ -118,7 +119,7 @@ mod tests {
);
anno_hash.insert(
KATA_ANNO_CFG_HYPERVISOR_DEFAULT_MEMORY.to_string(),
"100".to_string(),
"100MiB".to_string(),
);
anno_hash.insert(
KATA_ANNO_CFG_HYPERVISOR_ENABLE_IO_THREADS.to_string(),
@ -169,9 +170,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_ok());
assert!(anno.update_config_by_annotation(&mut config).is_ok());
KataConfig::set_active_config(Some(config), "qemu", "agnet0");
if let Some(ag) = KataConfig::get_default_config().get_agent() {
assert_eq!(
@ -292,9 +291,10 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_ok());
if let Some(hv) = KataConfig::get_default_config().get_hypervisor() {
assert_eq!(hv.blockdev_info.block_device_driver, "virtio-blk");
}
}
#[test]
@ -315,9 +315,10 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_ok());
if let Some(hv) = KataConfig::get_default_config().get_hypervisor() {
assert!(hv.memory_info.enable_guest_swap)
}
}
#[test]
@ -341,9 +342,7 @@ mod tests {
let path = Path::new(path).join("tests/texture/configuration-anno-0.toml");
let content = fs::read_to_string(&path).unwrap();
let mut config = TomlConfig::load(&content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -366,9 +365,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(&content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -388,9 +385,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -411,9 +406,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -434,9 +427,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -457,9 +448,7 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
@ -480,8 +469,25 @@ mod tests {
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno
.update_config_by_annotation(&mut config, "qemu", "agent0")
.is_err());
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
#[test]
fn test_fail_to_change_runtime_name() {
let content = include_str!("texture/configuration-anno-0.toml");
let qemu = QemuConfig::new();
qemu.register();
let config = TomlConfig::load(content).unwrap();
KataConfig::set_active_config(Some(config), "qemu", "agent0");
let mut anno_hash = HashMap::new();
anno_hash.insert(
KATA_ANNO_CFG_RUNTIME_NAME.to_string(),
"other-container".to_string(),
);
let anno = Annotation::new(anno_hash);
let mut config = TomlConfig::load(content).unwrap();
assert!(anno.update_config_by_annotation(&mut config).is_err());
}
}

View File

@ -84,5 +84,7 @@ sandbox_bind_mounts=["/proc/self"]
vfio_mode="vfio"
experimental=["a", "b"]
enable_pprof = true
hypervisor_name = "qemu"
agent_name = "agent0"

View File

@ -83,5 +83,6 @@ sandbox_bind_mounts=["/proc/self"]
vfio_mode="vfio"
experimental=["a", "b"]
enable_pprof = true
hypervisor_name = "qemu"
agent_name = "agent0"

View File

@ -12,7 +12,7 @@ serde_json = "1.0.73"
# - Dynamic keys required to allow HashMap keys to be slog::Serialized.
# - The 'max_*' features allow changing the log level at runtime
# (by stopping the compiler from removing log calls).
slog = { version = "2.7.0", features = ["dynamic-keys", "max_level_trace", "release_max_level_debug"] }
slog = { version = "2.5.2", features = ["dynamic-keys", "max_level_trace", "release_max_level_debug"] }
slog-json = "2.4.0"
slog-async = "2.7.0"
slog-scope = "4.4.0"