runtime-rs: add oci spec for prepare_vm method

The cloud-api-adaptor needs to support different types of pod VM
instance.
We needs to pass some annotations like machine_type, default_vcpus and
default_memory to prepare the VMs.

Signed-off-by: Chasing1020 <643601464@qq.com>
This commit is contained in:
Chasing1020 2024-10-15 14:30:14 +08:00
parent f1167645f3
commit 425f6ad4e6
8 changed files with 106 additions and 18 deletions

View File

@ -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<String>) -> Result<()> {
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
_annotations: &HashMap<String, String>,
) -> Result<()> {
let mut inner = self.inner.write().await;
inner.prepare_vm(id, netns).await
}

View File

@ -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<String>) -> Result<()> {
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
_annotations: &HashMap<String, String>,
) -> Result<()> {
let mut inner = self.inner.write().await;
inner.prepare_vm(id, netns).await
}

View File

@ -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<String>) -> Result<()> {
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
_annotations: &HashMap<String, String>,
) -> Result<()> {
let mut inner = self.inner.write().await;
inner.prepare_vm(id, netns).await
}

View File

@ -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<String>) -> Result<()>;
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
annotations: &HashMap<String, String>,
) -> Result<()>;
async fn start_vm(&self, timeout: i32) -> Result<()>;
async fn stop_vm(&self) -> Result<()>;
async fn wait_vm(&self) -> Result<i32>;

View File

@ -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<String>) -> Result<()> {
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
_annotations: &HashMap<String, String>,
) -> Result<()> {
let mut inner = self.inner.write().await;
inner.prepare_vm(id, netns).await
}

View File

@ -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<String, String>,
/// netns path
pub(crate) netns: Option<String>,
/// 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<String>) -> Result<()> {
fn prepare_annotations(
&self,
oci_annotations: &HashMap<String, String>,
) -> HashMap<String, String> {
let mut annotations: HashMap<String, String> = 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<String>,
annotations: &HashMap<String, String>,
) -> 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<String> {
@ -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),

View File

@ -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<String>) -> Result<()> {
async fn prepare_vm(
&self,
id: &str,
netns: Option<String>,
annotations: &HashMap<String, String>,
) -> 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<()> {

View File

@ -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