-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmod.rs
More file actions
360 lines (330 loc) · 13 KB
/
Copy pathmod.rs
File metadata and controls
360 lines (330 loc) · 13 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Builders that assemble Kubernetes resources from the validated cluster.
use snafu::{ResultExt, Snafu};
use stackable_operator::v2::types::operator::RoleGroupName;
use crate::{
controller::{
KubernetesResources, ValidatedCluster,
build::resource::{
config_map,
executor::build_executor_template_config_map,
listener::build_group_listener,
pdb::build_pdb,
rbac::{build_role_binding, build_service_account},
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
statefulset::build_server_rolegroup_statefulset,
},
executor_role_group_name, executor_role_name,
},
crd::{AirflowConfigOverrides, Container},
};
pub mod graceful_shutdown;
pub mod properties;
pub mod resource;
pub mod volumes;
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to build ConfigMap for role group {role_group}"))]
ConfigMap {
source: resource::config_map::Error,
role_group: RoleGroupName,
},
#[snafu(display("failed to build StatefulSet for role group {role_group}"))]
StatefulSet {
source: resource::statefulset::Error,
role_group: RoleGroupName,
},
#[snafu(display("failed to build the Kubernetes-executor pod-template ConfigMap"))]
ExecutorTemplate { source: resource::executor::Error },
}
/// Builds every Kubernetes resource for the given validated cluster.
///
/// Does not need a Kubernetes client: every reference to another Kubernetes resource is already
/// dereferenced and validated by this point. Cluster configuration is likewise already validated,
/// so the errors returned here are resource-assembly failures only.
pub fn build(cluster: &ValidatedCluster) -> Result<KubernetesResources, Error> {
let mut stateful_sets = vec![];
let mut services = vec![];
let mut listeners = vec![];
let mut config_maps = vec![];
let mut pod_disruption_budgets = vec![];
// The Kubernetes-executor pod template (only built for the Kubernetes executor; the Celery
// executor's workers are a regular role with its own role groups instead).
if let Some(executor_template) = &cluster.cluster_config.executor_template {
let executor_role_group = executor_role_group_name();
let executor_config_map = config_map::build_rolegroup_config_map(
cluster,
&executor_role_name(),
&executor_role_group,
// The Kubernetes-executor pod template does not apply webserver_config.py overrides.
&AirflowConfigOverrides::default(),
&executor_template.config.logging,
&Container::Base,
)
.context(ConfigMapSnafu {
role_group: executor_role_group,
})?;
config_maps.push(executor_config_map);
let executor_template_config_map = build_executor_template_config_map(
cluster,
&executor_template.config,
&executor_template.env_overrides,
&executor_template.pod_overrides,
)
.context(ExecutorTemplateSnafu)?;
config_maps.push(executor_template_config_map);
}
for (role, role_group_configs) in &cluster.role_groups {
if let Some(role_config) = cluster.role_configs.get(role) {
if let Some(pdb_config) = &role_config.pdb {
pod_disruption_budgets.extend(build_pdb(pdb_config, cluster, role));
}
if let Some(listener_class) = &role_config.listener_class
&& let Some(group_listener_name) = &role_config.group_listener_name
{
listeners.push(build_group_listener(
cluster,
role,
listener_class.clone(),
group_listener_name.clone(),
));
}
}
for (role_group_name, rg_config) in role_group_configs {
let logging = &rg_config.config.logging;
services.push(build_rolegroup_headless_service(
cluster,
role,
role_group_name,
));
services.push(build_rolegroup_metrics_service(
cluster,
role,
role_group_name,
));
config_maps.push(
config_map::build_rolegroup_config_map(
cluster,
&ValidatedCluster::role_name(role),
role_group_name,
&rg_config.config_overrides,
logging,
&Container::Airflow,
)
.context(ConfigMapSnafu {
role_group: role_group_name.clone(),
})?,
);
stateful_sets.push(
build_server_rolegroup_statefulset(
cluster,
role,
role_group_name,
rg_config,
logging,
)
.context(StatefulSetSnafu {
role_group: role_group_name.clone(),
})?,
);
}
}
Ok(KubernetesResources {
stateful_sets,
services,
listeners,
config_maps,
pod_disruption_budgets,
service_accounts: vec![build_service_account(cluster)],
role_bindings: vec![build_role_binding(cluster)],
})
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use stackable_operator::kube::Resource;
use super::build;
use crate::{
controller::{
ValidatedCluster, dereference::DereferencedObjects, validate::validate_cluster,
},
crd::{
authentication::{AirflowClientAuthenticationDetailsResolved, FlaskRolesSyncMoment},
authorization::AirflowAuthorizationResolved,
v1alpha2,
},
};
/// A validated cluster with default `webserver`/`scheduler` role groups and the given executor
/// (its `spec` key plus config, as standalone YAML), built via `validate_cluster` from a
/// minimal test CR (mirroring `validate::tests::test_cluster`), since `ValidatedCluster`
/// carries several resolved types (git-sync resources, validated logging, …) that are
/// impractical to construct by hand.
fn validated_cluster(executor_key: &str, executor_config: &str) -> ValidatedCluster {
let cluster_yaml = r#"
apiVersion: airflow.stackable.tech/v1alpha2
kind: AirflowCluster
metadata:
name: my-airflow
namespace: default
uid: e6ac237d-a6d4-43a1-8135-f36506110912
spec:
image:
productVersion: 3.1.6
clusterConfig:
loadExamples: false
exposeConfig: false
credentialsSecretName: airflow-admin-credentials
metadataDatabase:
postgresql:
host: airflow-postgresql
database: airflow
credentialsSecretName: airflow-postgresql-credentials
webservers:
config: {}
roleGroups:
default:
config: {}
schedulers:
config: {}
roleGroups:
default:
config: {}
"#;
// The executor block is inserted into the parsed document rather than spliced into the
// YAML text, so the fixture does not depend on matching the template's indentation.
let mut cluster_value: serde_yaml::Value =
serde_yaml::from_str(cluster_yaml).expect("the test CR is valid YAML");
cluster_value["spec"]
.as_mapping_mut()
.expect("the test CR has a spec mapping")
.insert(
executor_key.into(),
serde_yaml::from_str(executor_config).expect("the executor config is valid YAML"),
);
let cluster: v1alpha2::AirflowCluster =
serde_yaml::with::singleton_map_recursive::deserialize(cluster_value)
.expect("the test CR deserialises");
let dereferenced = DereferencedObjects {
authentication_config: AirflowClientAuthenticationDetailsResolved {
authentication_classes_resolved: vec![],
user_registration: true,
user_registration_role: "Public".to_string(),
sync_roles_at: FlaskRolesSyncMoment::default(),
},
authorization_config: AirflowAuthorizationResolved { opa: None },
};
validate_cluster(&cluster, "oci.stackable.tech/sdp", dereferenced)
.expect("test cluster validates")
}
/// Validated cluster with a Celery executor (its workers are provisioned via the queue, so no
/// executor pod template is built).
fn celery_executor_cluster() -> ValidatedCluster {
validated_cluster("celeryExecutors", "{config: {}, roleGroups: {}}")
}
/// Validated cluster with a Kubernetes executor, which builds an executor pod-template
/// ConfigMap instead of a worker role.
fn kubernetes_executor_cluster() -> ValidatedCluster {
validated_cluster("kubernetesExecutors", "{config: {}}")
}
fn sorted_names(resources: &[impl Resource]) -> Vec<&str> {
let mut names: Vec<&str> = resources
.iter()
.filter_map(|resource| resource.meta().name.as_deref())
.collect();
names.sort();
names
}
#[test]
fn build_produces_expected_resource_names() {
let cluster = celery_executor_cluster();
let resources = build(&cluster).expect("build succeeds");
assert_eq!(
sorted_names(&resources.stateful_sets),
[
"my-airflow-scheduler-default",
"my-airflow-webserver-default"
]
);
// One headless and one metrics Service per role group.
assert_eq!(resources.services.len(), 4);
assert_eq!(
sorted_names(&resources.config_maps),
[
"my-airflow-scheduler-default",
"my-airflow-webserver-default"
]
);
// The webserver is the only role with a group Listener.
assert_eq!(sorted_names(&resources.listeners), ["my-airflow-webserver"]);
// A default PDB per role (the Celery worker included).
assert_eq!(
sorted_names(&resources.pod_disruption_budgets),
[
"my-airflow-scheduler",
"my-airflow-webserver",
"my-airflow-worker"
]
);
}
/// Locks the RBAC resource names, the roleRef, and the recommended label set against
/// accidental drift. The fixture's cluster name deliberately differs from the product name so
/// that swapped `name`/`instance` label values cannot pass unnoticed.
#[test]
fn build_produces_rbac() {
let cluster = celery_executor_cluster();
let resources = build(&cluster).expect("build succeeds");
assert_eq!(
sorted_names(&resources.service_accounts),
["my-airflow-serviceaccount"]
);
assert_eq!(
sorted_names(&resources.role_bindings),
["my-airflow-rolebinding"]
);
let expected_labels = BTreeMap::from(
[
("app.kubernetes.io/component", "none"),
("app.kubernetes.io/instance", "my-airflow"),
(
"app.kubernetes.io/managed-by",
"airflow.stackable.tech_airflowcluster",
),
("app.kubernetes.io/name", "airflow"),
("app.kubernetes.io/role-group", "none"),
("app.kubernetes.io/version", "3.1.6-stackable0.0.0-dev"),
("stackable.tech/vendor", "Stackable"),
]
.map(|(key, value)| (key.to_string(), value.to_string())),
);
let service_account = resources
.service_accounts
.first()
.expect("a ServiceAccount is built");
assert_eq!(
service_account.metadata.labels,
Some(expected_labels.clone())
);
let role_binding = resources
.role_bindings
.first()
.expect("a RoleBinding is built");
assert_eq!(role_binding.metadata.labels, Some(expected_labels));
assert_eq!(role_binding.role_ref.name, "airflow-clusterrole");
}
/// The Kubernetes-executor branch of `build()` (moved here from `reconcile`) additionally emits
/// the executor role-group ConfigMap and the executor pod-template ConfigMap; the Celery case
/// does not.
#[test]
fn build_kubernetes_executor_adds_pod_template_config_maps() {
let cluster = kubernetes_executor_cluster();
let resources = build(&cluster).expect("build succeeds");
assert_eq!(
sorted_names(&resources.config_maps),
[
"my-airflow-executor-kubernetes",
"my-airflow-executor-pod-template",
"my-airflow-scheduler-default",
"my-airflow-webserver-default",
]
);
}
}