1
+ use std:: str:: FromStr ;
2
+
1
3
use async_trait:: async_trait;
2
4
use hickory_resolver:: {
3
5
config:: { NameServerConfig , Protocol , ResolverConfig , ResolverOpts } ,
4
6
TokioAsyncResolver ,
5
7
} ;
8
+ use serde:: Deserializer ;
6
9
use tracing:: info;
7
10
8
11
use super :: * ;
@@ -12,6 +15,8 @@ pub struct DnsDiscoveryConfig {
12
15
/// The port that monoliths should be listening on for load balancer connections.
13
16
pub service_port : u16 ,
14
17
/// 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 ) ]
15
20
pub dns_server : Option < SocketAddr > ,
16
21
/// The A record to query. If using docker-compose, this should be the service name for the monolith.
17
22
pub query : String ,
@@ -21,6 +26,21 @@ pub struct DnsDiscoveryConfig {
21
26
pub polling_interval : Option < Duration > ,
22
27
}
23
28
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
+
24
44
pub struct DnsServiceDiscoverer {
25
45
config : DnsDiscoveryConfig ,
26
46
}
@@ -76,7 +96,7 @@ mod test {
76
96
use serde_json:: json;
77
97
78
98
#[ test]
79
- fn server_deserializes_correctly ( ) {
99
+ fn dns_server_deserializes_correctly ( ) {
80
100
let json = json ! ( {
81
101
"service_port" : 8080 ,
82
102
"dns_server" : "127.0.0.1:100" ,
@@ -88,4 +108,43 @@ mod test {
88
108
89
109
assert_eq ! ( config. dns_server, Some ( ( [ 127 , 0 , 0 , 1 ] , 100 ) . into( ) ) ) ;
90
110
}
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
+ }
91
150
}
0 commit comments