@@ -179,7 +179,7 @@ impl SecretVolumeSelector {
179179 scope : & scope:: SecretScope ,
180180 ) -> Result < Vec < Address > , ScopeAddressesError > {
181181 use scope_addresses_error:: * ;
182- // Turn FQDNs into bare domain names by removing the trailing dot
182+ // Turn FQDNs into bare domain names by removing the trailing dots
183183 let cluster_domain = pod_info. kubernetes_cluster_domain . trim_end_matches ( "." ) ;
184184 let namespace = & self . namespace ;
185185 Ok ( match scope {
@@ -211,7 +211,7 @@ impl SecretVolumeSelector {
211211 . context ( NoListenerAddressesSnafu { listener : name } ) ?
212212 . iter ( )
213213 . map ( |addr| match addr {
214- // Turn FQDNs into bare domain names by removing the trailing dot
214+ // Turn FQDNs into bare domain names by removing the trailing dots
215215 Address :: Dns ( dns) => Address :: Dns ( dns. trim_end_matches ( "." ) . to_string ( ) ) ,
216216 _ => addr. clone ( ) ,
217217 } )
@@ -304,3 +304,114 @@ impl SecretBackendError for Infallible {
304304 match * self { }
305305 }
306306}
307+
308+ #[ cfg( test) ]
309+ mod tests {
310+ use std:: collections:: HashMap ;
311+
312+ use pod_info:: PodInfo ;
313+
314+ use super :: * ;
315+
316+ #[ test]
317+ fn test_scope_addresses_without_trailing_dot ( ) {
318+ let pod_info = construct_pod_info ( "cluster.local" ) ;
319+
320+ assert_eq ! (
321+ calculate_scope( & pod_info, & SecretScope :: Pod ) ,
322+ vec![
323+ dns( "my-sts.default.svc.cluster.local" ) ,
324+ dns( "my-sts-0.my-sts.default.svc.cluster.local" ) ,
325+ ip( "10.0.0.42" ) ,
326+ ]
327+ ) ;
328+
329+ assert_eq ! (
330+ calculate_scope(
331+ & pod_info,
332+ & SecretScope :: Service {
333+ name: "my-service" . to_owned( )
334+ }
335+ ) ,
336+ vec![ dns( "my-service.default.svc.cluster.local" ) , ]
337+ ) ;
338+
339+ assert_eq ! (
340+ calculate_scope( & pod_info, & SecretScope :: Node ) ,
341+ vec![ dns( "my-node" ) , ip( "192.168.0.1" ) , ]
342+ ) ;
343+ }
344+
345+ #[ test]
346+ fn test_scope_addresses_with_trailing_dot ( ) {
347+ let pod_info = construct_pod_info ( "custom.cluster.local." ) ;
348+
349+ assert_eq ! (
350+ calculate_scope( & pod_info, & SecretScope :: Pod ) ,
351+ vec![
352+ dns( "my-sts.default.svc.custom.cluster.local" ) ,
353+ dns( "my-sts-0.my-sts.default.svc.custom.cluster.local" ) ,
354+ ip( "10.0.0.42" ) ,
355+ ]
356+ ) ;
357+
358+ assert_eq ! (
359+ calculate_scope(
360+ & pod_info,
361+ & SecretScope :: Service {
362+ name: "my-service" . to_owned( )
363+ }
364+ ) ,
365+ vec![
366+ dns( "my-service.default.svc.custom.cluster.local" )
367+ ]
368+ ) ;
369+
370+ assert_eq ! (
371+ calculate_scope( & pod_info, & SecretScope :: Node ) ,
372+ vec![ dns( "my-node" ) , ip( "192.168.0.1" ) , ]
373+ ) ;
374+ }
375+
376+ fn construct_pod_info ( cluster_domain : & str ) -> PodInfo {
377+ PodInfo {
378+ pod_ips : vec ! [ "10.0.0.42" . parse( ) . unwrap( ) ] ,
379+ service_name : Some ( "my-sts" . to_owned ( ) ) ,
380+ node_name : "my-node" . to_owned ( ) ,
381+ node_ips : vec ! [ "192.168.0.1" . parse( ) . unwrap( ) ] ,
382+ listener_addresses : HashMap :: from ( [ ] ) ,
383+ kubernetes_cluster_domain : cluster_domain. parse ( ) . unwrap ( ) ,
384+ scheduling : SchedulingPodInfo {
385+ namespace : "default" . to_owned ( ) ,
386+ volume_listener_names : HashMap :: new ( ) ,
387+ has_node_scope : false ,
388+ } ,
389+ }
390+ }
391+
392+ fn calculate_scope ( pod_info : & PodInfo , scope : & SecretScope ) -> Vec < Address > {
393+ let secret_volume_selector = construct_secret_volume_selector ( ) ;
394+ secret_volume_selector
395+ . scope_addresses ( pod_info, scope)
396+ . unwrap ( )
397+ }
398+
399+ fn dns ( dns : & str ) -> Address {
400+ Address :: Dns ( dns. to_owned ( ) )
401+ }
402+
403+ fn ip ( ip : & str ) -> Address {
404+ Address :: Ip ( ip. parse ( ) . unwrap ( ) )
405+ }
406+
407+ fn construct_secret_volume_selector ( ) -> SecretVolumeSelector {
408+ serde_yaml:: from_str (
409+ r#"
410+ secrets.stackable.tech/class: tls
411+ csi.storage.k8s.io/pod.name: my-sts-0
412+ csi.storage.k8s.io/pod.namespace: default
413+ "# ,
414+ )
415+ . unwrap ( )
416+ }
417+ }
0 commit comments