-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathkubelet.rs
More file actions
69 lines (56 loc) · 2.02 KB
/
Copy pathkubelet.rs
File metadata and controls
69 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 proxy/configz request"))]
ConfigzRequest { source: http::Error },
#[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"))]
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 name = node.name_any();
let url = format!("/api/v1/nodes/{}/proxy/configz", name);
let req = http::Request::get(url)
.body(Default::default())
.context(ConfigzRequestSnafu)?;
let resp = client
.request::<ProxyConfigResponse>(req)
.await
.context(FetchNodeKubeletConfigSnafu { node: name })?;
Ok(resp.kubeletconfig)
}
}