11use std:: { env, path:: PathBuf , str:: FromStr , sync:: OnceLock } ;
22
3- use snafu:: { ResultExt , Snafu } ;
3+ use snafu:: { OptionExt , ResultExt , Snafu } ;
44use tracing:: instrument;
55
66use crate :: commons:: networking:: DomainName ;
@@ -25,8 +25,10 @@ pub enum Error {
2525 #[ snafu( display( r#"unable to find "search" entry"# ) ) ]
2626 NoSearchEntry ,
2727
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 ,
3032}
3133
3234/// Tries to retrieve the Kubernetes cluster domain.
@@ -118,24 +120,25 @@ fn retrieve_cluster_domain_from_resolv_conf(
118120 } )
119121 . context ( ReadResolvConfFileSnafu ) ?;
120122
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
124126 . lines ( )
125127 . 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
134136 . 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 ) ?;
139142
140143 // NOTE (@Techassi): This is really sad and bothers me more than I would like to admit. This
141144 // clone could be removed by using the code directly in the calling function. But that would
0 commit comments