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 ----
//
#[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<String, ConsulServiceEntry>,
}
@ -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<ConsulNodeListNode> = http.json().await?;
let resp: Vec<ConsulNode> = http.json().await?;
Ok(resp.into_iter().map(|n| n.node).collect::<Vec<_>>())
}

View File

@ -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() {

View File

@ -86,7 +86,13 @@ fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec<ProxyEntry> {
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<Arc<ProxyConfi
let will_retry_in = retry_to_time(watch_state.retries, Duration::from_secs(600));
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(),
e
);