|
| 1 | +use http; |
1 | 2 | use k8s_openapi::api::core::v1::Node;
|
2 | 3 | use kube::{
|
3 | 4 | Api,
|
4 | 5 | api::{ListParams, ResourceExt},
|
5 | 6 | client::Client,
|
6 | 7 | };
|
7 | 8 | use serde::Deserialize;
|
8 |
| -use http; |
9 |
| -use snafu::{Snafu, ResultExt, OptionExt}; |
| 9 | +use snafu::{OptionExt, ResultExt, Snafu}; |
| 10 | + |
| 11 | +use crate::commons::networking::DomainName; |
10 | 12 |
|
11 | 13 | #[derive(Debug, Snafu)]
|
12 | 14 | pub enum Error {
|
13 |
| - |
14 | 15 | #[snafu(display("failed to list nodes"))]
|
15 |
| - ListNodes { |
16 |
| - source: kube::Error, |
17 |
| - }, |
| 16 | + ListNodes { source: kube::Error }, |
18 | 17 | #[snafu(display("failed to build proxy/configz request"))]
|
19 |
| - ConfigzRequest { |
20 |
| - source: http::Error, |
21 |
| - }, |
| 18 | + ConfigzRequest { source: http::Error }, |
22 | 19 |
|
23 | 20 | #[snafu(display("failed to fetch kubelet config from node {node}"))]
|
24 |
| - FetchNodeKubeletConfig { |
25 |
| - source: kube::Error, |
26 |
| - node: String, |
27 |
| - }, |
| 21 | + FetchNodeKubeletConfig { source: kube::Error, node: String }, |
28 | 22 |
|
29 | 23 | #[snafu(display("failed to fetch `kubeletconfig` JSON key from configz response"))]
|
30 | 24 | KubeletConfigJsonKey,
|
31 | 25 |
|
32 | 26 | #[snafu(display("failed to deserialize kubelet config JSON"))]
|
33 |
| - KubeletConfigJson { |
34 |
| - source: serde_json::Error, |
35 |
| - }, |
| 27 | + KubeletConfigJson { source: serde_json::Error }, |
36 | 28 |
|
37 | 29 | #[snafu(display("empty Kubernetes nodes list"))]
|
38 | 30 | EmptyKubernetesNodesList,
|
39 | 31 | }
|
40 | 32 |
|
41 | 33 | #[derive(Debug, Deserialize)]
|
42 | 34 | #[serde(rename_all = "camelCase")]
|
43 |
| -struct KubeletConfig { |
44 |
| - cluster_domain: String, |
| 35 | +pub struct KubeletConfig { |
| 36 | + pub cluster_domain: DomainName, |
45 | 37 | }
|
46 | 38 |
|
47 | 39 | impl KubeletConfig {
|
48 | 40 | pub async fn fetch(client: &Client) -> Result<Self, Error> {
|
49 | 41 | let api: Api<Node> = Api::all(client.clone());
|
50 |
| - let nodes = api.list(&ListParams::default()).await.context(ListNodesSnafu)?; |
| 42 | + let nodes = api |
| 43 | + .list(&ListParams::default()) |
| 44 | + .await |
| 45 | + .context(ListNodesSnafu)?; |
51 | 46 | let node = nodes.iter().next().context(EmptyKubernetesNodesListSnafu)?;
|
52 | 47 |
|
53 | 48 | let name = node.name_any();
|
54 | 49 |
|
55 |
| - // Query node stats by issuing a request to the admin endpoint. |
56 |
| - // See https://kubernetes.io/docs/reference/instrumentation/node-metrics/ |
| 50 | + // Query kukbelet config |
57 | 51 | let url = format!("/api/v1/nodes/{}/proxy/configz", name);
|
58 |
| - let req = http::Request::get(url).body(Default::default()).context(ConfigzRequestSnafu)?; |
| 52 | + let req = http::Request::get(url) |
| 53 | + .body(Default::default()) |
| 54 | + .context(ConfigzRequestSnafu)?; |
59 | 55 |
|
60 | 56 | // Deserialize JSON response as a JSON value. Alternatively, a type that
|
61 | 57 | // implements `Deserialize` can be used.
|
62 |
| - let resp = client.request::<serde_json::Value>(req).await.context(FetchNodeKubeletConfigSnafu { node: name })?; |
| 58 | + let resp = client |
| 59 | + .request::<serde_json::Value>(req) |
| 60 | + .await |
| 61 | + .context(FetchNodeKubeletConfigSnafu { node: name })?; |
63 | 62 |
|
64 | 63 | // Our JSON value is an object so we can treat it like a dictionary.
|
65 | 64 | let summary = resp
|
66 | 65 | .get("kubeletconfig")
|
67 | 66 | .context(KubeletConfigJsonKeySnafu)?;
|
68 | 67 |
|
69 |
| - // The base JSON representation includes a lot of metrics, including |
70 |
| - // container metrics. Use a `NodeMetrics` type to deserialize only the |
71 |
| - // values we care about. |
72 | 68 | serde_json::from_value::<KubeletConfig>(summary.to_owned()).context(KubeletConfigJsonSnafu)
|
73 | 69 | }
|
74 |
| - |
75 | 70 | }
|
0 commit comments