feat(provider): get information from container. (#19)

This commit is contained in:
aLinChe
2025-03-28 20:40:49 +08:00
committed by GitHub
parent 21e9f3cbe8
commit 81a3613231
6 changed files with 207 additions and 3 deletions

View File

@ -16,6 +16,7 @@ use containerd_client::{
types::{
Mount, Platform,
transfer::{ImageStore, OciRegistry, UnpackConfiguration},
v1::Process,
},
with_namespace,
};
@ -62,7 +63,8 @@ impl Service {
}
pub async fn get_netns_ip(&self, cid: &str) -> Option<(String, String)> {
let map = self.netns_map.read().unwrap();
let map: std::sync::RwLockReadGuard<'_, HashMap<String, (String, String)>> =
self.netns_map.read().unwrap();
map.get(cid).cloned()
}
@ -330,6 +332,24 @@ impl Service {
}
}
pub async fn load_container(&self, cid: &str, ns: &str) -> Result<Container, Err> {
let namespace = self.check_namespace(ns);
let mut c = self.client.containers();
let request = ListContainersRequest {
..Default::default()
};
let response = c
.list(with_namespace!(request, namespace))
.await?
.into_inner();
let container = response
.containers
.into_iter()
.find(|container| container.id == cid)
.ok_or_else(|| -> Err { format!("Container {} not found", cid).into() })?;
Ok(container)
}
pub async fn get_container_list(&self, ns: &str) -> Result<Vec<String>, tonic::Status> {
let namespace = self.check_namespace(ns);
let namespace = namespace.as_str();
@ -352,6 +372,25 @@ impl Service {
.collect())
}
pub async fn get_task(&self, cid: &str, ns: &str) -> Result<Process, Err> {
let namespace = self.check_namespace(ns);
let mut tc = self.client.tasks();
let request = ListTasksRequest {
filter: format!("container=={}", cid),
};
let response = tc.list(with_namespace!(request, namespace)).await?;
let tasks = response.into_inner().tasks;
let task = tasks
.into_iter()
.find(|task| task.container_id == cid)
.ok_or_else(|| -> Err { format!("Task for container {} not found", cid).into() })?;
Ok(task)
}
pub async fn get_task_list() {
todo!()
}
@ -610,7 +649,7 @@ impl Service {
Ok(ret)
}
async fn get_env_and_args(
pub async fn get_env_and_args(
&self,
name: &str,
ns: &str,