From f5f223db6b77cd30ffc177cc50ccfe920f50d6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=AB=E8=8A=B1?= Date: Sat, 12 Apr 2025 18:15:59 +0800 Subject: [PATCH] feat(service): record port for access (#57) --- crates/provider/src/handlers/function_get.rs | 7 +- crates/service/src/lib.rs | 68 ++++++++++++++++---- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/crates/provider/src/handlers/function_get.rs b/crates/provider/src/handlers/function_get.rs index d0a2e74..8eba6c6 100644 --- a/crates/provider/src/handlers/function_get.rs +++ b/crates/provider/src/handlers/function_get.rs @@ -24,10 +24,7 @@ pub async fn get_function( namespace: &str, ) -> Result { let cid = function_name; - let (_, ip) = client - .get_netns_ip(cid) - .await - .unwrap_or((namespace.to_string(), String::new())); + let ip = client.get_ip(cid).await.unwrap(); let container = client .load_container(cid, namespace) @@ -42,7 +39,7 @@ pub async fn get_function( let all_labels = container.labels; let (labels, _) = build_labels_and_annotations(all_labels); - let (env, _) = client.get_env_and_args(&image, namespace).await?; + let env = client.get_runtime_config(&image, namespace).await?.env; let (env_vars, env_process) = read_env_from_process_env(env); // let secrets = read_secrets_from_mounts(&spec.mounts); // let memory_limit = read_memory_limit_from_spec(&spec); diff --git a/crates/service/src/lib.rs b/crates/service/src/lib.rs index c7b71f1..6932ab2 100644 --- a/crates/service/src/lib.rs +++ b/crates/service/src/lib.rs @@ -37,7 +37,7 @@ use tokio::time::timeout; // config.json,dockerhub密钥 // const DOCKER_CONFIG_DIR: &str = "/var/lib/faasd/.docker/"; -type NetnsMap = Arc>>; +type NetnsMap = Arc>>; lazy_static::lazy_static! { static ref GLOBAL_NETNS_MAP: NetnsMap = Arc::new(RwLock::new(HashMap::new())); } @@ -58,17 +58,27 @@ impl Service { }) } - pub async fn save_netns_ip(&self, cid: &str, netns: &str, ip: &str) { + pub async fn save_network_config(&self, cid: &str, net_conf: NetworkConfig) { let mut map = self.netns_map.write().unwrap(); - map.insert(cid.to_string(), (netns.to_string(), ip.to_string())); + map.insert(cid.to_string(), net_conf); } - pub async fn get_netns_ip(&self, cid: &str) -> Option<(String, String)> { - let map: std::sync::RwLockReadGuard<'_, HashMap> = - self.netns_map.read().unwrap(); + pub async fn get_network_config(&self, cid: &str) -> Option { + let map = self.netns_map.read().unwrap(); map.get(cid).cloned() } + pub async fn get_ip(&self, cid: &str) -> Option { + let map = self.netns_map.read().unwrap(); + map.get(cid).map(|net_conf| net_conf.ip.clone()) + } + + pub async fn get_address(&self, cid: &str) -> Option { + let map = self.netns_map.read().unwrap(); + map.get(cid) + .map(|net_conf| format!("{}:{}", net_conf.ip, net_conf.ports[0])) + } + pub async fn remove_netns_ip(&self, cid: &str) { let mut map = self.netns_map.write().unwrap(); map.remove(cid); @@ -105,7 +115,9 @@ impl Service { }; let _mount = self.prepare_snapshot(cid, ns, image_name).await?; - let (env, args) = self.get_env_and_args(image_name, ns).await?; + let config = self.get_runtime_config(image_name, ns).await?; + let env = config.env; + let args = config.args; let spec_path = generate_spec(cid, ns, args, env).unwrap(); let spec = fs::read_to_string(spec_path).unwrap(); @@ -242,8 +254,10 @@ impl Service { let _ = init_net_work(); println!("init_net_work ok"); let (ip, path) = cni::cni_network::create_cni_network(cid.to_string(), ns.to_string())?; + let ports = self.get_runtime_config(cid, ns).await?.ports; + let network_config = NetworkConfig::new(path, ip, ports); println!("create_cni_network ok"); - self.save_netns_ip(cid, &path, &ip).await; + self.save_network_config(cid, network_config).await; println!("save_netns_ip ok"); let mut tc = self.client.tasks(); let req = CreateTaskRequest { @@ -685,16 +699,16 @@ impl Service { Ok(ret) } - pub async fn get_env_and_args( - &self, - name: &str, - ns: &str, - ) -> Result<(Vec, Vec), Err> { + pub async fn get_runtime_config(&self, name: &str, ns: &str) -> Result { let img_config = self.get_img_config(name, ns).await.unwrap(); if let Some(config) = img_config.config() { let env = config.env().as_ref().map_or_else(Vec::new, |v| v.clone()); let args = config.cmd().as_ref().map_or_else(Vec::new, |v| v.clone()); - Ok((env, args)) + let ports = config + .exposed_ports() + .as_ref() + .map_or_else(Vec::new, |v| v.clone()); + Ok(RunTimeConfig::new(env, args, ports)) } else { Err("No config found".into()) } @@ -732,3 +746,29 @@ impl Service { } //容器是容器,要先启动,然后才能运行任务 //要想删除一个正在运行的Task,必须先kill掉这个task,然后才能删除。 + +#[derive(Debug)] +pub struct RunTimeConfig { + pub env: Vec, + pub args: Vec, + pub ports: Vec, +} + +impl RunTimeConfig { + pub fn new(env: Vec, args: Vec, ports: Vec) -> Self { + RunTimeConfig { env, args, ports } + } +} + +#[derive(Debug, Clone)] +pub struct NetworkConfig { + pub netns: String, + pub ip: String, + pub ports: Vec, +} + +impl NetworkConfig { + pub fn new(netns: String, ip: String, ports: Vec) -> Self { + NetworkConfig { netns, ip, ports } + } +}