Skip to content

Commit 0dda9be

Browse files
cjrkoadyc3
andauthored
add deserialization to SocketAddr struct to make port for dns_config optional (#1564)
* deserialize SocketAddr for dns_config * cargo fmt * clippy * clippy * clippy x3 * oops * update string parsing * Update crates/ott-common/src/discovery/dns.rs Co-authored-by: Carson McManus <[email protected]> * I can't get the port to be completely optional * clippy * Add default parsing for dns_server * throw error when all deserialization attempts fail * change default * changes --------- Co-authored-by: Carson McManus <[email protected]>
1 parent b84a8d9 commit 0dda9be

File tree

1 file changed

+60
-1
lines changed
  • crates/ott-common/src/discovery

1 file changed

+60
-1
lines changed

crates/ott-common/src/discovery/dns.rs

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::str::FromStr;
2+
13
use async_trait::async_trait;
24
use hickory_resolver::{
35
config::{NameServerConfig, Protocol, ResolverConfig, ResolverOpts},
46
TokioAsyncResolver,
57
};
8+
use serde::Deserializer;
69
use tracing::info;
710

811
use super::*;
@@ -12,6 +15,8 @@ pub struct DnsDiscoveryConfig {
1215
/// The port that monoliths should be listening on for load balancer connections.
1316
pub service_port: u16,
1417
/// The DNS server to query. Optional. If not provided, the system configuration will be used instead.
18+
#[serde(deserialize_with = "deserialize_dns_server")]
19+
#[serde(default)]
1520
pub dns_server: Option<SocketAddr>,
1621
/// The A record to query. If using docker-compose, this should be the service name for the monolith.
1722
pub query: String,
@@ -21,6 +26,21 @@ pub struct DnsDiscoveryConfig {
2126
pub polling_interval: Option<Duration>,
2227
}
2328

29+
fn deserialize_dns_server<'de, D>(deserializer: D) -> Result<Option<SocketAddr>, D::Error>
30+
where
31+
D: Deserializer<'de>,
32+
{
33+
let buf = String::deserialize(deserializer)?;
34+
35+
match IpAddr::from_str(&buf) {
36+
Ok(ip) => Ok(Some(SocketAddr::new(ip, 53))),
37+
Err(_) => match SocketAddr::from_str(&buf) {
38+
Ok(socket) => Ok(Some(socket)),
39+
Err(e) => Err(serde::de::Error::custom(e)),
40+
},
41+
}
42+
}
43+
2444
pub struct DnsServiceDiscoverer {
2545
config: DnsDiscoveryConfig,
2646
}
@@ -76,7 +96,7 @@ mod test {
7696
use serde_json::json;
7797

7898
#[test]
79-
fn server_deserializes_correctly() {
99+
fn dns_server_deserializes_correctly() {
80100
let json = json!({
81101
"service_port": 8080,
82102
"dns_server": "127.0.0.1:100",
@@ -88,4 +108,43 @@ mod test {
88108

89109
assert_eq!(config.dns_server, Some(([127, 0, 0, 1], 100).into()));
90110
}
111+
112+
#[test]
113+
fn dns_server_deserialization_defaults_to_port_53() {
114+
let json = json!({
115+
"service_port": 8080,
116+
"dns_server": "127.0.0.1",
117+
"query": "".to_string(),
118+
});
119+
120+
let config: DnsDiscoveryConfig =
121+
serde_json::from_value(json).expect("Failed to deserialize json");
122+
123+
assert_eq!(config.dns_server, Some(([127, 0, 0, 1], 53).into()));
124+
}
125+
126+
#[test]
127+
fn dns_server_is_optional() {
128+
let json = json!({
129+
"service_port": 8080,
130+
"query": "".to_string(),
131+
});
132+
133+
let config: DnsDiscoveryConfig =
134+
serde_json::from_value(json).expect("Failed to deserialize json");
135+
136+
assert!(config.dns_server.is_none())
137+
}
138+
139+
#[test]
140+
fn dns_server_failed_deserialization_throws_error() {
141+
let json = json!({
142+
"not": "valid",
143+
"DnsDiscoveryConfig": true,
144+
});
145+
146+
let config: Result<DnsDiscoveryConfig, serde_json::Error> = serde_json::from_value(json);
147+
148+
assert!(config.is_err())
149+
}
91150
}

0 commit comments

Comments
 (0)