-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmod.rs
More file actions
189 lines (171 loc) · 6.47 KB
/
Copy pathmod.rs
File metadata and controls
189 lines (171 loc) · 6.47 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
//! Builders that turn a [`ValidatedCluster`](crate::controller::ValidatedCluster) into
//! Kubernetes resources.
use std::str::FromStr;
use snafu::{ResultExt, Snafu};
use stackable_operator::{
utils::cluster_info::KubernetesClusterInfo, v2::types::operator::RoleGroupName,
};
use crate::{
controller::{
KubernetesResources, ValidatedCluster,
build::resource::{
config_map::{self, build_rolegroup_config_map},
discovery::{self, build_discovery_config_map},
pdb::build_pdb,
service::{build_rolegroup_metrics_service, build_rolegroup_service},
statefulset::{self, build_rolegroup_statefulset},
},
},
crd::HbaseRole,
};
// 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");
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to build ConfigMap for role {hbase_role} role group {role_group}"))]
ConfigMap {
source: config_map::Error,
hbase_role: HbaseRole,
role_group: RoleGroupName,
},
#[snafu(display("failed to build StatefulSet for role {hbase_role} role group {role_group}"))]
StatefulSet {
source: statefulset::Error,
hbase_role: HbaseRole,
role_group: RoleGroupName,
},
#[snafu(display("failed to build discovery ConfigMap"))]
Discovery { source: discovery::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, so the errors returned here are resource-assembly
/// failures only. `cluster_info` is static cluster metadata (not a client call), and
/// `service_account_name` is the name of the RBAC `ServiceAccount` the role-group Pods run under
/// (RBAC resources are built and applied separately, in the reconcile step).
pub fn build(
cluster: &ValidatedCluster,
cluster_info: &KubernetesClusterInfo,
service_account_name: &str,
) -> Result<KubernetesResources, Error> {
let mut stateful_sets = vec![];
let mut services = vec![];
let mut config_maps = vec![];
let mut pod_disruption_budgets = vec![];
for (hbase_role, role_group_configs) in &cluster.role_group_configs {
for (role_group_name, rg_config) in role_group_configs {
services.push(build_rolegroup_service(
cluster,
hbase_role,
role_group_name,
));
services.push(build_rolegroup_metrics_service(
cluster,
hbase_role,
role_group_name,
));
config_maps.push(
build_rolegroup_config_map(cluster, cluster_info, hbase_role, role_group_name)
.with_context(|_| ConfigMapSnafu {
hbase_role: hbase_role.clone(),
role_group: role_group_name.clone(),
})?,
);
stateful_sets.push(
build_rolegroup_statefulset(
cluster,
hbase_role,
role_group_name,
rg_config,
service_account_name,
)
.with_context(|_| StatefulSetSnafu {
hbase_role: hbase_role.clone(),
role_group: role_group_name.clone(),
})?,
);
}
if let Some(role_config) = cluster.role_configs.get(hbase_role)
&& let Some(pdb) = build_pdb(&role_config.pdb, cluster, hbase_role)
{
pod_disruption_budgets.push(pdb);
}
}
// The role-level discovery ConfigMap advertises the cluster's connection information; it is
// deterministic (derived only from the validated cluster and static cluster info).
config_maps.push(build_discovery_config_map(cluster, cluster_info).context(DiscoverySnafu)?);
Ok(KubernetesResources {
stateful_sets,
services,
config_maps,
pod_disruption_budgets,
})
}
pub mod graceful_shutdown;
pub mod jvm;
pub mod kerberos;
pub mod opa;
pub mod properties;
pub mod region_mover;
pub mod resource;
pub mod role;
#[cfg(test)]
mod tests {
use stackable_operator::kube::Resource;
use super::build;
use crate::test_utils;
/// Collects the `.metadata.name`s of the given resources, sorted for stable comparison.
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 = test_utils::validated_cluster();
let cluster_info = test_utils::cluster_info();
let resources =
build(&cluster, &cluster_info, "hbase-serviceaccount").expect("build succeeds");
// One StatefulSet per role group (one `default` group for each of the three roles).
assert_eq!(
sorted_names(&resources.stateful_sets),
[
"hbase-master-default",
"hbase-regionserver-default",
"hbase-restserver-default",
]
);
// One headless and one metrics Service per role group.
assert_eq!(
sorted_names(&resources.services),
[
"hbase-master-default-headless",
"hbase-master-default-metrics",
"hbase-regionserver-default-headless",
"hbase-regionserver-default-metrics",
"hbase-restserver-default-headless",
"hbase-restserver-default-metrics",
]
);
// One ConfigMap per role group plus the cluster-wide discovery ConfigMap (`hbase`).
assert_eq!(
sorted_names(&resources.config_maps),
[
"hbase",
"hbase-master-default",
"hbase-regionserver-default",
"hbase-restserver-default",
]
);
// A default PodDisruptionBudget per role.
assert_eq!(
sorted_names(&resources.pod_disruption_budgets),
["hbase-master", "hbase-regionserver", "hbase-restserver"]
);
}
}