-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
231 lines (206 loc) · 8.12 KB
/
Copy pathmod.rs
File metadata and controls
231 lines (206 loc) · 8.12 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
//! Build steps that turn a `ValidatedCluster` into Kubernetes resources.
use std::str::FromStr;
use snafu::{ResultExt, Snafu};
use stackable_operator::v2::types::operator::RoleGroupName;
use crate::{
controller::{
KubernetesResources,
build::resource::{
config_map::build_rolegroup_config_map,
listener::{build_group_listener, group_listener_name},
pdb::build_pdb,
rbac::{build_role_binding, build_service_account},
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
statefulset::build_rolegroup_statefulset,
},
validate::ValidatedCluster,
},
crd::DruidRole,
};
// Placeholder role-group name used for the recommended labels of the role-level discovery
// `ConfigMap` (which is not tied to a single role group).
stackable_operator::constant!(pub(crate) PLACEHOLDER_DISCOVERY_ROLE_GROUP: RoleGroupName = "discovery");
// Placeholder role-group name used for the recommended labels of the role-level `Listener`
// (which is not tied to a single role group).
stackable_operator::constant!(pub(crate) NONE_ROLE_GROUP_NAME: RoleGroupName = "none");
pub mod authentication;
pub mod graceful_shutdown;
pub mod jvm;
pub mod properties;
pub mod resource;
pub mod security;
#[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,
},
}
/// Builds the Kubernetes resources 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.
/// The remaining errors are resource-assembly failures only.
///
/// The Router group `Listener` and the discovery `ConfigMap`s are not built here: the discovery
/// `ConfigMap` derives from the *applied* Router listener's ingress address, so both are built and
/// applied in the reconcile step instead.
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![];
for (druid_role, role_group_configs) in &cluster.role_group_configs {
let role_config = cluster.role_config(druid_role);
if let Some(pdb) = build_pdb(&role_config.pdb, cluster, druid_role) {
pod_disruption_budgets.push(pdb);
}
// The Router group Listener is built and applied in the reconcile step instead (see the
// module docs and [`KubernetesResources`]), so it is skipped here.
if *druid_role != DruidRole::Router
&& let Some(listener_class) = &role_config.listener_class
&& let Some(listener_group_name) = group_listener_name(cluster, druid_role)
{
listeners.push(build_group_listener(
cluster,
listener_class,
listener_group_name,
druid_role,
));
}
for (role_group_name, rg) in role_group_configs {
services.push(build_rolegroup_headless_service(
cluster,
druid_role,
role_group_name,
));
services.push(build_rolegroup_metrics_service(
cluster,
druid_role,
role_group_name,
));
config_maps.push(
build_rolegroup_config_map(cluster, druid_role, role_group_name, rg).context(
ConfigMapSnafu {
role_group: role_group_name.clone(),
},
)?,
);
stateful_sets.push(
build_rolegroup_statefulset(cluster, druid_role, role_group_name, rg).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::validate::test_support::{
MINIMAL_DRUID_YAML, druid_from_yaml, validated_cluster,
};
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 druid = druid_from_yaml(MINIMAL_DRUID_YAML);
let cluster = validated_cluster(&druid);
let resources = build(&cluster).expect("build succeeds");
// One StatefulSet and one ConfigMap per role group (one role group per role).
let expected_role_group_names = [
"simple-druid-broker-default",
"simple-druid-coordinator-default",
"simple-druid-historical-default",
"simple-druid-middlemanager-default",
"simple-druid-router-default",
];
assert_eq!(
sorted_names(&resources.stateful_sets),
expected_role_group_names
);
assert_eq!(
sorted_names(&resources.config_maps),
expected_role_group_names
);
// Group Listeners are built for the externally reachable roles except the Router (whose
// Listener is applied in the reconcile step): Broker and Coordinator.
// TODO: add router listener here once properly build in the built step.
assert_eq!(
sorted_names(&resources.listeners),
["simple-druid-broker", "simple-druid-coordinator"]
);
}
/// 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 druid = druid_from_yaml(MINIMAL_DRUID_YAML);
let cluster = validated_cluster(&druid);
let resources = build(&cluster).expect("build succeeds");
assert_eq!(
sorted_names(&resources.service_accounts),
["simple-druid-serviceaccount"]
);
assert_eq!(
sorted_names(&resources.role_bindings),
["simple-druid-rolebinding"]
);
let expected_labels = BTreeMap::from(
[
("app.kubernetes.io/component", "none"),
("app.kubernetes.io/instance", "simple-druid"),
(
"app.kubernetes.io/managed-by",
"druid.stackable.tech_druidcluster",
),
("app.kubernetes.io/name", "druid"),
("app.kubernetes.io/role-group", "none"),
("app.kubernetes.io/version", "30.0.0-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, "druid-clusterrole");
}
}