-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathairflow_controller.rs
More file actions
185 lines (154 loc) · 5.62 KB
/
Copy pathairflow_controller.rs
File metadata and controls
185 lines (154 loc) · 5.62 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
//! 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,
k8s_openapi::api::core::v1::EnvVar,
kube::{
core::{DeserializeGuard, error_boundary},
runtime::controller::Action,
},
logging::controller::ReconcilerError,
shared::time::Duration,
};
use strum::{EnumDiscriminants, IntoStaticStr};
use crate::{
controller::{
apply::{self, Applier, ensure_random_secrets},
build,
update_status::{self, update_status},
},
crd::{OPERATOR_NAME, 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 the Kubernetes resources"))]
ApplyResources { source: apply::Error },
#[snafu(display("failed to build the Kubernetes resources"))]
BuildResources { source: build::Error },
#[snafu(display("failed to ensure the shared random Secrets exist"))]
EnsureSecrets { source: apply::Error },
#[snafu(display("failed to update the cluster status"))]
UpdateStatus { source: update_status::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 validated_cluster = crate::controller::validate::validate_cluster(
airflow,
&ctx.operator_environment.image_repository,
dereferenced,
)
.context(ValidateSnafu)?;
let resources = build::build(&validated_cluster).context(BuildResourcesSnafu)?;
ensure_random_secrets(client, &validated_cluster)
.await
.context(EnsureSecretsSnafu)?;
let applied = Applier::new(
client,
&validated_cluster,
ClusterResourceApplyStrategy::from(&airflow.spec.cluster_operation),
&airflow.spec.object_overrides,
)
.apply(resources)
.await
.context(ApplyResourcesSnafu)?;
update_status(client, airflow, &applied)
.await
.context(UpdateStatusSnafu)?;
Ok(Action::await_change())
}
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"]);
}
}