mirror of
https://github.com/faas-rs/faasd-in-rust.git
synced 2025-06-18 12:06:35 +00:00
feat(gateway):enable to deploy and delete function with request (#54)
* fix:move trait IAMHandler to types/ * feat:enable to resolve request and to set proxy for request * save implementation for trait IAMHandler * remove trait IAMHandler and add deploy and delete * add feishu_bot.yml * 修复一点ctr逻辑 (#29) * 修复一点逻辑 * fmt * feat:remove trait IAMHandler and bind fucntion to gateway * resolve conflict in cni_network * fix(delete): fix delete task. * fix(clippy): fmt * fix:remove darwin and launch.json * fix:hakari generate * fix: remove files and build targets * fix: some format problems --------- Co-authored-by: vitus213 <2811215248@qq.com> Co-authored-by: 火花 <sparkhhhhhhhhhh@outlook.com> Co-authored-by: scutKKsix <1129332011@qq.com> Co-authored-by: Samuka007 <samuka007@dragonos.org>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
pub mod spec;
|
||||
pub mod systemd;
|
||||
|
||||
use cni::cni_network::init_net_work;
|
||||
use containerd_client::{
|
||||
Client,
|
||||
services::v1::{
|
||||
@ -170,12 +171,10 @@ impl Service {
|
||||
.list(with_namespace!(request, namespace))
|
||||
.await?
|
||||
.into_inner();
|
||||
println!("Tasks: {:?}", responce.tasks);
|
||||
drop(tc);
|
||||
if let Some(task) = responce
|
||||
.tasks
|
||||
.iter()
|
||||
.find(|task| task.container_id == container.id)
|
||||
{
|
||||
|
||||
if let Some(task) = responce.tasks.iter().find(|task| task.id == container.id) {
|
||||
println!("Task found: {}, Status: {}", task.id, task.status);
|
||||
// TASK_UNKNOWN (0) — 未知状态
|
||||
// TASK_CREATED (1) — 任务已创建
|
||||
@ -184,7 +183,7 @@ impl Service {
|
||||
// TASK_EXITED (4) — 任务已退出
|
||||
// TASK_PAUSED (5) — 任务已暂停
|
||||
// TASK_FAILED (6) — 任务失败
|
||||
self.delete_task(&task.container_id, ns).await;
|
||||
let _ = self.delete_task(&task.id, ns).await;
|
||||
}
|
||||
|
||||
let delete_request = DeleteContainerRequest {
|
||||
@ -197,7 +196,7 @@ impl Service {
|
||||
.expect("Failed to delete container");
|
||||
self.remove_netns_ip(cid).await;
|
||||
|
||||
// println!("Container: {:?} deleted", cc);
|
||||
println!("Container: {:?} deleted", cc);
|
||||
} else {
|
||||
todo!("Container not found");
|
||||
}
|
||||
@ -237,9 +236,15 @@ impl Service {
|
||||
.await?
|
||||
.into_inner()
|
||||
.mounts;
|
||||
println!("mounts ok");
|
||||
drop(sc);
|
||||
println!("drop sc ok");
|
||||
let _ = init_net_work();
|
||||
println!("init_net_work ok");
|
||||
let (ip, path) = cni::cni_network::create_cni_network(cid.to_string(), ns.to_string())?;
|
||||
println!("create_cni_network ok");
|
||||
self.save_netns_ip(cid, &path, &ip).await;
|
||||
println!("save_netns_ip ok");
|
||||
let mut tc = self.client.tasks();
|
||||
let req = CreateTaskRequest {
|
||||
container_id: cid.to_string(),
|
||||
@ -257,6 +262,7 @@ impl Service {
|
||||
..Default::default()
|
||||
};
|
||||
let _resp = self.client.tasks().start(with_namespace!(req, ns)).await?;
|
||||
println!("Task: {:?} started", cid);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -284,7 +290,7 @@ impl Service {
|
||||
pub async fn resume_task() {
|
||||
todo!()
|
||||
}
|
||||
pub async fn delete_task(&self, cid: &str, ns: &str) {
|
||||
pub async fn delete_task(&self, cid: &str, ns: &str) -> Result<(), Err> {
|
||||
let namespace = self.check_namespace(ns);
|
||||
let namespace = namespace.as_str();
|
||||
|
||||
@ -300,6 +306,7 @@ impl Service {
|
||||
Ok::<(), Err>(())
|
||||
})
|
||||
.await;
|
||||
println!(" after wait");
|
||||
|
||||
let kill_request = KillRequest {
|
||||
container_id: cid.to_string(),
|
||||
@ -317,22 +324,43 @@ impl Service {
|
||||
container_id: cid.to_string(),
|
||||
};
|
||||
|
||||
let _resp = c
|
||||
.delete(with_namespace!(req, namespace))
|
||||
.await
|
||||
.expect("Failed to delete task");
|
||||
println!("Task: {:?} deleted", cid);
|
||||
// let _resp = c
|
||||
// .delete(with_namespace!(req, namespace))
|
||||
// .await
|
||||
// .expect("Failed to delete task");
|
||||
// println!("Task: {:?} deleted", cid);
|
||||
match c.delete(with_namespace!(req, namespace)).await {
|
||||
Ok(_) => {
|
||||
println!("Task: {:?} deleted", cid);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to delete task: {}", e);
|
||||
Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Ok(Err(e)) => {
|
||||
eprintln!("Wait task failed: {}", e);
|
||||
Err(e)
|
||||
}
|
||||
Err(_) => {
|
||||
let kill_request = KillRequest {
|
||||
container_id: cid.to_string(),
|
||||
signal: 9,
|
||||
all: true,
|
||||
..Default::default()
|
||||
};
|
||||
c.kill(with_namespace!(kill_request, namespace))
|
||||
.await
|
||||
.expect("Failed to FORCE kill task");
|
||||
match c.kill(with_namespace!(kill_request, namespace)).await {
|
||||
Ok(_) => {
|
||||
println!("Task: {:?} force killed", cid);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to force kill task: {}", e);
|
||||
Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -390,7 +418,7 @@ impl Service {
|
||||
|
||||
let task = tasks
|
||||
.into_iter()
|
||||
.find(|task| task.container_id == cid)
|
||||
.find(|task| task.id == cid)
|
||||
.ok_or_else(|| -> Err { format!("Task for container {} not found", cid).into() })?;
|
||||
|
||||
Ok(task)
|
||||
|
@ -329,7 +329,9 @@ pub fn generate_spec(
|
||||
let mut spec = populate_default_unix_spec(id, ns);
|
||||
spec.process.args = args;
|
||||
spec.process.env = env;
|
||||
let path = format!("{}/{}/{}.json", PATH_TO_SPEC_PREFIX, namespace, id);
|
||||
let dir_path = format!("{}/{}", PATH_TO_SPEC_PREFIX, namespace);
|
||||
let path = format!("{}/{}.json", dir_path, id);
|
||||
std::fs::create_dir_all(&dir_path)?;
|
||||
save_spec_to_file(&spec, &path)?;
|
||||
Ok(path)
|
||||
}
|
||||
|
Reference in New Issue
Block a user