Skip to content

Commit 2e71f67

Browse files
committed
move kubelet query to cluster_info mod
1 parent 357a62d commit 2e71f67

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

crates/stackable-operator/src/client.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ use tracing::trace;
2121

2222
use crate::{
2323
kvp::LabelSelectorExt,
24-
utils::{
25-
cluster_info::{KubernetesClusterInfo, KubernetesClusterInfoOpts},
26-
kubelet,
27-
},
24+
utils::cluster_info::{KubernetesClusterInfo, KubernetesClusterInfoOpts},
2825
};
2926

3027
pub type Result<T, E = Error> = std::result::Result<T, E>;
@@ -88,8 +85,10 @@ pub enum Error {
8885
#[snafu(display("unable to create kubernetes client"))]
8986
CreateKubeClient { source: kube::Error },
9087

91-
#[snafu(display("unable to fetch kubelet config"))]
92-
KubeletConfig { source: kubelet::Error },
88+
#[snafu(display("unable to fetch cluster information from kubelet"))]
89+
NewKubeletClusterInfo {
90+
source: crate::utils::cluster_info::Error,
91+
},
9392
}
9493

9594
/// This `Client` can be used to access Kubernetes.
@@ -657,25 +656,9 @@ pub async fn initialize_operator(
657656
.context(InferKubeConfigSnafu)?;
658657
let default_namespace = kubeconfig.default_namespace.clone();
659658
let client = kube::Client::try_from(kubeconfig).context(CreateKubeClientSnafu)?;
660-
661-
let local_cluster_info_opts = match cluster_info_opts.kubernetes_cluster_domain {
662-
None => {
663-
trace!("Cluster domain not set, fetching kubelet config to determine cluster domain.");
664-
665-
let kubelet_config = kubelet::KubeletConfig::fetch(&client)
666-
.await
667-
.context(KubeletConfigSnafu)?;
668-
669-
KubernetesClusterInfoOpts {
670-
kubernetes_cluster_domain: Some(kubelet_config.cluster_domain),
671-
}
672-
}
673-
_ => KubernetesClusterInfoOpts {
674-
kubernetes_cluster_domain: cluster_info_opts.kubernetes_cluster_domain.clone(),
675-
},
676-
};
677-
678-
let cluster_info = KubernetesClusterInfo::new(&local_cluster_info_opts);
659+
let cluster_info = KubernetesClusterInfo::new(&client, cluster_info_opts)
660+
.await
661+
.context(NewKubeletClusterInfoSnafu)?;
679662

680663
Ok(Client::new(
681664
client,
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
use std::str::FromStr;
1+
use kube::Client;
2+
use snafu::{ResultExt, Snafu};
23

3-
use crate::commons::networking::DomainName;
4+
use crate::{commons::networking::DomainName, utils::kubelet};
45

5-
const KUBERNETES_CLUSTER_DOMAIN_DEFAULT: &str = "cluster.local";
6+
#[derive(Debug, Snafu)]
7+
pub enum Error {
8+
#[snafu(display("unable to fetch kubelet config"))]
9+
KubeletConfig { source: kubelet::Error },
10+
}
611

7-
/// Some information that we know about the Kubernetes cluster.
812
#[derive(Debug, Clone)]
913
pub struct KubernetesClusterInfo {
1014
/// The Kubernetes cluster domain, typically `cluster.local`.
@@ -21,25 +25,28 @@ pub struct KubernetesClusterInfoOpts {
2125
}
2226

2327
impl KubernetesClusterInfo {
24-
pub fn new(cluster_info_opts: &KubernetesClusterInfoOpts) -> Self {
28+
pub async fn new(
29+
client: &Client,
30+
cluster_info_opts: &KubernetesClusterInfoOpts,
31+
) -> Result<Self, Error> {
2532
let cluster_domain = match &cluster_info_opts.kubernetes_cluster_domain {
2633
Some(cluster_domain) => {
2734
tracing::info!(%cluster_domain, "Using configured Kubernetes cluster domain");
2835

2936
cluster_domain.clone()
3037
}
3138
None => {
32-
// TODO(sbernauer): Do some sort of advanced auto-detection, see https://github.com/stackabletech/issues/issues/436.
33-
// There have been attempts of parsing the `/etc/resolv.conf`, but they have been
34-
// reverted. Please read on the linked issue for details.
35-
let cluster_domain = DomainName::from_str(KUBERNETES_CLUSTER_DOMAIN_DEFAULT)
36-
.expect("KUBERNETES_CLUSTER_DOMAIN_DEFAULT constant must a valid domain");
37-
tracing::info!(%cluster_domain, "Defaulting Kubernetes cluster domain as it has not been configured");
39+
let kubelet_config = kubelet::KubeletConfig::fetch(client)
40+
.await
41+
.context(KubeletConfigSnafu)?;
42+
43+
let cluster_domain = kubelet_config.cluster_domain;
44+
tracing::info!(%cluster_domain, "Using Kubernetes cluster domain from the kubelet config");
3845

3946
cluster_domain
4047
}
4148
};
4249

43-
Self { cluster_domain }
50+
Ok(Self { cluster_domain })
4451
}
4552
}

0 commit comments

Comments
 (0)