mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-01 00:46:38 +00:00
Merge pull request #6287 from lifupan/main
sandbox: set the dns for the sandbox
This commit is contained in:
@@ -7,6 +7,7 @@ use anyhow::{anyhow, Result};
|
|||||||
use nix::mount::{self, MsFlags};
|
use nix::mount::{self, MsFlags};
|
||||||
use slog::Logger;
|
use slog::Logger;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path;
|
||||||
|
|
||||||
const KATA_GUEST_SANDBOX_DNS_FILE: &str = "/run/kata-containers/sandbox/resolv.conf";
|
const KATA_GUEST_SANDBOX_DNS_FILE: &str = "/run/kata-containers/sandbox/resolv.conf";
|
||||||
const GUEST_DNS_FILE: &str = "/etc/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())
|
.map(|x| x.trim())
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.join("\n");
|
.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)?;
|
fs::write(src, content)?;
|
||||||
|
|
||||||
// bind mount to /etc/resolv.conf
|
// bind mount to /etc/resolv.conf
|
||||||
|
@@ -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;
|
||||||
|
|
||||||
|
@@ -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<()>;
|
||||||
|
@@ -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")?;
|
||||||
|
|
||||||
|
@@ -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()
|
||||||
|
Reference in New Issue
Block a user