sandbox: set the dns for the sandbox

The rust agent had supported to set the guest dns
server in start sandbox request, thus add the dns
in the runtime side.

Fixes:#6286

Signed-off-by: Fupan Li <fupan.lfp@antgroup.com>
This commit is contained in:
Fupan Li 2023-02-15 11:19:41 +08:00
parent 32ebe1895b
commit 04e930073c
4 changed files with 20 additions and 6 deletions

View File

@ -42,6 +42,7 @@ pub const MIN_SHARED_9PFS_SIZE_MB: u32 = 4 * 1024;
pub const MAX_SHARED_9PFS_SIZE_MB: u32 = 8 * 1024 * 1024; pub const MAX_SHARED_9PFS_SIZE_MB: u32 = 8 * 1024 * 1024;
pub const DEFAULT_GUEST_HOOK_PATH: &str = "/opt/kata/hooks"; pub const DEFAULT_GUEST_HOOK_PATH: &str = "/opt/kata/hooks";
pub const DEFAULT_GUEST_DNS_FILE: &str = "/etc/resolv.conf";
pub const DEFAULT_GUEST_VCPUS: u32 = 1; pub const DEFAULT_GUEST_VCPUS: u32 = 1;

View File

@ -9,7 +9,7 @@ use async_trait::async_trait;
#[async_trait] #[async_trait]
pub trait Sandbox: Send + Sync { pub trait Sandbox: Send + Sync {
async fn start(&self, netns: Option<String>) -> Result<()>; async fn start(&self, netns: Option<String>, dns: Vec<String>) -> Result<()>;
async fn stop(&self) -> Result<()>; async fn stop(&self) -> Result<()>;
async fn cleanup(&self, container_id: &str) -> Result<()>; async fn cleanup(&self, container_id: &str) -> Result<()>;
async fn shutdown(&self) -> Result<()>; async fn shutdown(&self) -> Result<()>;

View File

@ -15,11 +15,14 @@ use common::{
RuntimeHandler, RuntimeInstance, Sandbox, RuntimeHandler, RuntimeInstance, Sandbox,
}; };
use hypervisor::Param; use hypervisor::Param;
use kata_types::{annotations::Annotation, config::TomlConfig}; use kata_types::{
annotations::Annotation, config::default::DEFAULT_GUEST_DNS_FILE, config::TomlConfig,
};
#[cfg(feature = "linux")] #[cfg(feature = "linux")]
use linux_container::LinuxContainer; use linux_container::LinuxContainer;
use persist::sandbox_persist::Persist; use persist::sandbox_persist::Persist;
use shim_interface::shim_mgmt::ERR_NO_SHIM_SERVER; use shim_interface::shim_mgmt::ERR_NO_SHIM_SERVER;
use tokio::fs;
use tokio::sync::{mpsc::Sender, RwLock}; use tokio::sync::{mpsc::Sender, RwLock};
#[cfg(feature = "virt")] #[cfg(feature = "virt")]
use virt_container::{ use virt_container::{
@ -48,6 +51,7 @@ impl RuntimeHandlerManagerInner {
async fn init_runtime_handler( async fn init_runtime_handler(
&mut self, &mut self,
netns: Option<String>, netns: Option<String>,
dns: Vec<String>,
config: Arc<TomlConfig>, config: Arc<TomlConfig>,
) -> Result<()> { ) -> Result<()> {
info!(sl!(), "new runtime handler {}", &config.runtime.name); info!(sl!(), "new runtime handler {}", &config.runtime.name);
@ -70,7 +74,7 @@ impl RuntimeHandlerManagerInner {
// start sandbox // start sandbox
runtime_instance runtime_instance
.sandbox .sandbox
.start(netns) .start(netns, dns)
.await .await
.context("start sandbox")?; .context("start sandbox")?;
self.runtime_instance = Some(Arc::new(runtime_instance)); self.runtime_instance = Some(Arc::new(runtime_instance));
@ -83,6 +87,8 @@ impl RuntimeHandlerManagerInner {
return Ok(()); return Ok(());
} }
let mut dns: Vec<String> = vec![];
#[cfg(feature = "linux")] #[cfg(feature = "linux")]
LinuxContainer::init().context("init linux container")?; LinuxContainer::init().context("init linux container")?;
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
@ -107,8 +113,15 @@ impl RuntimeHandlerManagerInner {
None None
}; };
for m in &spec.mounts {
if m.destination == DEFAULT_GUEST_DNS_FILE {
let contents = fs::read_to_string(&m.source).await?;
dns = contents.split('\n').map(|e| e.to_string()).collect();
}
}
let config = load_config(spec, options).context("load config")?; let config = load_config(spec, options).context("load config")?;
self.init_runtime_handler(netns, Arc::new(config)) self.init_runtime_handler(netns, dns, Arc::new(config))
.await .await
.context("init runtime handler")?; .context("init runtime handler")?;

View File

@ -123,7 +123,7 @@ impl VirtSandbox {
#[async_trait] #[async_trait]
impl Sandbox for VirtSandbox { impl Sandbox for VirtSandbox {
async fn start(&self, netns: Option<String>) -> Result<()> { async fn start(&self, netns: Option<String>, dns: Vec<String>) -> Result<()> {
let id = &self.sid; let id = &self.sid;
// if sandbox running, return // if sandbox running, return
@ -170,7 +170,7 @@ impl Sandbox for VirtSandbox {
let kernel_modules = KernelModule::set_kernel_modules(agent_config.kernel_modules)?; let kernel_modules = KernelModule::set_kernel_modules(agent_config.kernel_modules)?;
let req = agent::CreateSandboxRequest { let req = agent::CreateSandboxRequest {
hostname: "".to_string(), hostname: "".to_string(),
dns: vec![], dns,
storages: self storages: self
.resource_manager .resource_manager
.get_storage_for_sandbox() .get_storage_for_sandbox()