Skip to content

Commit 23697ae

Browse files
committed
refactor: Adjust tracing
1 parent 59ca78b commit 23697ae

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

crates/stackable-operator/src/commons/networking.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ impl TryFrom<String> for DomainName {
3030
}
3131
}
3232

33+
impl TryFrom<&str> for DomainName {
34+
type Error = validation::Errors;
35+
36+
fn try_from(value: &str) -> Result<Self, Self::Error> {
37+
value.parse()
38+
}
39+
}
40+
3341
impl From<DomainName> for String {
3442
fn from(value: DomainName) -> Self {
3543
value.0

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

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

33
use snafu::{OptionExt, ResultExt, Snafu};
4+
use tracing::instrument;
45

56
use crate::commons::networking::DomainName;
67

@@ -56,52 +57,64 @@ pub enum Error {
5657
/// - <https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/>
5758
pub static KUBERNETES_CLUSTER_DOMAIN: OnceLock<DomainName> = OnceLock::new();
5859

60+
#[instrument]
5961
pub(crate) fn retrieve_cluster_domain() -> Result<DomainName, Error> {
6062
// 1. Read KUBERNETES_CLUSTER_DOMAIN env var
61-
tracing::info!("Trying to determine the Kubernetes cluster domain...");
63+
tracing::debug!("Trying to determine the Kubernetes cluster domain...");
6264

6365
match env::var(KUBERNETES_CLUSTER_DOMAIN_ENV) {
6466
Ok(cluster_domain) if !cluster_domain.is_empty() => {
67+
let cluster_domain = DomainName::from_str(&cluster_domain)
68+
.context(ParseDomainNameSnafu { cluster_domain })?;
6569
tracing::info!(
66-
cluster_domain,
67-
"Kubernetes cluster domain set by environment variable"
68-
);
69-
return DomainName::from_str(&cluster_domain)
70-
.context(ParseDomainNameSnafu { cluster_domain });
71-
}
72-
_ => {
73-
tracing::info!(
74-
"The environment variable \"{KUBERNETES_CLUSTER_DOMAIN_ENV}\" is not set or empty"
70+
%cluster_domain,
71+
"Using Kubernetes cluster domain from {KUBERNETES_CLUSTER_DOMAIN_ENV} environment variable"
7572
);
73+
return Ok(cluster_domain);
7674
}
75+
_ => {}
7776
};
7877

7978
// 2. If no env var is set, check if we run in a clustered (Kubernetes/Openshift) environment
8079
// by checking if KUBERNETES_SERVICE_HOST is set: If not default to 'cluster.local'.
81-
tracing::info!("Trying to determine the operator runtime environment...");
80+
tracing::debug!(
81+
"Trying to determine the operator runtime environment as environment variable \
82+
\"{KUBERNETES_CLUSTER_DOMAIN_ENV}\" is not set"
83+
);
8284

8385
match env::var(KUBERNETES_SERVICE_HOST_ENV) {
8486
Ok(_) => {
8587
let cluster_domain = retrieve_cluster_domain_from_resolv_conf(RESOLVE_CONF_FILE_PATH)?;
88+
let cluster_domain = DomainName::from_str(&cluster_domain)
89+
.context(ParseDomainNameSnafu { cluster_domain })?;
8690

8791
tracing::info!(
88-
cluster_domain,
92+
%cluster_domain,
8993
"Using Kubernetes cluster domain from {RESOLVE_CONF_FILE_PATH} file"
9094
);
9195

92-
DomainName::from_str(&cluster_domain).context(ParseDomainNameSnafu { cluster_domain })
96+
Ok(cluster_domain)
9397
}
9498
Err(_) => {
95-
let cluster_domain = KUBERNETES_CLUSTER_DOMAIN_DEFAULT;
96-
tracing::info!(cluster_domain, "Using default Kubernetes cluster domain");
97-
DomainName::from_str(cluster_domain).context(ParseDomainNameSnafu { cluster_domain })
99+
let cluster_domain = DomainName::from_str(KUBERNETES_CLUSTER_DOMAIN_DEFAULT).context(
100+
ParseDomainNameSnafu {
101+
cluster_domain: KUBERNETES_CLUSTER_DOMAIN_DEFAULT,
102+
},
103+
)?;
104+
105+
tracing::info!(
106+
%cluster_domain,
107+
"Could not determine Kubernetes cluster domain as the operator is not running within Kubernetes, assuming default Kubernetes cluster domain"
108+
);
109+
Ok(cluster_domain)
98110
}
99111
}
100112
}
101113

114+
#[instrument]
102115
fn retrieve_cluster_domain_from_resolv_conf<P>(path: P) -> Result<String, Error>
103116
where
104-
P: AsRef<Path>,
117+
P: std::fmt::Debug + AsRef<Path>,
105118
{
106119
let content = std::fs::read_to_string(path).context(ReadResolvConfFileSnafu)?;
107120

0 commit comments

Comments
 (0)