-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: automatic cluster detection #1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
592fdc1
77e8e1b
dd4b5d1
f6b4aef
5fe91bd
ecd63cd
ea12f24
357a62d
2e71f67
4a07fd3
ca69256
a5120a2
3338c56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use http; | ||
use k8s_openapi::api::core::v1::Node; | ||
use kube::{ | ||
Api, | ||
api::{ListParams, ResourceExt}, | ||
client::Client, | ||
}; | ||
use serde::Deserialize; | ||
use snafu::{OptionExt, ResultExt, Snafu}; | ||
|
||
use crate::commons::networking::DomainName; | ||
|
||
#[derive(Debug, Snafu)] | ||
pub enum Error { | ||
#[snafu(display("failed to list nodes"))] | ||
ListNodes { source: kube::Error }, | ||
|
||
#[snafu(display("failed to build request for url path \"{url_path}\""))] | ||
BuildConfigzRequest { | ||
source: http::Error, | ||
url_path: String, | ||
}, | ||
|
||
#[snafu(display("failed to fetch kubelet config from node {node:?}"))] | ||
FetchNodeKubeletConfig { source: kube::Error, node: String }, | ||
|
||
#[snafu(display("failed to fetch `kubeletconfig` JSON key from configz response"))] | ||
KubeletConfigJsonKey, | ||
|
||
#[snafu(display("failed to deserialize kubelet config JSON"))] | ||
KubeletConfigJson { source: serde_json::Error }, | ||
|
||
#[snafu(display( | ||
"empty Kubernetes nodes list. At least one node is required to fetch the cluster domain from the kubelet config" | ||
))] | ||
EmptyKubernetesNodesList, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct ProxyConfigResponse { | ||
kubeletconfig: KubeletConfig, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct KubeletConfig { | ||
pub cluster_domain: DomainName, | ||
} | ||
|
||
impl KubeletConfig { | ||
/// Fetches the kubelet configuration from the "first" node in the Kubernetes cluster. | ||
pub async fn fetch(client: &Client) -> Result<Self, Error> { | ||
let api: Api<Node> = Api::all(client.clone()); | ||
let nodes = api | ||
.list(&ListParams::default()) | ||
.await | ||
.context(ListNodesSnafu)?; | ||
let node = nodes.iter().next().context(EmptyKubernetesNodesListSnafu)?; | ||
let node_name = node.name_any(); | ||
Comment on lines
+54
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can inject this through the downwards API instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slick! forgot about that :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do a followup PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, good idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
let url_path = format!("/api/v1/nodes/{node_name}/proxy/configz"); | ||
let req = http::Request::get(url_path.clone()) | ||
.body(Default::default()) | ||
.context(BuildConfigzRequestSnafu { url_path })?; | ||
|
||
let resp = client | ||
.request::<ProxyConfigResponse>(req) | ||
.await | ||
.context(FetchNodeKubeletConfigSnafu { node: node_name })?; | ||
|
||
Ok(resp.kubeletconfig) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod bash; | ||
pub mod cluster_info; | ||
pub mod crds; | ||
pub mod kubelet; | ||
pub mod logging; | ||
mod option; | ||
mod url; | ||
|
Uh oh!
There was an error while loading. Please reload this page.