From 9119c2f45c348497ea8d2f2f73768f50a198c4af Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 7 Dec 2021 18:31:04 +0100 Subject: [PATCH] Use node IP when service IP is not available --- src/consul.rs | 11 +++++++++-- src/main.rs | 1 + src/proxy_config.rs | 11 +++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/consul.rs b/src/consul.rs index 240c177..eb7aafd 100644 --- a/src/consul.rs +++ b/src/consul.rs @@ -9,13 +9,18 @@ use serde::{Deserialize, Serialize}; // ---- Watch and retrieve Consul catalog ---- // #[derive(Serialize, Deserialize, Debug)] -pub struct ConsulNodeListNode { +pub struct ConsulNode { #[serde(rename = "Node")] pub node: String, + #[serde(rename = "Address")] + pub address: String, } #[derive(Serialize, Deserialize, Debug)] pub struct ConsulServiceEntry { + #[serde(rename = "Service")] + pub service: String, + #[serde(rename = "Address")] pub address: String, @@ -28,6 +33,8 @@ pub struct ConsulServiceEntry { #[derive(Serialize, Deserialize, Debug)] pub struct ConsulNodeCatalog { + #[serde(rename = "Node")] + pub node: ConsulNode, #[serde(rename = "Services")] pub services: HashMap, } @@ -84,7 +91,7 @@ impl Consul { let url = format!("{}/v1/catalog/nodes", self.url); let http = self.client.get(&url).send().await?; - let resp: Vec = http.json().await?; + let resp: Vec = http.json().await?; Ok(resp.into_iter().map(|n| n.node).collect::>()) } diff --git a/src/main.rs b/src/main.rs index 3a51702..f38767e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ struct Opt { pub https_bind_addr: SocketAddr, } + #[tokio::main(flavor = "multi_thread", worker_threads = 10)] async fn main() { if std::env::var("RUST_LOG").is_err() { diff --git a/src/proxy_config.rs b/src/proxy_config.rs index 9d07604..3e3e62f 100644 --- a/src/proxy_config.rs +++ b/src/proxy_config.rs @@ -86,7 +86,13 @@ fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec { for (_, svc) in catalog.services.iter() { let ip_addr = match svc.address.parse() { Ok(ip) => ip, - _ => continue, + _ => match catalog.node.address.parse() { + Ok(ip) => ip, + _ => { + warn!("Could not get address for service {} at node {}", svc.service, catalog.node.node); + continue; + } + } }; let addr = SocketAddr::new(ip_addr, svc.port); @@ -178,7 +184,8 @@ pub fn spawn_proxy_config_task(consul: Consul) -> watch::Receiver