Use node IP when service IP is not available

This commit is contained in:
Alex 2021-12-07 18:31:04 +01:00
parent 489d364676
commit 9119c2f45c
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1
3 changed files with 19 additions and 4 deletions

View file

@ -9,13 +9,18 @@ use serde::{Deserialize, Serialize};
// ---- Watch and retrieve Consul catalog ---- // ---- Watch and retrieve Consul catalog ----
// //
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ConsulNodeListNode { pub struct ConsulNode {
#[serde(rename = "Node")] #[serde(rename = "Node")]
pub node: String, pub node: String,
#[serde(rename = "Address")]
pub address: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ConsulServiceEntry { pub struct ConsulServiceEntry {
#[serde(rename = "Service")]
pub service: String,
#[serde(rename = "Address")] #[serde(rename = "Address")]
pub address: String, pub address: String,
@ -28,6 +33,8 @@ pub struct ConsulServiceEntry {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ConsulNodeCatalog { pub struct ConsulNodeCatalog {
#[serde(rename = "Node")]
pub node: ConsulNode,
#[serde(rename = "Services")] #[serde(rename = "Services")]
pub services: HashMap<String, ConsulServiceEntry>, pub services: HashMap<String, ConsulServiceEntry>,
} }
@ -84,7 +91,7 @@ impl Consul {
let url = format!("{}/v1/catalog/nodes", self.url); let url = format!("{}/v1/catalog/nodes", self.url);
let http = self.client.get(&url).send().await?; let http = self.client.get(&url).send().await?;
let resp: Vec<ConsulNodeListNode> = http.json().await?; let resp: Vec<ConsulNode> = http.json().await?;
Ok(resp.into_iter().map(|n| n.node).collect::<Vec<_>>()) Ok(resp.into_iter().map(|n| n.node).collect::<Vec<_>>())
} }

View file

@ -38,6 +38,7 @@ struct Opt {
pub https_bind_addr: SocketAddr, pub https_bind_addr: SocketAddr,
} }
#[tokio::main(flavor = "multi_thread", worker_threads = 10)] #[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() { async fn main() {
if std::env::var("RUST_LOG").is_err() { if std::env::var("RUST_LOG").is_err() {

View file

@ -86,7 +86,13 @@ fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec<ProxyEntry> {
for (_, svc) in catalog.services.iter() { for (_, svc) in catalog.services.iter() {
let ip_addr = match svc.address.parse() { let ip_addr = match svc.address.parse() {
Ok(ip) => ip, 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); let addr = SocketAddr::new(ip_addr, svc.port);
@ -178,7 +184,8 @@ pub fn spawn_proxy_config_task(consul: Consul) -> watch::Receiver<Arc<ProxyConfi
let will_retry_in = retry_to_time(watch_state.retries, Duration::from_secs(600)); let will_retry_in = retry_to_time(watch_state.retries, Duration::from_secs(600));
error!( error!(
"Failed to query consul. Will retry in {}s. {}", "Failed to query consul for node {}. Will retry in {}s. {}",
node,
will_retry_in.as_secs(), will_retry_in.as_secs(),
e e
); );