Skip to content

Commit 60cb372

Browse files
authored
fix: Fix Kubernetes cluster domain parsing from resolv.conf (#895)
* fix: Fix Kubernetes cluster domain parsing from resolv.conf * changelog * changelog * Rename l -> entry
1 parent a2ac5f5 commit 60cb372

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

crates/stackable-operator/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Fixed
8+
9+
- Fix Kubernetes cluster domain parsing from resolv.conf, e.g. on AWS EKS.
10+
We now only consider Kubernetes services domains instead of all domains (which could include non-Kubernetes domains) ([#895]).
11+
12+
[#895]: https://github.com/stackabletech/operator-rs/pull/895
13+
714
## [0.79.0] - 2024-10-18
815

916
### Added
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
search cluster.local
2+
nameserver 10.243.21.53
3+
options ndots:5
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
search default.svc.cluster.local svc.cluster.local cluster.local ec2.internal
2+
nameserver 172.20.0.10
3+
options ndots:5

crates/stackable-operator/src/utils/cluster_domain.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{env, path::PathBuf, str::FromStr, sync::OnceLock};
22

3-
use snafu::{ResultExt, Snafu};
3+
use snafu::{OptionExt, ResultExt, Snafu};
44
use tracing::instrument;
55

66
use 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

Comments
 (0)