Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor flag parser #134

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vmm/sandbox/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 42 additions & 9 deletions vmm/sandbox/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,48 @@ pub struct Args {
#[arg(short, long)]
pub version: bool,

/// Config file path, only for cloud hypervisor and stratovirt
#[arg(short, long, value_name = "FILE")]
pub config: Option<String>,
/// Config file path, only for cloud hypervisor and stratovirt, default is `/var/lib/kuasar/config_clh.toml`
#[arg(
short,
long,
value_name = "FILE",
default_value = "/var/lib/kuasar/config_clh.toml"
)]
pub config: String,

/// Sandboxer working directory
#[arg(short, long, value_name = "DIR")]
pub dir: Option<String>,
/// Sandboxer working directory, default is `/run/kuasar-vmm`
#[arg(short, long, value_name = "DIR", default_value = "/run/kuasar-vmm")]
pub dir: String,

/// Address for sandboxer's server
#[arg(short, long, value_name = "FILE")]
pub listen: Option<String>,
/// Address for sandboxer's server, default is `/run/vmm-sandboxer.sock`
#[arg(
short,
long,
value_name = "FILE",
default_value = "/run/vmm-sandboxer.sock"
)]
pub listen: String,

// log_level is optional and should not have default value if not given, since
// it can be defined in configuration file.
/// Logging level for sandboxer [trace, debug, info, warn, error, fatal, panic]
#[arg(long, value_name = "STRING")]
pub log_level: Option<String>,
}

#[cfg(test)]
mod tests {
use clap::Parser;

use crate::args::Args;

#[test]
fn test_args_parse_default() {
let args = Args::parse();
assert!(!args.version);
assert_eq!(args.config, "/var/lib/kuasar/config_clh.toml");
assert_eq!(args.dir, "/run/kuasar-vmm");
assert_eq!(args.listen, "/run/vmm-sandboxer.sock");
assert!(args.log_level.is_none());
}
}
42 changes: 30 additions & 12 deletions vmm/sandbox/src/bin/cloud_hypervisor/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,44 @@ limitations under the License.

use clap::Parser;
use vmm_sandboxer::{
args, cloud_hypervisor::init_cloud_hypervisor_sandboxer, utils::init_logger, version,
args,
cloud_hypervisor::{factory::CloudHypervisorVMFactory, hooks::CloudHypervisorHooks},
config::Config,
sandbox::KuasarSandboxer,
utils::init_logger,
version,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
return;
}
// Initialize sandboxer
let sandboxer = init_cloud_hypervisor_sandboxer(&args).await?;

// Initialize log
init_logger(sandboxer.log_level());
let config = Config::load_config(&args.config).await.unwrap();

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();
// Update args log level if it not presents args but in config.
init_logger(&args.log_level.unwrap_or(config.sandbox.log_level()));

let mut sandboxer: KuasarSandboxer<CloudHypervisorVMFactory, CloudHypervisorHooks> =
KuasarSandboxer::new(
config.sandbox,
config.hypervisor,
CloudHypervisorHooks::default(),
);

Ok(())
// Do recovery job
sandboxer.recover(&args.dir).await;

// Run the sandboxer
containerd_sandbox::run(
"kuasar-vmm-sandboxer-clh",
&args.listen,
&args.dir,
sandboxer,
)
.await
.unwrap();
}
54 changes: 43 additions & 11 deletions vmm/sandbox/src/bin/qemu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,58 @@ limitations under the License.
*/

use clap::Parser;
use vmm_sandboxer::{args, qemu::init_qemu_sandboxer, utils::init_logger, version};
use vmm_sandboxer::{
args,
config::Config,
kata_config::KataConfig,
qemu::{factory::QemuVMFactory, hooks::QemuHooks},
sandbox::KuasarSandboxer,
utils::init_logger,
version,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
return;
}

// Initialize sandboxer
let sandboxer = init_qemu_sandboxer(&args).await?;
// For compatibility with kata config
let config_path = std::env::var("KATA_CONFIG_PATH")
.unwrap_or_else(|_| "/usr/share/defaults/kata-containers/configuration.toml".to_string());
let path = std::path::Path::new(&config_path);

let config = if path.exists() {
KataConfig::init(path).await.unwrap();
let sandbox_config = KataConfig::sandbox_config("qemu").await.unwrap();
let vmm_config = KataConfig::hypervisor_config("qemu", |h| h.clone())
.await
.unwrap()
.to_qemu_config()
.unwrap();
Config::new(sandbox_config, vmm_config)
} else {
Config::load_config(&args.config).await.unwrap()
};

// Initialize log
init_logger(sandboxer.log_level());
init_logger(&config.sandbox.log_level());

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();
let sandboxer: KuasarSandboxer<QemuVMFactory, QemuHooks> = KuasarSandboxer::new(
config.sandbox,
config.hypervisor.clone(),
QemuHooks::new(config.hypervisor),
);

Ok(())
// Run the sandboxer
containerd_sandbox::run(
"kuasar-vmm-sandboxer-qemu",
&args.listen,
&args.dir,
sandboxer,
)
.await
.unwrap();
}
44 changes: 32 additions & 12 deletions vmm/sandbox/src/bin/stratovirt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,46 @@ limitations under the License.
*/

use clap::Parser;
use vmm_sandboxer::{args, stratovirt::init_stratovirt_sandboxer, utils::init_logger, version};
use vmm_sandboxer::{
args,
config::Config,
sandbox::KuasarSandboxer,
stratovirt::{
config::StratoVirtVMConfig, factory::StratoVirtVMFactory, hooks::StratoVirtHooks,
},
utils::init_logger,
version,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
return;
}

// Initialize sandboxer
let sandboxer = init_stratovirt_sandboxer(&args).await?;
let config: Config<StratoVirtVMConfig> = Config::load_config(&args.config).await.unwrap();

// Initialize log
init_logger(sandboxer.log_level());
// Update args log level if it not presents args but in config.
init_logger(&args.log_level.unwrap_or(config.sandbox.log_level()));

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();
let mut sandboxer: KuasarSandboxer<StratoVirtVMFactory, StratoVirtHooks> = KuasarSandboxer::new(
config.sandbox,
config.hypervisor.clone(),
StratoVirtHooks::new(config.hypervisor),
);

// Do recovery job
sandboxer.recover(&args.dir).await;

Ok(())
// Run the sandboxer
containerd_sandbox::run(
"kuasar-vmm-sandboxer-stratovirt",
&args.listen,
&args.dir,
sandboxer,
)
.await
.unwrap();
}
1 change: 1 addition & 0 deletions vmm/sandbox/src/cloud_hypervisor/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
cloud_hypervisor::CloudHypervisorVM, sandbox::KuasarSandbox, utils::get_resources, vm::Hooks,
};

#[derive(Default)]
pub struct CloudHypervisorHooks {}

#[async_trait::async_trait]
Expand Down
18 changes: 0 additions & 18 deletions vmm/sandbox/src/cloud_hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ use tokio::{
};
use vmm_common::SHARED_DIR_SUFFIX;

use self::{factory::CloudHypervisorVMFactory, hooks::CloudHypervisorHooks};
use crate::{
args::Args,
cloud_hypervisor::{
client::ChClient,
config::{CloudHypervisorConfig, CloudHypervisorVMConfig, VirtiofsdConfig},
Expand All @@ -42,9 +40,7 @@ use crate::{
},
},
device::{BusType, DeviceInfo},
load_config,
param::ToCmdLineParams,
sandbox::KuasarSandboxer,
utils::{read_std, set_cmd_fd, set_cmd_netns, wait_channel, wait_pid, write_file_atomic},
vm::{Pids, VcpuThreads, VM},
};
Expand All @@ -56,7 +52,6 @@ pub mod factory;
pub mod hooks;

const VCPU_PREFIX: &str = "vcpu";
pub const CONFIG_CLH_PATH: &str = "/var/lib/kuasar/config_clh.toml";

#[derive(Default, Serialize, Deserialize)]
pub struct CloudHypervisorVM {
Expand Down Expand Up @@ -399,16 +394,3 @@ fn spawn_wait(
}
})
}

pub async fn init_cloud_hypervisor_sandboxer(
args: &Args,
) -> Result<KuasarSandboxer<CloudHypervisorVMFactory, CloudHypervisorHooks>> {
let (config, persist_dir_path) =
load_config::<CloudHypervisorVMConfig>(args, CONFIG_CLH_PATH).await?;
let hooks = CloudHypervisorHooks {};
let mut s = KuasarSandboxer::new(config.sandbox, config.hypervisor, hooks);
if !persist_dir_path.is_empty() {
s.recover(&persist_dir_path).await?;
}
Ok(s)
}
Loading
Loading