Skip to content

Commit 270277b

Browse files
committed
Use the local kubelet's cluster domain, rather than a random one
1 parent 903249c commit 270277b

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,35 @@ pub struct KubernetesClusterInfo {
1818
#[derive(clap::Parser, Debug, Default, PartialEq, Eq)]
1919
pub struct KubernetesClusterInfoOpts {
2020
/// Kubernetes cluster domain, usually this is `cluster.local`.
21-
// We are not using a default value here, as operators will probably do an more advanced
22-
// auto-detection of the cluster domain in case it is not specified in the future.
21+
// We are not using a default value here, as we query the cluster if it is not specified.
2322
#[arg(long, env)]
2423
pub kubernetes_cluster_domain: Option<DomainName>,
24+
25+
/// Name of the Kubernetes Node that the operator is running on.
26+
#[arg(long, env)]
27+
pub kubernetes_node_name: String,
2528
}
2629

2730
impl KubernetesClusterInfo {
2831
pub async fn new(
2932
client: &Client,
3033
cluster_info_opts: &KubernetesClusterInfoOpts,
3134
) -> Result<Self, Error> {
32-
let cluster_domain = match &cluster_info_opts.kubernetes_cluster_domain {
33-
Some(cluster_domain) => {
35+
let cluster_domain = match cluster_info_opts {
36+
KubernetesClusterInfoOpts {
37+
kubernetes_cluster_domain: Some(cluster_domain),
38+
..
39+
} => {
3440
tracing::info!(%cluster_domain, "Using configured Kubernetes cluster domain");
3541

3642
cluster_domain.clone()
3743
}
38-
None => {
39-
let kubelet_config = kubelet::KubeletConfig::fetch(client)
44+
KubernetesClusterInfoOpts {
45+
kubernetes_node_name: node_name,
46+
..
47+
} => {
48+
tracing::info!(%node_name, "Fetching Kubernetes cluster domain from the local kubelet");
49+
let kubelet_config = kubelet::KubeletConfig::fetch(client, node_name)
4050
.await
4151
.context(KubeletConfigSnafu)?;
4252

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
use http;
2-
use k8s_openapi::api::core::v1::Node;
3-
use kube::{
4-
Api,
5-
api::{ListParams, ResourceExt},
6-
client::Client,
7-
};
2+
use kube::client::Client;
83
use serde::Deserialize;
9-
use snafu::{OptionExt, ResultExt, Snafu};
4+
use snafu::{ResultExt, Snafu};
105

116
use crate::commons::networking::DomainName;
127

138
#[derive(Debug, Snafu)]
149
pub enum Error {
15-
#[snafu(display("failed to list nodes"))]
16-
ListNodes { source: kube::Error },
17-
1810
#[snafu(display("failed to build request for url path \"{url_path}\""))]
1911
BuildConfigzRequest {
2012
source: http::Error,
@@ -29,11 +21,6 @@ pub enum Error {
2921

3022
#[snafu(display("failed to deserialize kubelet config JSON"))]
3123
KubeletConfigJson { source: serde_json::Error },
32-
33-
#[snafu(display(
34-
"empty Kubernetes nodes list. At least one node is required to fetch the cluster domain from the kubelet config"
35-
))]
36-
EmptyKubernetesNodesList,
3724
}
3825

3926
#[derive(Debug, Deserialize)]
@@ -49,16 +36,8 @@ pub struct KubeletConfig {
4936
}
5037

5138
impl KubeletConfig {
52-
/// Fetches the kubelet configuration from the "first" node in the Kubernetes cluster.
53-
pub async fn fetch(client: &Client) -> Result<Self, Error> {
54-
let api: Api<Node> = Api::all(client.clone());
55-
let nodes = api
56-
.list(&ListParams::default())
57-
.await
58-
.context(ListNodesSnafu)?;
59-
let node = nodes.iter().next().context(EmptyKubernetesNodesListSnafu)?;
60-
let node_name = node.name_any();
61-
39+
/// Fetches the kubelet configuration from the specified node in the Kubernetes cluster.
40+
pub async fn fetch(client: &Client, node_name: &str) -> Result<Self, Error> {
6241
let url_path = format!("/api/v1/nodes/{node_name}/proxy/configz");
6342
let req = http::Request::get(url_path.clone())
6443
.body(Default::default())

0 commit comments

Comments
 (0)