Support for loading IP addresses from DiploNAT autodiscovery #4

Merged
lx merged 6 commits from diplonat-autodiscovery into main 2023-04-21 14:00:06 +00:00
Showing only changes of commit 49d94dae1d - Show all commits

View file

@ -13,13 +13,13 @@ use tracing::*;
use df_consul::*;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct DiplonatAutodiscoveryResult<A> {
pub timestamp: u64,
pub address: Option<A>,
}
#[derive(Default, Debug)]
#[derive(Default, Debug, Eq, PartialEq)]
pub struct AutodiscoveredAddresses {
pub ipv4: HashMap<String, DiplonatAutodiscoveryResult<Ipv4Addr>>,
pub ipv6: HashMap<String, DiplonatAutodiscoveryResult<Ipv6Addr>>,
@ -30,6 +30,7 @@ pub fn watch_autodiscovered_ips(
mut must_exit: watch::Receiver<bool>,
) -> watch::Receiver<Arc<AutodiscoveredAddresses>> {
let (tx, rx) = watch::channel(Arc::new(AutodiscoveredAddresses::default()));
let rx2 = rx.clone();
tokio::spawn(async move {
let mut last_index = None;
@ -66,9 +67,13 @@ pub fn watch_autodiscovered_ips(
}
}
debug!("Autodiscovered addresses fetched from Consul: {:?}", addresses);
if addresses.strip_timestamps() != rx2.borrow().strip_timestamps() {
addresses.dump();
}
if tx.send(Arc::new(addresses)).is_err() {
info!("Autodiscovered addresses watcher terminating");
return;
}
}
});
@ -103,6 +108,24 @@ fn parse_autodiscovered_address(
Ok(())
}
impl AutodiscoveredAddresses {
fn strip_timestamps(&self) -> (HashMap<&str, Option<Ipv4Addr>>, HashMap<&str, Option<Ipv6Addr>>) {
(self.ipv4.iter().map(|(k, v)| (k.as_str(), v.address)).collect(),
self.ipv6.iter().map(|(k, v)| (k.as_str(), v.address)).collect())
}
fn dump(&self) {
println!("---- Autodiscovered addresses (fetched from DiploNAT): ----");
for (k, v) in self.ipv4.iter() {
println!(" IPv4 {} {} {:?}", k, v.timestamp, v.address);
}
for (k, v) in self.ipv6.iter() {
println!(" IPv6 {} {} {:?}", k, v.timestamp, v.address);
}
println!("");
}
}
pub fn timestamp() -> u64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)