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

View File

@ -9,7 +9,7 @@ use async_trait::async_trait;
#[async_trait]
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 cleanup(&self, container_id: &str) -> Result<()>;
async fn shutdown(&self) -> Result<()>;

View File

@ -15,11 +15,14 @@ use common::{
RuntimeHandler, RuntimeInstance, Sandbox,
};
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")]
use linux_container::LinuxContainer;
use persist::sandbox_persist::Persist;
use shim_interface::shim_mgmt::ERR_NO_SHIM_SERVER;
use tokio::fs;
use tokio::sync::{mpsc::Sender, RwLock};
#[cfg(feature = "virt")]
use virt_container::{
@ -48,6 +51,7 @@ impl RuntimeHandlerManagerInner {
async fn init_runtime_handler(
&mut self,
netns: Option<String>,
dns: Vec<String>,
config: Arc<TomlConfig>,
) -> Result<()> {
info!(sl!(), "new runtime handler {}", &config.runtime.name);
@ -70,7 +74,7 @@ impl RuntimeHandlerManagerInner {
// start sandbox
runtime_instance
.sandbox
.start(netns)
.start(netns, dns)
.await
.context("start sandbox")?;
self.runtime_instance = Some(Arc::new(runtime_instance));
@ -83,6 +87,8 @@ impl RuntimeHandlerManagerInner {
return Ok(());
}
let mut dns: Vec<String> = vec![];
#[cfg(feature = "linux")]
LinuxContainer::init().context("init linux container")?;
#[cfg(feature = "wasm")]
@ -107,8 +113,15 @@ impl RuntimeHandlerManagerInner {
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")?;
self.init_runtime_handler(netns, Arc::new(config))
self.init_runtime_handler(netns, dns, Arc::new(config))
.await
.context("init runtime handler")?;

View File

@ -123,7 +123,7 @@ impl VirtSandbox {
#[async_trait]
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;
// if sandbox running, return
@ -170,7 +170,7 @@ impl Sandbox for VirtSandbox {
let kernel_modules = KernelModule::set_kernel_modules(agent_config.kernel_modules)?;
let req = agent::CreateSandboxRequest {
hostname: "".to_string(),
dns: vec![],
dns,
storages: self
.resource_manager
.get_storage_for_sandbox()