@@ -2,13 +2,14 @@ use std::{os::unix::prelude::AsRawFd, path::Path};
22
33use pin_project:: pin_project;
44use socket2:: Socket ;
5- use stackable_operator:: { commons:: listener:: AddressType , k8s_openapi:: api:: core:: v1:: Node } ;
65use tokio:: {
76 io:: { AsyncRead , AsyncWrite } ,
87 net:: { UnixListener , UnixStream } ,
98} ;
109use tonic:: transport:: server:: Connected ;
1110
11+ pub mod address;
12+
1213/// Adapter for using [`UnixStream`] as a [`tonic`] connection
1314/// Tonic usually communicates via TCP sockets, but the Kubernetes CSI interface expects
1415/// plugins to use Unix sockets instead.
@@ -101,52 +102,9 @@ pub fn error_full_message(err: &dyn std::error::Error) -> String {
101102 full_msg
102103}
103104
104- #[ derive( Debug , Clone , Copy ) ]
105- pub struct AddressCandidates < ' a > {
106- pub ip : Option < & ' a str > ,
107- pub hostname : Option < & ' a str > ,
108- }
109-
110- impl < ' a > AddressCandidates < ' a > {
111- pub fn pick ( & self , preferred_address_type : AddressType ) -> Option < ( & ' a str , AddressType ) > {
112- let ip = self . ip . zip ( Some ( AddressType :: Ip ) ) ;
113- let hostname = self . hostname . zip ( Some ( AddressType :: Hostname ) ) ;
114- match preferred_address_type {
115- AddressType :: Ip => ip. or ( hostname) ,
116- AddressType :: Hostname => hostname. or ( ip) ,
117- }
118- }
119- }
120-
121- /// Try to guess the primary address of a Node, which it is expected that external clients should be able to reach it on
122- pub fn node_primary_address ( node : & Node ) -> AddressCandidates {
123- let addrs = node
124- . status
125- . as_ref ( )
126- . and_then ( |s| s. addresses . as_deref ( ) )
127- . unwrap_or_default ( ) ;
128-
129- AddressCandidates {
130- ip : addrs
131- . iter ( )
132- . find ( |addr| addr. type_ == "ExternalIP" )
133- . or_else ( || addrs. iter ( ) . find ( |addr| addr. type_ == "InternalIP" ) )
134- . map ( |addr| addr. address . as_str ( ) ) ,
135- hostname : addrs
136- . iter ( )
137- . find ( |addr| addr. type_ == "Hostname" )
138- . map ( |addr| addr. address . as_str ( ) ) ,
139- }
140- }
141-
142105#[ cfg( test) ]
143106mod tests {
144- use stackable_operator:: {
145- commons:: listener:: AddressType ,
146- k8s_openapi:: api:: core:: v1:: { Node , NodeAddress , NodeStatus } ,
147- } ;
148-
149- use crate :: utils:: { error_full_message, node_primary_address} ;
107+ use crate :: utils:: error_full_message;
150108
151109 #[ test]
152110 fn error_messages ( ) {
@@ -164,71 +122,4 @@ mod tests {
164122 "leaf: middleware: root error"
165123 ) ;
166124 }
167-
168- #[ test]
169- fn node_with_only_ips_primary_address_returns_external_ip ( ) {
170- let node = node_from_addresses ( vec ! [ ( "InternalIP" , "10.1.2.3" ) , ( "ExternalIP" , "1.2.3.4" ) ] ) ;
171- let node_primary_address = node_primary_address ( & node) ;
172- assert_eq ! (
173- node_primary_address. pick( AddressType :: Ip ) ,
174- Some ( ( "1.2.3.4" , AddressType :: Ip ) )
175- ) ;
176- assert_eq ! (
177- node_primary_address. pick( AddressType :: Hostname ) ,
178- Some ( ( "1.2.3.4" , AddressType :: Ip ) )
179- ) ;
180- }
181-
182- #[ test]
183- fn node_with_only_hostname_primary_address_returns_hostname ( ) {
184- let node = node_from_addresses ( vec ! [
185- ( "Hostname" , "first-hostname" ) ,
186- ( "Hostname" , "second-hostname" ) ,
187- ] ) ;
188- let node_primary_address = node_primary_address ( & node) ;
189- assert_eq ! (
190- node_primary_address. pick( AddressType :: Ip ) ,
191- Some ( ( "first-hostname" , AddressType :: Hostname ) )
192- ) ;
193- assert_eq ! (
194- node_primary_address. pick( AddressType :: Hostname ) ,
195- Some ( ( "first-hostname" , AddressType :: Hostname ) )
196- ) ;
197- }
198-
199- #[ test]
200- fn node_with_hostname_and_ips_primary_address ( ) {
201- let node = node_from_addresses ( vec ! [
202- ( "Hostname" , "node-0" ) ,
203- ( "ExternalIP" , "1.2.3.4" ) ,
204- ( "InternalIP" , "10.1.2.3" ) ,
205- ] ) ;
206- let node_primary_address = node_primary_address ( & node) ;
207- assert_eq ! (
208- node_primary_address. pick( AddressType :: Ip ) ,
209- Some ( ( "1.2.3.4" , AddressType :: Ip ) )
210- ) ;
211- assert_eq ! (
212- node_primary_address. pick( AddressType :: Hostname ) ,
213- Some ( ( "node-0" , AddressType :: Hostname ) )
214- ) ;
215- }
216-
217- fn node_from_addresses < ' a > ( addresses : impl IntoIterator < Item = ( & ' a str , & ' a str ) > ) -> Node {
218- Node {
219- status : Some ( NodeStatus {
220- addresses : Some (
221- addresses
222- . into_iter ( )
223- . map ( |( ty, addr) | NodeAddress {
224- type_ : ty. to_string ( ) ,
225- address : addr. to_string ( ) ,
226- } )
227- . collect ( ) ,
228- ) ,
229- ..Default :: default ( )
230- } ) ,
231- ..Default :: default ( )
232- }
233- }
234125}
0 commit comments