-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathairflow_controller.rs
More file actions
314 lines (272 loc) · 9.65 KB
/
Copy pathairflow_controller.rs
File metadata and controls
314 lines (272 loc) · 9.65 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
//! Ensures that `Pod`s are configured and running for each [`v1alpha2::AirflowCluster`]
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};
use const_format::concatcp;
use snafu::{ResultExt, Snafu};
use stackable_operator::{
cli::OperatorEnvironmentOptions,
cluster_resources::ClusterResourceApplyStrategy,
commons::random_secret_creation,
k8s_openapi::api::core::v1::EnvVar,
kube::{
core::{DeserializeGuard, error_boundary},
runtime::controller::Action,
},
logging::controller::ReconcilerError,
shared::time::Duration,
status::condition::{
compute_conditions, operations::ClusterOperationsConditionBuilder,
statefulset::StatefulSetConditionBuilder,
},
v2::cluster_resources::cluster_resources_new,
};
use strum::{EnumDiscriminants, IntoStaticStr};
use crate::{
controller::{ValidatedCluster, build, controller_name, operator_name, product_name},
crd::{
AirflowClusterStatus, OPERATOR_NAME,
internal_secret::{
FERNET_KEY_SECRET_KEY, INTERNAL_SECRET_SECRET_KEY, JWT_SECRET_SECRET_KEY,
},
v1alpha2,
},
};
pub const AIRFLOW_CONTROLLER_NAME: &str = "airflowcluster";
pub const CONTAINER_IMAGE_BASE_NAME: &str = "airflow";
pub const AIRFLOW_FULL_CONTROLLER_NAME: &str =
concatcp!(AIRFLOW_CONTROLLER_NAME, '.', OPERATOR_NAME);
pub struct Ctx {
pub client: stackable_operator::client::Client,
pub operator_environment: OperatorEnvironmentOptions,
}
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[snafu(visibility(pub(crate)))]
pub enum Error {
#[snafu(display("failed to apply Kubernetes resource"))]
ApplyResource {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to build the Kubernetes resources"))]
BuildResources { source: build::Error },
#[snafu(display("failed to delete orphaned resources"))]
DeleteOrphanedResources {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to update status"))]
ApplyStatus {
source: stackable_operator::client::Error,
},
#[snafu(display("failed to create internal secret"))]
InternalSecret {
source: random_secret_creation::Error,
},
#[snafu(display("failed to dereference cluster resources"))]
Dereference {
source: crate::controller::dereference::Error,
},
#[snafu(display("failed to validate cluster configuration"))]
Validate {
source: crate::controller::validate::Error,
},
#[snafu(display("AirflowCluster object is invalid"))]
InvalidAirflowCluster {
source: error_boundary::InvalidObject,
},
}
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
impl ReconcilerError for Error {
fn category(&self) -> &'static str {
ErrorDiscriminants::from(self).into()
}
}
pub async fn reconcile_airflow(
airflow: Arc<DeserializeGuard<v1alpha2::AirflowCluster>>,
ctx: Arc<Ctx>,
) -> Result<Action> {
tracing::info!("Starting reconcile");
let airflow = airflow
.0
.as_ref()
.map_err(error_boundary::InvalidObject::clone)
.context(InvalidAirflowClusterSnafu)?;
let client = &ctx.client;
let dereferenced = crate::controller::dereference::dereference(client, airflow)
.await
.context(DereferenceSnafu)?;
let cluster_operation_cond_builder =
ClusterOperationsConditionBuilder::new(&airflow.spec.cluster_operation);
let validated_cluster = crate::controller::validate::validate_cluster(
airflow,
&ctx.operator_environment.image_repository,
dereferenced,
)
.context(ValidateSnafu)?;
ensure_random_secrets(client, &validated_cluster).await?;
let mut cluster_resources = cluster_resources_new(
&product_name(),
&operator_name(),
&controller_name(),
&validated_cluster.name,
&validated_cluster.namespace,
&validated_cluster.uid,
ClusterResourceApplyStrategy::from(&airflow.spec.cluster_operation),
&airflow.spec.object_overrides,
);
let resources = build::build(&validated_cluster).context(BuildResourcesSnafu)?;
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
// Apply order is: StatefulSets last (a changed mounted ConfigMap/Secret
// must exist first, else Pods restart -- commons-operator#111). The ServiceAccount comes
// first because the Pods reference it at creation time.
for service_account in resources.service_accounts {
cluster_resources
.add(client, service_account)
.await
.context(ApplyResourceSnafu)?;
}
for role_binding in resources.role_bindings {
cluster_resources
.add(client, role_binding)
.await
.context(ApplyResourceSnafu)?;
}
for service in resources.services {
cluster_resources
.add(client, service)
.await
.context(ApplyResourceSnafu)?;
}
for listener in resources.listeners {
cluster_resources
.add(client, listener)
.await
.context(ApplyResourceSnafu)?;
}
for config_map in resources.config_maps {
cluster_resources
.add(client, config_map)
.await
.context(ApplyResourceSnafu)?;
}
for pdb in resources.pod_disruption_budgets {
cluster_resources
.add(client, pdb)
.await
.context(ApplyResourceSnafu)?;
}
for statefulset in resources.stateful_sets {
ss_cond_builder.add(
cluster_resources
.add(client, statefulset)
.await
.context(ApplyResourceSnafu)?,
);
}
cluster_resources
.delete_orphaned_resources(client)
.await
.context(DeleteOrphanedResourcesSnafu)?;
let status = AirflowClusterStatus {
conditions: compute_conditions(
airflow,
&[&ss_cond_builder, &cluster_operation_cond_builder],
),
};
client
.apply_patch_status(OPERATOR_NAME, airflow, &status)
.await
.context(ApplyStatusSnafu)?;
Ok(Action::await_change())
}
/// Ensures the three shared random Secrets (internal / JWT / Fernet) exist, creating any that are
/// missing. These are read-or-create client operations, so they cannot be part of the client-free
/// `build()` step.
async fn ensure_random_secrets(
client: &stackable_operator::client::Client,
cluster: &ValidatedCluster,
) -> Result<(), Error> {
random_secret_creation::create_random_secret_if_not_exists(
cluster.internal_secret_name().as_ref(),
INTERNAL_SECRET_SECRET_KEY,
256,
cluster,
client,
)
.await
.context(InternalSecretSnafu)?;
random_secret_creation::create_random_secret_if_not_exists(
cluster.jwt_secret_name().as_ref(),
JWT_SECRET_SECRET_KEY,
256,
cluster,
client,
)
.await
.context(InternalSecretSnafu)?;
// https://airflow.apache.org/docs/apache-airflow/stable/security/secrets/fernet.html#security-fernet
// does not document how long the fernet key should be, but recommends using
// python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
// which returns 32 bytes.
random_secret_creation::create_random_secret_if_not_exists(
cluster.fernet_key_name().as_ref(),
FERNET_KEY_SECRET_KEY,
32,
cluster,
client,
)
.await
.context(InternalSecretSnafu)?;
Ok(())
}
pub fn error_policy(
_obj: Arc<DeserializeGuard<v1alpha2::AirflowCluster>>,
error: &Error,
_ctx: Arc<Ctx>,
) -> Action {
match error {
// root object is invalid, will be requeued when modified anyway
Error::InvalidAirflowCluster { .. } => Action::await_change(),
_ => Action::requeue(*Duration::from_secs(10)),
}
}
/// Convert user-supplied `envOverrides` into a list of [`EnvVar`]s.
pub(crate) fn env_vars_from_overrides(env_overrides: &HashMap<String, String>) -> Vec<EnvVar> {
// Collect into a `BTreeMap` first so the env vars come out in a deterministic (sorted) order;
// `HashMap` iteration order is randomised per instance and would otherwise churn the containers
// this feeds between reconciles. Mirrors the override handling in `env_vars.rs`.
env_overrides
.iter()
.collect::<BTreeMap<_, _>>()
.into_iter()
.map(|(k, v)| EnvVar {
name: k.clone(),
value: Some(v.clone()),
..EnvVar::default()
})
.collect()
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::env_vars_from_overrides;
/// The env vars must come out in a deterministic (sorted-by-name) order. `env_overrides` is a
/// `HashMap`, whose iteration order is randomised per instance, so iterating it directly would
/// vary the rendered env array between reconciles and churn the git-sync containers it feeds.
#[test]
fn env_vars_from_overrides_are_sorted_by_name() {
let overrides = HashMap::from([
("CHARLIE".to_string(), "3".to_string()),
("ALPHA".to_string(), "1".to_string()),
("ECHO".to_string(), "5".to_string()),
("BRAVO".to_string(), "2".to_string()),
("DELTA".to_string(), "4".to_string()),
]);
let names: Vec<String> = env_vars_from_overrides(&overrides)
.into_iter()
.map(|env_var| env_var.name)
.collect();
assert_eq!(names, ["ALPHA", "BRAVO", "CHARLIE", "DELTA", "ECHO"]);
}
}