-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecutor.rs
More file actions
204 lines (178 loc) · 6.83 KB
/
Copy pathexecutor.rs
File metadata and controls
204 lines (178 loc) · 6.83 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
//! Builds the Kubernetes-executor pod-template [`ConfigMap`]: a `ConfigMap` whose single entry is
//! the serialized Pod template Airflow uses to provision one Pod per task.
use std::collections::HashMap;
use snafu::{ResultExt, Snafu};
use stackable_operator::{
builder::{
configmap::ConfigMapBuilder,
meta::ObjectMetaBuilder,
pod::{PodBuilder, security::PodSecurityContextBuilder},
},
k8s_openapi::{
DeepMerge,
api::core::v1::{ConfigMap, PodTemplateSpec},
},
kvp::{Label, LabelError},
v2::{
builder::pod::container::new_container_builder,
product_logging::framework::STACKABLE_LOG_DIR,
},
};
use crate::{
controller::{
ValidatedAirflowConfig, ValidatedCluster,
build::{
graceful_shutdown::add_graceful_shutdown_config,
properties::env_vars::build_airflow_template_envs,
resource::pod::{
add_authentication_volumes_and_volume_mounts, add_git_sync_resources,
build_logging_container,
},
volumes::{self, CONFIG_VOLUME_NAME, LOG_CONFIG_VOLUME_NAME, LOG_VOLUME_NAME},
},
executor_role_group_name, executor_role_name, executor_template_role_group_name,
},
crd::{CONFIG_PATH, Container, LOG_CONFIG_DIR, TEMPLATE_NAME},
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to configure graceful shutdown"))]
GracefulShutdown {
source: crate::controller::build::graceful_shutdown::Error,
},
#[snafu(display("failed to add needed volume"))]
AddVolume {
source: stackable_operator::builder::pod::Error,
},
#[snafu(display("failed to add needed volumeMount"))]
AddVolumeMount {
source: stackable_operator::builder::pod::container::Error,
},
#[snafu(display("failed to build label"))]
BuildLabel { source: LabelError },
#[snafu(display("pod template serialization"))]
PodTemplateSerde { source: serde_yaml::Error },
#[snafu(display("failed to build the pod template config map"))]
PodTemplateConfigMap {
source: stackable_operator::builder::configmap::Error,
},
#[snafu(display("failed to build shared pod resources"))]
Pod {
source: crate::controller::build::resource::pod::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
pub fn build_executor_template_config_map(
cluster: &ValidatedCluster,
executor_config: &ValidatedAirflowConfig,
env_overrides: &HashMap<String, String>,
pod_overrides: &PodTemplateSpec,
) -> Result<ConfigMap> {
let resolved_product_image = &cluster.image;
let authentication_config = &cluster.cluster_config.authentication_config;
// The git-sync resources were resolved up-front during validation; read them off the validated
// executor config rather than reconstructing them here.
let git_sync_resources = &executor_config.git_sync_resources;
let mut pb = PodBuilder::new();
let pb_metadata =
ObjectMetaBuilder::new()
.with_labels(cluster.recommended_labels_for(
&executor_role_name(),
&executor_template_role_group_name(),
))
.build();
pb.metadata(pb_metadata)
.image_pull_secrets_from_product_image(resolved_product_image)
.affinity(&executor_config.affinity)
.service_account_name(
cluster
.cluster_resource_names()
.service_account_name()
.to_string(),
)
.restart_policy("Never")
.security_context(PodSecurityContextBuilder::new().fs_group(1000).build());
add_graceful_shutdown_config(executor_config.graceful_shutdown_timeout, &mut pb)
.context(GracefulShutdownSnafu)?;
// N.B. this "base" name is an airflow requirement and should not be changed!
// See https://airflow.apache.org/docs/apache-airflow-providers-cncf-kubernetes/8.4.0/kubernetes_executor.html#base-image
let mut airflow_container = new_container_builder(&Container::Base.to_container_name());
add_authentication_volumes_and_volume_mounts(
authentication_config,
&mut airflow_container,
&mut pb,
)
.context(PodSnafu)?;
airflow_container
.image_from_product_image(resolved_product_image)
.resources(executor_config.resources.clone().into())
.add_env_vars(build_airflow_template_envs(
cluster,
env_overrides,
&executor_config.logging,
git_sync_resources,
))
.add_volume_mounts(cluster.volume_mounts())
.context(AddVolumeMountSnafu)?
.add_volume_mount(&*CONFIG_VOLUME_NAME, CONFIG_PATH)
.context(AddVolumeMountSnafu)?
.add_volume_mount(&*LOG_CONFIG_VOLUME_NAME, LOG_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount(&*LOG_VOLUME_NAME, STACKABLE_LOG_DIR)
.context(AddVolumeMountSnafu)?;
add_git_sync_resources(
&mut pb,
&mut airflow_container,
git_sync_resources,
false,
true,
)
.context(PodSnafu)?;
cluster
.metadata_database_connection_details()
.add_to_container(&mut airflow_container);
pb.add_container(airflow_container.build());
pb.add_volumes(cluster.volumes().clone())
.context(AddVolumeSnafu)?;
pb.add_volumes(volumes::create_volumes(
cluster
.role_group_resource_names(&executor_role_name(), &executor_role_group_name())
.role_group_config_map()
.as_ref(),
&executor_config.logging.product_container,
))
.context(AddVolumeSnafu)?;
if let Some(vector_log_config) = &executor_config.logging.vector_container {
pb.add_container(build_logging_container(
resolved_product_image,
vector_log_config,
&cluster.role_group_resource_names(
&executor_role_name(),
&executor_template_role_group_name(),
),
));
}
let mut pod_template = pb.build_template();
pod_template.merge_from(pod_overrides.clone());
let mut cm_builder = ConfigMapBuilder::new();
let restarter_label =
Label::try_from(("restarter.stackable.tech/enabled", "true")).context(BuildLabelSnafu)?;
cm_builder
.metadata(
cluster
.object_meta(
cluster.executor_template_configmap_name(),
cluster.recommended_labels_for(
&executor_role_name(),
&executor_template_role_group_name(),
),
)
.with_label(restarter_label)
.build(),
)
.add_data(
TEMPLATE_NAME,
serde_yaml::to_string(&pod_template).context(PodTemplateSerdeSnafu)?,
);
cm_builder.build().context(PodTemplateConfigMapSnafu)
}