Skip to content

Commit

Permalink
Merge pull request kata-containers#10349 from lifupan/main_nsandboxapi
Browse files Browse the repository at this point in the history
sandbox: refactor the sandbox init process
  • Loading branch information
justxuewei authored Sep 27, 2024
2 parents 3911bd3 + f7bc627 commit 11b1a72
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 137 deletions.
51 changes: 37 additions & 14 deletions src/runtime-rs/crates/resource/src/cpu_mem/initial_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::convert::TryFrom;
use std::{collections::HashMap, convert::TryFrom};

use anyhow::{Context, Result};
use kata_types::{
Expand All @@ -22,6 +22,34 @@ struct InitialSize {
orig_toml_default_mem: u32,
}

// generate initial resource(vcpu and memory in MiB) from annotations
impl TryFrom<&HashMap<String, String>> for InitialSize {
type Error = anyhow::Error;
fn try_from(an: &HashMap<String, String>) -> Result<Self> {
let mut vcpu: u32 = 0;

let annotation = Annotation::new(an.clone());
let (period, quota, memory) =
get_sizing_info(annotation).context("failed to get sizing info")?;
let mut cpu = oci::LinuxCpu::default();
cpu.set_period(Some(period));
cpu.set_quota(Some(quota));

// although it may not be actually a linux container, we are only using the calculation inside
// LinuxContainerCpuResources::try_from to generate our vcpu number
if let Ok(cpu_resource) = LinuxContainerCpuResources::try_from(&cpu) {
vcpu = get_nr_vcpu(&cpu_resource);
}
let mem_mb = convert_memory_to_mb(memory);

Ok(Self {
vcpu,
mem_mb,
orig_toml_default_mem: 0,
})
}
}

// generate initial resource(vcpu and memory in MiB) from spec's information
impl TryFrom<&oci::Spec> for InitialSize {
type Error = anyhow::Error;
Expand All @@ -32,19 +60,7 @@ impl TryFrom<&oci::Spec> for InitialSize {
// podsandbox, from annotation
ContainerType::PodSandbox => {
let spec_annos = spec.annotations().clone().unwrap_or_default();
let annotation = Annotation::new(spec_annos);
let (period, quota, memory) =
get_sizing_info(annotation).context("failed to get sizing info")?;
let mut cpu = oci::LinuxCpu::default();
cpu.set_period(Some(period));
cpu.set_quota(Some(quota));

// although it may not be actually a linux container, we are only using the calculation inside
// LinuxContainerCpuResources::try_from to generate our vcpu number
if let Ok(cpu_resource) = LinuxContainerCpuResources::try_from(&cpu) {
vcpu = get_nr_vcpu(&cpu_resource);
}
mem_mb = convert_memory_to_mb(memory);
return InitialSize::try_from(&spec_annos);
}
// single container, from container spec
_ => {
Expand Down Expand Up @@ -107,6 +123,13 @@ impl InitialSizeManager {
})
}

pub fn new_from(annotation: &HashMap<String, String>) -> Result<Self> {
Ok(Self {
resource: InitialSize::try_from(annotation)
.context("failed to construct static resource")?,
})
}

pub fn setup_config(&mut self, config: &mut TomlConfig) -> Result<()> {
// update this data to the hypervisor config for later use by hypervisor
let hypervisor_name = &config.runtime.hypervisor_name;
Expand Down
3 changes: 2 additions & 1 deletion src/runtime-rs/crates/runtimes/common/src/runtime_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::sync::Arc;

use crate::{message::Message, ContainerManager, Sandbox};
use crate::{message::Message, types::SandboxConfig, ContainerManager, Sandbox};
use anyhow::Result;
use async_trait::async_trait;
use kata_types::config::TomlConfig;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub trait RuntimeHandler: Send + Sync {
msg_sender: Sender<Message>,
config: Arc<TomlConfig>,
init_size_manager: InitialSizeManager,
sandbox_config: SandboxConfig,
) -> Result<RuntimeInstance>;

fn cleanup(&self, id: &str) -> Result<()>;
Expand Down
10 changes: 1 addition & 9 deletions src/runtime-rs/crates/runtimes/common/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use crate::{types::ContainerProcess, ContainerManager};
use anyhow::Result;
use async_trait::async_trait;
use oci_spec::runtime as oci;
use runtime_spec as spec;
use std::sync::Arc;

#[derive(Clone)]
Expand All @@ -28,13 +26,7 @@ impl std::fmt::Debug for SandboxNetworkEnv {

#[async_trait]
pub trait Sandbox: Send + Sync {
async fn start(
&self,
dns: Vec<String>,
spec: &oci::Spec,
state: &spec::State,
network_env: SandboxNetworkEnv,
) -> Result<()>;
async fn start(&self) -> Result<()>;
async fn stop(&self) -> Result<()>;
async fn cleanup(&self) -> Result<()>;
async fn shutdown(&self) -> Result<()>;
Expand Down
19 changes: 18 additions & 1 deletion src/runtime-rs/crates/runtimes/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ mod trans_into_agent;
mod trans_into_shim;
pub mod utils;

use std::fmt;
use std::{
collections::{hash_map::RandomState, HashMap},
fmt,
};

use crate::SandboxNetworkEnv;

use anyhow::{Context, Result};
use kata_sys_util::validate;
use kata_types::mount::Mount;
use oci_spec::runtime as oci;
use strum::Display;

/// TaskRequest: TaskRequest from shim
Expand Down Expand Up @@ -135,6 +141,17 @@ pub struct ContainerConfig {
pub stderr: Option<String>,
}

#[derive(Clone, Debug)]
pub struct SandboxConfig {
pub sandbox_id: String,
pub hostname: String,
pub dns: Vec<String>,
pub network_env: SandboxNetworkEnv,
pub annotations: HashMap<String, String, RandomState>,
pub hooks: Option<oci::Hooks>,
pub state: runtime_spec::State,
}

#[derive(Debug, Clone)]
pub struct PID {
pub pid: u32,
Expand Down
3 changes: 2 additions & 1 deletion src/runtime-rs/crates/runtimes/linux_container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use common::{message::Message, RuntimeHandler, RuntimeInstance};
use common::{message::Message, types::SandboxConfig, RuntimeHandler, RuntimeInstance};
use kata_types::config::TomlConfig;
use resource::cpu_mem::initial_size::InitialSizeManager;
use tokio::sync::mpsc::Sender;
Expand All @@ -34,6 +34,7 @@ impl RuntimeHandler for LinuxContainer {
_msg_sender: Sender<Message>,
_config: Arc<TomlConfig>,
_init_size_manager: InitialSizeManager,
_sandbox_config: SandboxConfig,
) -> Result<RuntimeInstance> {
todo!()
}
Expand Down
Loading

0 comments on commit 11b1a72

Please sign in to comment.