1
1
use std:: { env, path:: PathBuf , str:: FromStr , sync:: OnceLock } ;
2
2
3
- use snafu:: { ResultExt , Snafu } ;
3
+ use snafu:: { OptionExt , ResultExt , Snafu } ;
4
4
use tracing:: instrument;
5
5
6
6
use crate :: commons:: networking:: DomainName ;
@@ -25,8 +25,10 @@ pub enum Error {
25
25
#[ snafu( display( r#"unable to find "search" entry"# ) ) ]
26
26
NoSearchEntry ,
27
27
28
- #[ snafu( display( r#"unable to find unambiguous domain in "search" entry"# ) ) ]
29
- AmbiguousDomainEntries ,
28
+ #[ snafu( display(
29
+ r#"unable to find the Kubernetes service domain, which needs to start with "svc.""#
30
+ ) ) ]
31
+ FindKubernetesServiceDomain ,
30
32
}
31
33
32
34
/// Tries to retrieve the Kubernetes cluster domain.
@@ -118,24 +120,25 @@ fn retrieve_cluster_domain_from_resolv_conf(
118
120
} )
119
121
. context ( ReadResolvConfFileSnafu ) ?;
120
122
121
- // If there are multiple search directives, only the search
122
- // man 5 resolv.conf
123
- let Some ( last_search_entry) = content
123
+ // If there are multiple search directives, only the last search directive is relevant.
124
+ // See ` man 5 resolv.conf`
125
+ let last_search_entry = content
124
126
. lines ( )
125
127
. rev ( )
126
- . map ( |l| l . trim ( ) )
127
- . find ( |& l| l . starts_with ( "search" ) )
128
- . map ( |l| l . trim_start_matches ( "search" ) . trim ( ) )
129
- else {
130
- return NoSearchEntrySnafu . fail ( ) ;
131
- } ;
132
-
133
- let Some ( shortest_entry) = last_search_entry
128
+ . map ( |entry| entry . trim ( ) )
129
+ . find ( |& entry| entry . starts_with ( "search" ) )
130
+ . map ( |entry| entry . trim_start_matches ( "search" ) . trim ( ) )
131
+ . context ( NoSearchEntrySnafu ) ? ;
132
+
133
+ // We only care about entries starting with "svc." to limit the entries to the ones used by
134
+ // Kubernetes for Services.
135
+ let shortest_entry = last_search_entry
134
136
. split_ascii_whitespace ( )
135
- . min_by_key ( |item| item. len ( ) )
136
- else {
137
- return AmbiguousDomainEntriesSnafu . fail ( ) ;
138
- } ;
137
+ // Normally there should only be one such entry, but we take the first on in any case.
138
+ . find ( |& entry| entry. starts_with ( "svc." ) )
139
+ // Strip the "svc." prefix to get only the cluster domain.
140
+ . map ( |entry| entry. trim_start_matches ( "svc." ) . trim_end ( ) )
141
+ . context ( FindKubernetesServiceDomainSnafu ) ?;
139
142
140
143
// NOTE (@Techassi): This is really sad and bothers me more than I would like to admit. This
141
144
// clone could be removed by using the code directly in the calling function. But that would
0 commit comments