fix(spec):生成spec的时候补充cwd信息 (#75)

This commit is contained in:
火花 2025-04-17 16:34:12 +08:00 committed by GitHub
parent e2a95d4fed
commit c4176723f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 10 deletions

View File

@ -454,9 +454,7 @@ impl ContainerdManager {
fn get_spec(cid: &str, ns: &str, image_name: &str) -> Result<Option<Any>, ContainerdError> {
let config = ImageManager::get_runtime_config(image_name).unwrap();
let env = config.env;
let args = config.args;
let spec_path = generate_spec(cid, ns, args, env).map_err(|e| {
let spec_path = generate_spec(cid, ns, &config).map_err(|e| {
log::error!("Failed to generate spec: {}", e);
ContainerdError::GenerateSpecError(e.to_string())
})?;

View File

@ -28,11 +28,17 @@ pub struct ImageRuntimeConfig {
pub env: Vec<String>,
pub args: Vec<String>,
pub ports: Vec<String>,
pub cwd: String,
}
impl ImageRuntimeConfig {
pub fn new(env: Vec<String>, args: Vec<String>, ports: Vec<String>) -> Self {
ImageRuntimeConfig { env, args, ports }
pub fn new(env: Vec<String>, args: Vec<String>, ports: Vec<String>, cwd: String) -> Self {
ImageRuntimeConfig {
env,
args,
ports,
cwd,
}
}
}
@ -387,7 +393,11 @@ impl ImageManager {
.exposed_ports()
.clone()
.expect("Failed to get exposed ports");
Ok(ImageRuntimeConfig::new(env, args, ports))
let cwd = config
.working_dir()
.clone()
.expect("Failed to get working dir");
Ok(ImageRuntimeConfig::new(env, args, ports, cwd))
} else {
Err(ImageError::ImageConfigurationNotFound(format!(
"Image configuration is empty for image {}",

View File

@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use std::fs::File;
use crate::image_manager::ImageRuntimeConfig;
// 定义版本的常量
const VERSION_MAJOR: u32 = 1;
const VERSION_MINOR: u32 = 1;
@ -319,16 +321,16 @@ fn get_netns(ns: &str, cid: &str) -> String {
pub fn generate_spec(
id: &str,
ns: &str,
args: Vec<String>,
env: Vec<String>,
runtime_config: &ImageRuntimeConfig,
) -> Result<String, std::io::Error> {
let namespace = match ns {
"" => DEFAULT_NAMESPACE,
_ => ns,
};
let mut spec = populate_default_unix_spec(id, ns);
spec.process.args = args;
spec.process.env = env;
spec.process.args = runtime_config.args.clone();
spec.process.env = runtime_config.env.clone();
spec.process.cwd = runtime_config.cwd.clone();
let dir_path = format!("{}/{}", PATH_TO_SPEC_PREFIX, namespace);
let path = format!("{}/{}.json", dir_path, id);
std::fs::create_dir_all(&dir_path)?;