-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
182 lines (155 loc) · 5.71 KB
/
mod.rs
File metadata and controls
182 lines (155 loc) · 5.71 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use indexmap::IndexMap;
use kube::{ResourceExt, core::GroupVersionKind};
use serde::Serialize;
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::status::condition::ClusterCondition;
use tracing::info;
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::{
constants::PRODUCTS,
platform::{credentials, service},
utils::k8s::{self, Client, ConditionsExt},
};
mod grafana;
mod minio;
mod opensearch;
mod prometheus;
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct Stacklet {
/// Name of the stacklet.
pub name: String,
/// Some CRDs are cluster scoped.
pub namespace: Option<String>,
/// Name of the product.
pub product: String,
/// Endpoint addresses the product is reachable at.
/// The key is the service name (e.g. `web-ui`), the value is the URL.
pub endpoints: IndexMap<String, String>,
/// Multiple cluster conditions.
pub conditions: Vec<k8s::DisplayCondition>,
}
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to create Kubernetes client"))]
KubeClientCreate { source: k8s::Error },
#[snafu(display("failed to fetch data from the Kubernetes API"))]
KubeClientFetch { source: k8s::Error },
#[snafu(display("no namespace set for custom resource {crd_name:?}"))]
CustomCrdNamespace { crd_name: String },
#[snafu(display("failed to deserialize cluster conditions from JSON"))]
DeserializeConditions { source: serde_json::Error },
#[snafu(display("failed to receive service information"))]
ServiceFetch { source: service::Error },
#[snafu(display("product name {product_name:?} not found"))]
GetProduct { product_name: String },
}
/// Lists all installed stacklets. If `namespace` is [`None`], stacklets from ALL
/// namespaces are returned. If `namespace` is [`Some`], only stacklets installed
/// in the specified namespace are returned. The `options` allow further
/// customization of the returned information.
pub async fn list_stacklets(
client: &Client,
namespace: Option<&str>,
) -> Result<Vec<Stacklet>, Error> {
let mut stacklets = list_stackable_stacklets(client, namespace).await?;
stacklets.extend(grafana::list(client, namespace).await?);
stacklets.extend(minio::list(client, namespace).await?);
stacklets.extend(opensearch::list(client, namespace).await?);
stacklets.extend(prometheus::list(client, namespace).await?);
Ok(stacklets)
}
pub async fn get_credentials_for_product(
client: &Client,
namespace: &str,
object_name: &str,
product_name: &str,
) -> Result<Option<credentials::Credentials>, Error> {
let product_gvk = gvk_from_product_name(product_name)?;
let product_cluster = match client
.get_namespaced_object(namespace, object_name, &product_gvk)
.await
.context(KubeClientFetchSnafu)?
{
Some(obj) => obj,
None => {
info!(
"Failed to retrieve credentials because the gvk {product_gvk:?} cannot be resolved"
);
return Ok(None);
}
};
let credentials = match credentials::get(client, product_name, &product_cluster).await {
Ok(credentials) => credentials,
Err(credentials::Error::NoSecret) => None,
Err(credentials::Error::KubeClientFetch { source }) => {
return Err(Error::KubeClientFetch { source });
}
};
Ok(credentials)
}
async fn list_stackable_stacklets(
client: &Client,
namespace: Option<&str>,
) -> Result<Vec<Stacklet>, Error> {
let mut stacklets = Vec::new();
for (product_name, group, version, kind) in PRODUCTS {
let product_gvk = GroupVersionKind {
group: group.to_string(),
version: version.to_string(),
kind: kind.to_string(),
};
let objects = match client
.list_objects(&product_gvk, namespace)
.await
.context(KubeClientFetchSnafu)?
{
Some(obj) => obj,
None => {
info!(
"Failed to list stacklets because the gvk {product_gvk:?} can not be resolved"
);
continue;
}
};
for object in objects {
let conditions: Vec<ClusterCondition> = match object.data.pointer("/status/conditions")
{
Some(conditions) => serde_json::from_value(conditions.clone())
.context(DeserializeConditionsSnafu)?,
None => vec![],
};
let object_name = object.name_any();
let object_namespace = match object.namespace() {
Some(ns) => ns,
// If the custom resource does not have a namespace set it can't expose a service
None => continue,
};
let endpoints =
service::get_endpoints(client, product_name, &object_name, &object_namespace)
.await
.context(ServiceFetchSnafu)?;
stacklets.push(Stacklet {
namespace: Some(object_namespace),
product: product_name.to_string(),
conditions: conditions.plain(),
name: object_name,
endpoints,
});
}
}
Ok(stacklets)
}
fn gvk_from_product_name(product_name: &str) -> Result<GroupVersionKind, Error> {
let (_, group, version, kind) = PRODUCTS
.iter()
.find(|(other_product_name, _, _, _)| product_name == *other_product_name)
.context(GetProductSnafu { product_name })?;
Ok(GroupVersionKind {
group: group.to_string(),
version: version.to_string(),
kind: kind.to_string(),
})
}