Skip to content

Commit

Permalink
refactor: Adjust tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernauer committed Oct 17, 2024
1 parent 59ca78b commit 23697ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
8 changes: 8 additions & 0 deletions crates/stackable-operator/src/commons/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl TryFrom<String> for DomainName {
}
}

impl TryFrom<&str> for DomainName {
type Error = validation::Errors;

fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}

impl From<DomainName> for String {
fn from(value: DomainName) -> Self {
value.0
Expand Down
47 changes: 30 additions & 17 deletions crates/stackable-operator/src/utils/cluster_domain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, path::Path, str::FromStr, sync::OnceLock};

use snafu::{OptionExt, ResultExt, Snafu};
use tracing::instrument;

use crate::commons::networking::DomainName;

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

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

match env::var(KUBERNETES_CLUSTER_DOMAIN_ENV) {
Ok(cluster_domain) if !cluster_domain.is_empty() => {
let cluster_domain = DomainName::from_str(&cluster_domain)
.context(ParseDomainNameSnafu { cluster_domain })?;
tracing::info!(
cluster_domain,
"Kubernetes cluster domain set by environment variable"
);
return DomainName::from_str(&cluster_domain)
.context(ParseDomainNameSnafu { cluster_domain });
}
_ => {
tracing::info!(
"The environment variable \"{KUBERNETES_CLUSTER_DOMAIN_ENV}\" is not set or empty"
%cluster_domain,
"Using Kubernetes cluster domain from {KUBERNETES_CLUSTER_DOMAIN_ENV} environment variable"
);
return Ok(cluster_domain);
}
_ => {}
};

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

match env::var(KUBERNETES_SERVICE_HOST_ENV) {
Ok(_) => {
let cluster_domain = retrieve_cluster_domain_from_resolv_conf(RESOLVE_CONF_FILE_PATH)?;
let cluster_domain = DomainName::from_str(&cluster_domain)
.context(ParseDomainNameSnafu { cluster_domain })?;

tracing::info!(
cluster_domain,
%cluster_domain,
"Using Kubernetes cluster domain from {RESOLVE_CONF_FILE_PATH} file"
);

DomainName::from_str(&cluster_domain).context(ParseDomainNameSnafu { cluster_domain })
Ok(cluster_domain)
}
Err(_) => {
let cluster_domain = KUBERNETES_CLUSTER_DOMAIN_DEFAULT;
tracing::info!(cluster_domain, "Using default Kubernetes cluster domain");
DomainName::from_str(cluster_domain).context(ParseDomainNameSnafu { cluster_domain })
let cluster_domain = DomainName::from_str(KUBERNETES_CLUSTER_DOMAIN_DEFAULT).context(
ParseDomainNameSnafu {
cluster_domain: KUBERNETES_CLUSTER_DOMAIN_DEFAULT,
},
)?;

tracing::info!(
%cluster_domain,
"Could not determine Kubernetes cluster domain as the operator is not running within Kubernetes, assuming default Kubernetes cluster domain"
);
Ok(cluster_domain)
}
}
}

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

Expand Down

0 comments on commit 23697ae

Please sign in to comment.