修改一些逻辑 (#40)

* 修改一些逻辑

* 完善错误处理

* 恢复原来的Service初始化函数
This commit is contained in:
火花 2025-04-07 20:53:31 +08:00 committed by GitHub
parent 2f9f6c4ca9
commit 0222f5988a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 14 deletions

View File

@ -11,7 +11,7 @@ use handlers::*;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let service = Arc::new( let service = Arc::new(
Service::new("/run/containerd/containerd.sock".to_string()) Service::new("/run/containerd/containerd.sock")
.await .await
.unwrap(), .unwrap(),
); );

View File

@ -49,8 +49,8 @@ pub struct Service {
} }
impl Service { impl Service {
pub async fn new(endpoint: String) -> Result<Self, Err> { pub async fn new(socket_path: &str) -> Result<Self, Err> {
let client = Client::from_path(endpoint).await.unwrap(); let client = Client::from_path(socket_path).await.unwrap();
Ok(Service { Ok(Service {
client: Arc::new(client), client: Arc::new(client),
netns_map: GLOBAL_NETNS_MAP.clone(), netns_map: GLOBAL_NETNS_MAP.clone(),
@ -73,8 +73,13 @@ impl Service {
map.remove(cid); map.remove(cid);
} }
async fn prepare_snapshot(&self, cid: &str, ns: &str) -> Result<Vec<Mount>, Err> { async fn prepare_snapshot(
let parent_snapshot = self.get_parent_snapshot(cid, ns).await?; &self,
cid: &str,
ns: &str,
img_name: &str,
) -> Result<Vec<Mount>, Err> {
let parent_snapshot = self.get_parent_snapshot(img_name, ns).await?;
let req = PrepareSnapshotRequest { let req = PrepareSnapshotRequest {
snapshotter: "overlayfs".to_string(), snapshotter: "overlayfs".to_string(),
key: cid.to_string(), key: cid.to_string(),
@ -98,7 +103,7 @@ impl Service {
_ => ns, _ => ns,
}; };
let _mount = self.prepare_snapshot(cid, ns).await?; let _mount = self.prepare_snapshot(cid, ns, image_name).await?;
let (env, args) = self.get_env_and_args(image_name, ns).await?; let (env, args) = self.get_env_and_args(image_name, ns).await?;
let spec_path = generate_spec(cid, ns, args, env).unwrap(); let spec_path = generate_spec(cid, ns, args, env).unwrap();
let spec = fs::read_to_string(spec_path).unwrap(); let spec = fs::read_to_string(spec_path).unwrap();
@ -548,11 +553,11 @@ impl Service {
::serde_json::from_slice(&resp).unwrap() ::serde_json::from_slice(&resp).unwrap()
} }
pub async fn get_img_config(&self, name: &str, ns: &str) -> Option<ImageConfiguration> { pub async fn get_img_config(&self, img_name: &str, ns: &str) -> Option<ImageConfiguration> {
let mut c = self.client.images(); let mut c = self.client.images();
let req = GetImageRequest { let req = GetImageRequest {
name: name.to_string(), name: img_name.to_string(),
}; };
let resp = c let resp = c
.get(with_namespace!(req, ns)) .get(with_namespace!(req, ns))
@ -560,7 +565,7 @@ impl Service {
.map_err(|e| { .map_err(|e| {
eprintln!( eprintln!(
"Failed to get the config of {} in namespace {}: {}", "Failed to get the config of {} in namespace {}: {}",
name, ns, e img_name, ns, e
); );
e e
}) })
@ -583,7 +588,7 @@ impl Service {
.map_err(|e| { .map_err(|e| {
eprintln!( eprintln!(
"Failed to read content for {} in namespace {}: {}", "Failed to read content for {} in namespace {}: {}",
name, ns, e img_name, ns, e
); );
e e
}) })
@ -594,13 +599,13 @@ impl Service {
.map_err(|e| { .map_err(|e| {
eprintln!( eprintln!(
"Failed to read message for {} in namespace {}: {}", "Failed to read message for {} in namespace {}: {}",
name, ns, e img_name, ns, e
); );
e e
}) })
.ok()? .ok()?
.ok_or_else(|| { .ok_or_else(|| {
eprintln!("No data found for {} in namespace {}", name, ns); eprintln!("No data found for {} in namespace {}", img_name, ns);
std::io::Error::new(std::io::ErrorKind::NotFound, "No data found") std::io::Error::new(std::io::ErrorKind::NotFound, "No data found")
}) })
.ok()? .ok()?
@ -629,8 +634,11 @@ impl Service {
Some(img_config) Some(img_config)
} }
async fn get_parent_snapshot(&self, name: &str, ns: &str) -> Result<String, Err> { async fn get_parent_snapshot(&self, img_name: &str, ns: &str) -> Result<String, Err> {
let img_config = self.get_img_config(name, ns).await.unwrap(); let img_config = match self.get_img_config(img_name, ns).await {
Some(config) => config,
None => return Err("Failed to get image configuration".into()),
};
let mut iter = img_config.rootfs().diff_ids().iter(); let mut iter = img_config.rootfs().diff_ids().iter();
let mut ret = iter let mut ret = iter