diff --git a/src/runtime-rs/crates/hypervisor/src/ch/mod.rs b/src/runtime-rs/crates/hypervisor/src/ch/mod.rs index adac88a15e..37df445855 100644 --- a/src/runtime-rs/crates/hypervisor/src/ch/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/ch/mod.rs @@ -11,6 +11,7 @@ use async_trait::async_trait; use kata_types::capabilities::{Capabilities, CapabilityBits}; use kata_types::config::hypervisor::Hypervisor as HypervisorConfig; use persist::sandbox_persist::Persist; +use std::collections::HashMap; use std::sync::Arc; use tokio::sync::{mpsc, Mutex, RwLock}; @@ -59,7 +60,12 @@ impl Default for CloudHypervisor { #[async_trait] impl Hypervisor for CloudHypervisor { - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()> { + async fn prepare_vm( + &self, + id: &str, + netns: Option, + _annotations: &HashMap, + ) -> Result<()> { let mut inner = self.inner.write().await; inner.prepare_vm(id, netns).await } diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs index 36ac15f932..b3413000e7 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs @@ -12,6 +12,7 @@ use inner::DragonballInner; use persist::sandbox_persist::Persist; pub mod vmm_instance; +use std::collections::HashMap; use std::sync::Arc; use anyhow::{Context, Result}; @@ -69,7 +70,12 @@ impl Dragonball { #[async_trait] impl Hypervisor for Dragonball { #[instrument] - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()> { + async fn prepare_vm( + &self, + id: &str, + netns: Option, + _annotations: &HashMap, + ) -> Result<()> { let mut inner = self.inner.write().await; inner.prepare_vm(id, netns).await } diff --git a/src/runtime-rs/crates/hypervisor/src/firecracker/mod.rs b/src/runtime-rs/crates/hypervisor/src/firecracker/mod.rs index 3b65eadd88..989b96c7a8 100644 --- a/src/runtime-rs/crates/hypervisor/src/firecracker/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/firecracker/mod.rs @@ -18,6 +18,7 @@ use inner::FcInner; use kata_types::capabilities::Capabilities; use kata_types::capabilities::CapabilityBits; use persist::sandbox_persist::Persist; +use std::collections::HashMap; use std::sync::Arc; use tokio::sync::mpsc; use tokio::sync::Mutex; @@ -58,7 +59,12 @@ impl Firecracker { #[async_trait] impl Hypervisor for Firecracker { - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()> { + async fn prepare_vm( + &self, + id: &str, + netns: Option, + _annotations: &HashMap, + ) -> Result<()> { let mut inner = self.inner.write().await; inner.prepare_vm(id, netns).await } diff --git a/src/runtime-rs/crates/hypervisor/src/lib.rs b/src/runtime-rs/crates/hypervisor/src/lib.rs index a56f2c9d58..4cb601b752 100644 --- a/src/runtime-rs/crates/hypervisor/src/lib.rs +++ b/src/runtime-rs/crates/hypervisor/src/lib.rs @@ -97,7 +97,12 @@ pub struct MemoryConfig { #[async_trait] pub trait Hypervisor: std::fmt::Debug + Send + Sync { // vm manager - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()>; + async fn prepare_vm( + &self, + id: &str, + netns: Option, + annotations: &HashMap, + ) -> Result<()>; async fn start_vm(&self, timeout: i32) -> Result<()>; async fn stop_vm(&self) -> Result<()>; async fn wait_vm(&self) -> Result; diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs b/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs index 6a6c923cab..decd776fde 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/mod.rs @@ -18,6 +18,7 @@ use persist::sandbox_persist::Persist; use anyhow::{Context, Result}; use async_trait::async_trait; +use std::collections::HashMap; use std::sync::Arc; use tokio::sync::RwLock; use tokio::sync::{mpsc, Mutex}; @@ -52,7 +53,12 @@ impl Qemu { #[async_trait] impl Hypervisor for Qemu { - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()> { + async fn prepare_vm( + &self, + id: &str, + netns: Option, + _annotations: &HashMap, + ) -> Result<()> { let mut inner = self.inner.write().await; inner.prepare_vm(id, netns).await } diff --git a/src/runtime-rs/crates/hypervisor/src/remote/inner.rs b/src/runtime-rs/crates/hypervisor/src/remote/inner.rs index 81e193794a..2c4e7caf80 100644 --- a/src/runtime-rs/crates/hypervisor/src/remote/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/remote/inner.rs @@ -9,7 +9,14 @@ use crate::{ use crate::{MemoryConfig, VcpuThreadIds}; use anyhow::{Context, Result}; use async_trait::async_trait; -use kata_types::capabilities::{Capabilities, CapabilityBits}; +use kata_types::{ + annotations::{ + cri_containerd::{SANDBOX_NAMESPACE_LABEL_KEY, SANDBOX_NAME_LABEL_KEY}, + KATA_ANNO_CFG_HYPERVISOR_DEFAULT_MEMORY, KATA_ANNO_CFG_HYPERVISOR_DEFAULT_VCPUS, + KATA_ANNO_CFG_HYPERVISOR_IMAGE_PATH, KATA_ANNO_CFG_HYPERVISOR_MACHINE_TYPE, + }, + capabilities::{Capabilities, CapabilityBits}, +}; use persist::sandbox_persist::Persist; use protocols::{ remote::{CreateVMRequest, StartVMRequest, StopVMRequest}, @@ -30,8 +37,6 @@ pub struct RemoteInner { pub(crate) config: HypervisorConfig, /// agent socket path pub(crate) agent_socket_path: String, - /// sandbox annotations - pub(crate) annotations: HashMap, /// netns path pub(crate) netns: Option, /// hypervisor unix client @@ -47,7 +52,6 @@ impl std::fmt::Debug for RemoteInner { .field("id", &self.id) .field("config", &self.config) .field("agent_socket_path", &self.agent_socket_path) - .field("annotations", &self.annotations) .field("netns", &self.netns) .finish() } @@ -61,7 +65,6 @@ impl RemoteInner { id: "".to_string(), config: HypervisorConfig::default(), agent_socket_path: "".to_string(), - annotations: HashMap::new(), netns: None, client: None, @@ -85,7 +88,51 @@ impl RemoteInner { } } - pub(crate) async fn prepare_vm(&mut self, id: &str, netns: Option) -> Result<()> { + fn prepare_annotations( + &self, + oci_annotations: &HashMap, + ) -> HashMap { + let mut annotations: HashMap = HashMap::new(); + let config = &self.config; + annotations.insert( + SANDBOX_NAME_LABEL_KEY.to_string(), + oci_annotations + .get(SANDBOX_NAME_LABEL_KEY) + .cloned() + .unwrap_or_default(), + ); + annotations.insert( + SANDBOX_NAMESPACE_LABEL_KEY.to_string(), + oci_annotations + .get(SANDBOX_NAMESPACE_LABEL_KEY) + .cloned() + .unwrap_or_default(), + ); + annotations.insert( + KATA_ANNO_CFG_HYPERVISOR_MACHINE_TYPE.to_string(), + config.machine_info.machine_type.to_string(), + ); + annotations.insert( + KATA_ANNO_CFG_HYPERVISOR_DEFAULT_VCPUS.to_string(), + config.cpu_info.default_vcpus.to_string(), + ); + annotations.insert( + KATA_ANNO_CFG_HYPERVISOR_DEFAULT_MEMORY.to_string(), + config.memory_info.default_memory.to_string(), + ); + annotations.insert( + KATA_ANNO_CFG_HYPERVISOR_IMAGE_PATH.to_string(), + config.boot_info.image.to_string(), + ); + annotations + } + + pub(crate) async fn prepare_vm( + &mut self, + id: &str, + netns: Option, + annotations: &HashMap, + ) -> Result<()> { info!(sl!(), "Preparing REMOTE VM"); self.id = id.to_string(); @@ -99,7 +146,7 @@ impl RemoteInner { let ctx = context::Context::default(); let req = CreateVMRequest { id: id.to_string(), - annotations: self.annotations.clone(), + annotations: self.prepare_annotations(annotations), networkNamespacePath: netns.clone().unwrap_or_default(), ..Default::default() }; @@ -181,12 +228,12 @@ impl RemoteInner { pub(crate) async fn remove_device(&self, _device: DeviceType) -> Result<()> { warn!(sl!(), "RemoteInner::remove_device(): NOT YET IMPLEMENTED"); - todo!() + Ok(()) } pub(crate) async fn update_device(&self, _device: DeviceType) -> Result<()> { warn!(sl!(), "RemoteInner::update_device(): NOT YET IMPLEMENTED"); - todo!() + Ok(()) } pub(crate) async fn get_agent_socket(&self) -> Result { @@ -331,7 +378,6 @@ impl Persist for RemoteInner { id: hypervisor_state.id, config: hypervisor_state.config, agent_socket_path: "".to_string(), - annotations: HashMap::new(), netns: hypervisor_state.netns, client: None, exit_notify: Some(exit_notify), diff --git a/src/runtime-rs/crates/hypervisor/src/remote/mod.rs b/src/runtime-rs/crates/hypervisor/src/remote/mod.rs index 41932a2e6a..9650b7d2cd 100644 --- a/src/runtime-rs/crates/hypervisor/src/remote/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/remote/mod.rs @@ -10,6 +10,8 @@ use async_trait::async_trait; use inner::RemoteInner; use kata_types::capabilities::{Capabilities, CapabilityBits}; use persist::sandbox_persist::Persist; +use std::collections::HashMap; + use std::sync::Arc; use tokio::sync::RwLock; @@ -41,9 +43,14 @@ impl Remote { #[async_trait] impl Hypervisor for Remote { - async fn prepare_vm(&self, id: &str, netns: Option) -> Result<()> { + async fn prepare_vm( + &self, + id: &str, + netns: Option, + annotations: &HashMap, + ) -> Result<()> { let mut inner = self.inner.write().await; - inner.prepare_vm(id, netns).await + inner.prepare_vm(id, netns, annotations).await } async fn start_vm(&self, timeout: i32) -> Result<()> { diff --git a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs index 56126161ab..86491febe6 100644 --- a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs +++ b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs @@ -19,6 +19,7 @@ use containerd_shim_protos::events::task::{TaskExit, TaskOOM}; use hypervisor::VsockConfig; #[cfg(not(target_arch = "s390x"))] use hypervisor::HYPERVISOR_FIRECRACKER; +use hypervisor::HYPERVISOR_REMOTE; #[cfg(all(feature = "dragonball", not(target_arch = "s390x")))] use hypervisor::{dragonball::Dragonball, HYPERVISOR_DRAGONBALL}; use hypervisor::{qemu::Qemu, HYPERVISOR_QEMU}; @@ -326,7 +327,11 @@ impl Sandbox for VirtSandbox { } self.hypervisor - .prepare_vm(id, sandbox_config.network_env.netns.clone()) + .prepare_vm( + id, + sandbox_config.network_env.netns.clone(), + &sandbox_config.annotations, + ) .await .context("prepare vm")?; @@ -649,6 +654,7 @@ impl Persist for VirtSandbox { #[cfg(not(target_arch = "s390x"))] HYPERVISOR_FIRECRACKER => Ok(Some(hypervisor_state)), HYPERVISOR_QEMU => Ok(Some(hypervisor_state)), + HYPERVISOR_REMOTE => Ok(Some(hypervisor_state)), _ => Err(anyhow!( "Unsupported hypervisor {}", hypervisor_state.hypervisor_type