diff options
Diffstat (limited to 'src/dns_config.rs')
-rw-r--r-- | src/dns_config.rs | 114 |
1 files changed, 45 insertions, 69 deletions
diff --git a/src/dns_config.rs b/src/dns_config.rs index 8e82c32..acee8d7 100644 --- a/src/dns_config.rs +++ b/src/dns_config.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use std::fmt; use std::net::{Ipv4Addr, Ipv6Addr}; use std::sync::Arc; -use std::time::{Duration, SystemTime}; +use std::time::Duration; use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -11,6 +11,8 @@ use tracing::*; use df_consul::*; +use crate::autodiscovery::*; + const IP_TARGET_METADATA_TAG_PREFIX: &str = "public_"; const CNAME_TARGET_METADATA_TAG: &str = "cname_target"; @@ -66,29 +68,25 @@ pub fn spawn_dns_config_task( ) -> watch::Receiver<Arc<DnsConfig>> { let (tx, rx) = watch::channel(Arc::new(DnsConfig::new())); - let fetcher = DnsConfigFetcher { - consul, - node_ipv4_cache: HashMap::new(), - node_ipv6_cache: HashMap::new(), - }; + let fetcher = DnsConfigTask { consul }; tokio::spawn(fetcher.task(tx, must_exit)); rx } -struct DnsConfigFetcher { +struct DnsConfigTask { consul: Consul, - node_ipv4_cache: HashMap<String, (u64, Option<Ipv4Addr>)>, - node_ipv6_cache: HashMap<String, (u64, Option<Ipv6Addr>)>, } -impl DnsConfigFetcher { +impl DnsConfigTask { async fn task( mut self, tx: watch::Sender<Arc<DnsConfig>>, mut must_exit: watch::Receiver<bool>, ) { + let mut autodiscovery_rx = watch_autodiscovered_ips(self.consul.clone(), must_exit.clone()); + let mut catalog_rx = self .consul .watch_all_service_health(Duration::from_secs(60)); @@ -96,11 +94,13 @@ impl DnsConfigFetcher { while !*must_exit.borrow() { select! { _ = catalog_rx.changed() => (), + _ = autodiscovery_rx.changed() => (), _ = must_exit.changed() => continue, }; let services = catalog_rx.borrow_and_update().clone(); - match self.parse_catalog(&services).await { + let autodiscovery = autodiscovery_rx.borrow_and_update().clone(); + match self.parse_catalog(&services, &autodiscovery) { Ok(dns_config) => tx.send(Arc::new(dns_config)).expect("Internal error"), Err(e) => { error!("Error when parsing tags: {}", e); @@ -109,7 +109,11 @@ impl DnsConfigFetcher { } } - async fn parse_catalog(&mut self, services: &catalog::AllServiceHealth) -> Result<DnsConfig> { + fn parse_catalog( + &mut self, + services: &catalog::AllServiceHealth, + autodiscovery: &AutodiscoveredAddresses, + ) -> Result<DnsConfig> { let mut dns_config = DnsConfig::new(); for (_svc, nodes) in services.iter() { for node in nodes.iter() { @@ -118,7 +122,7 @@ impl DnsConfigFetcher { continue; } for tag in node.service.tags.iter() { - if let Some((k, v)) = self.parse_d53_tag(tag, &node.node).await? { + if let Some((k, v)) = self.parse_d53_tag(tag, &node.node, autodiscovery)? { dns_config.add(k, v); } } @@ -128,10 +132,11 @@ impl DnsConfigFetcher { Ok(dns_config) } - async fn parse_d53_tag( + fn parse_d53_tag( &mut self, tag: &str, node: &catalog::Node, + autodiscovery: &AutodiscoveredAddresses, ) -> Result<Option<(DnsEntryKey, DnsEntryValue)>> { let splits = tag.split(' ').collect::<Vec<_>>(); if splits.len() != 2 { @@ -139,14 +144,14 @@ impl DnsConfigFetcher { } let (record_type, target) = match splits[0] { - "d53-a" => match self.get_node_ipv4(&node).await? { + "d53-a" => match self.get_node_ipv4(&autodiscovery, &node)? { Some(tgt) => (DnsRecordType::A, tgt.to_string()), None => { warn!("Got d53-a tag `{}` but node {} does not appear to have a known public IPv4 address. Tag is ignored.", tag, node.node); return Ok(None); } }, - "d53-aaaa" => match self.get_node_ipv6(&node).await? { + "d53-aaaa" => match self.get_node_ipv6(&autodiscovery, &node)? { Some(tgt) => (DnsRecordType::AAAA, tgt.to_string()), None => { warn!("Got d53-aaaa tag `{}` but node {} does not appear to have a known public IPv6 address. Tag is ignored.", tag, node.node); @@ -174,77 +179,48 @@ impl DnsConfigFetcher { ))) } - async fn get_node_ipv4(&mut self, node: &catalog::Node) -> Result<Option<Ipv4Addr>> { - Self::get_node_ip(&self.consul, "ipv4", &mut self.node_ipv4_cache, node).await + fn get_node_ipv4( + &mut self, + autodiscovery: &AutodiscoveredAddresses, + node: &catalog::Node, + ) -> Result<Option<Ipv4Addr>> { + Self::get_node_ip("ipv4", &autodiscovery.ipv4, node) } - async fn get_node_ipv6(&mut self, node: &catalog::Node) -> Result<Option<Ipv6Addr>> { - Self::get_node_ip(&self.consul, "ipv6", &mut self.node_ipv6_cache, node).await + fn get_node_ipv6( + &mut self, + autodiscovery: &AutodiscoveredAddresses, + node: &catalog::Node, + ) -> Result<Option<Ipv6Addr>> { + Self::get_node_ip("ipv6", &autodiscovery.ipv6, node) } - async fn get_node_ip<A>( - consul: &Consul, - family: &str, - cache: &mut HashMap<String, (u64, Option<A>)>, + fn get_node_ip<A>( + family: &'static str, + autodiscovery: &HashMap<String, DiplonatAutodiscoveryResult<A>>, node: &catalog::Node, ) -> Result<Option<A>> where A: Serialize + for<'de> Deserialize<'de> + std::fmt::Debug + std::str::FromStr + Copy + Eq, <A as std::str::FromStr>::Err: Send + Sync + std::error::Error + 'static, { - match cache.get(&node.node) { - Some((t, a)) if timestamp() <= t + AUTODISCOVERY_CACHE_DURATION => Ok(*a), - _ => { - let kv_key = format!("diplonat/autodiscovery/{}/{}", family, node.node); - let autodiscovery = consul.kv_get(&kv_key).await?; - - if let Some(json) = autodiscovery { - let a = serde_json::from_slice::<DiplonatAutodiscoveryResult<A>>(&json)?; - if timestamp() <= a.timestamp + AUTODISCOVERY_CACHE_DURATION { - if cache.get(&node.node).map(|x| x.1) != Some(a.address) { - info!( - "Got {} address for {} from diplonat autodiscovery: {:?}", - family, node.node, a.address - ); - } - cache.insert(node.node.clone(), (a.timestamp, a.address)); - return Ok(a.address); - } else { - warn!("{} address for {} from diplonat autodiscovery is outdated (value: {:?}), falling back on value from Consul node meta", family, node.node, a.address); - } + match autodiscovery.get(&node.node) { + Some(ar) if timestamp() <= ar.timestamp + AUTODISCOVERY_CACHE_DURATION => { + Ok(ar.address) + } + x => { + if let Some(ar) = x { + warn!("{} address for {} from diplonat autodiscovery is outdated (value: {:?}), falling back on value from Consul node meta", family, node.node, ar.address); } let meta_tag = format!("{}{}", IP_TARGET_METADATA_TAG_PREFIX, family); - let a = node.meta.get(&meta_tag).map(|x| x.parse()).transpose()?; - - if cache.get(&node.node).map(|x| x.1) != Some(a) { - info!( - "Got {} address for {} from Consul node meta: {:?}", - family, node.node, a - ); - } - cache.insert(node.node.clone(), (timestamp(), a)); - Ok(a) + let addr = node.meta.get(&meta_tag).map(|x| x.parse()).transpose()?; + Ok(addr) } } } } -// ---- util for interaction with diplonat ---- - -#[derive(Serialize, Deserialize, Debug)] -pub struct DiplonatAutodiscoveryResult<A> { - pub timestamp: u64, - pub address: Option<A>, -} - -fn timestamp() -> u64 { - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .expect("clock error") - .as_secs() -} - // ---- Display impls ---- impl std::fmt::Display for DnsRecordType { |