Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with Docker dns servers #1290

Merged
merged 11 commits into from
Feb 14, 2024
51 changes: 51 additions & 0 deletions crates/ott-balancer/src/discovery/dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use async_trait::async_trait;
use tracing::info;
use trust_dns_resolver::TokioAsyncResolver;

use super::*;

#[derive(Debug, Clone, Deserialize)]
pub struct DockerDiscoveryConfig {
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
/// The port that monoliths should be listening on for load balancer connections.
pub monolith_port: u16,
pub dns_server: Option<String>,
}

pub struct DockerMonolithDiscoverer {
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
config: DockerDiscoveryConfig,
query: Option<String>,
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
}

impl DockerMonolithDiscoverer {
pub fn new(config: DockerDiscoveryConfig) -> Self {
info!(
"Creating DockerMonolithDiscoverer, Docker DNS server: {}",
&config.docker_dns_server
moreno-michael marked this conversation as resolved.
Show resolved Hide resolved
);
let query = format!("{}", &config.dns_server);
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
Self { config, query }
}
}

#[async_trait]
impl MonolithDiscoverer for DockerMonolithDiscoverer {
async fn discover(&mut self) -> anyhow::Result<Vec<MonolithConnectionConfig>> {
let resolver =
TokioAsyncResolver::tokio_from_system_conf().expect("failed to create resolver");
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
moreno-michael marked this conversation as resolved.
Show resolved Hide resolved

let lookup = resolver.ipv4_lookup(&self.query).await?;
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
let monoliths = lookup
.iter()
.map(|ip| MonolithConnectionConfig {
host: HostOrIp::Ip(IpAddr::V4(*ip)),
port: self.config.monolith_port,
})
.collect::<Vec<_>>();

Ok(monoliths)
}

fn mode(&self) -> DiscoveryMode {
DiscoveryMode::Polling(Duration::from_secs(10))
}
dyc3 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading