Merge pull request #6287 from lifupan/main

sandbox: set the dns for the sandbox
This commit is contained in:
Bin Liu 2023-02-16 15:00:01 +08:00 committed by GitHub
commit 629a31ec6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 6 deletions

View File

@ -7,6 +7,7 @@ use anyhow::{anyhow, Result};
use nix::mount::{self, MsFlags};
use slog::Logger;
use std::fs;
use std::path;
const KATA_GUEST_SANDBOX_DNS_FILE: &str = "/run/kata-containers/sandbox/resolv.conf";
const GUEST_DNS_FILE: &str = "/etc/resolv.conf";
@ -64,6 +65,12 @@ fn do_setup_guest_dns(logger: Logger, dns_list: Vec<String>, src: &str, dst: &st
.map(|x| x.trim())
.collect::<Vec<&str>>()
.join("\n");
// make sure the src file's parent path exist.
let file_path = path::Path::new(src);
if let Some(p) = file_path.parent() {
fs::create_dir_all(p)?;
}
fs::write(src, content)?;
// bind mount to /etc/resolv.conf

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()