-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmod.rs
More file actions
297 lines (266 loc) · 10.3 KB
/
Copy pathmod.rs
File metadata and controls
297 lines (266 loc) · 10.3 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
pub mod build;
pub mod dereference;
pub mod validate;
pub mod zookeeper;
use std::{collections::BTreeMap, str::FromStr};
use const_format::concatcp;
pub use stackable_operator::v2::types::operator::RoleGroupName;
use stackable_operator::{
builder::meta::ObjectMetaBuilder,
commons::product_image_selection::ResolvedProductImage,
k8s_openapi::{
api::{
apps::v1::StatefulSet,
core::v1::{ConfigMap, Service},
policy::v1::PodDisruptionBudget,
},
apimachinery::pkg::apis::meta::v1::ObjectMeta,
},
kube::Resource,
kvp::Labels,
v2::{
HasName, HasUid, NameIsValidLabelValue,
builder::meta::ownerreference_from_resource,
kvp::label::{recommended_labels, role_group_selector},
role_group_utils::ResourceNames,
types::{
kubernetes::{ConfigMapName, NamespaceName, SecretClassName, Uid},
operator::{
ClusterName, ControllerName, OperatorName, ProductName, ProductVersion, RoleName,
},
},
},
};
use crate::{
controller::{build::opa::HbaseOpaConfig, zookeeper::ZookeeperConnectionInformation},
crd::{APP_NAME, AnyServiceConfig, HbaseRole, OPERATOR_NAME, v1alpha1},
};
pub const HBASE_CONTROLLER_NAME: &str = "hbasecluster";
pub const FULL_HBASE_CONTROLLER_NAME: &str = concatcp!(HBASE_CONTROLLER_NAME, '.', OPERATOR_NAME);
/// The product name (`hbase`) as a type-safe label value.
pub(crate) fn product_name() -> ProductName {
ProductName::from_str(APP_NAME).expect("'hbase' is a valid product name")
}
/// The operator name as a type-safe label value.
pub(crate) fn operator_name() -> OperatorName {
OperatorName::from_str(OPERATOR_NAME).expect("the operator name is a valid label value")
}
/// The controller name as a type-safe label value.
pub(crate) fn controller_name() -> ControllerName {
ControllerName::from_str(HBASE_CONTROLLER_NAME)
.expect("the controller name is a valid label value")
}
/// The complete set of Kubernetes resources built for a [`ValidatedCluster`], ready to be applied.
///
/// hbase exposes its listeners as volume/PVC sources inside the `StatefulSet` rather than as
/// top-level `Listener` objects, so (unlike some sibling operators) there is no `listeners` field.
pub struct KubernetesResources {
pub stateful_sets: Vec<StatefulSet>,
pub services: Vec<Service>,
pub config_maps: Vec<ConfigMap>,
pub pod_disruption_budgets: Vec<PodDisruptionBudget>,
}
/// The validated cluster: proves that config merging and validation succeeded for
/// every role and role group before any resources are created.
#[derive(Clone, Debug)]
pub struct ValidatedCluster {
/// Backs the [`Resource`] implementation (provides `meta()`/`name_any()`) so the build
/// functions can derive `ObjectMeta`, owner references and labels without the full
/// `HbaseCluster` object. Holds only name, namespace and uid.
metadata: ObjectMeta,
/// The logical (and Kubernetes object) name of the cluster.
pub name: ClusterName,
/// The namespace the cluster lives in.
pub namespace: NamespaceName,
/// The UID of the `HbaseCluster` object, used to build owner references.
pub uid: Uid,
pub image: ResolvedProductImage,
/// The product version as a valid label value, used for the recommended
/// `app.kubernetes.io/version` label. Derived from the resolved image's app version label
/// value.
pub product_version: ProductVersion,
pub cluster_config: ValidatedClusterConfig,
pub role_group_configs: BTreeMap<HbaseRole, BTreeMap<RoleGroupName, HbaseRoleGroupConfig>>,
pub role_configs: BTreeMap<HbaseRole, ValidatedRoleConfig>,
}
impl ValidatedCluster {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: ClusterName,
namespace: NamespaceName,
uid: Uid,
image: ResolvedProductImage,
cluster_config: ValidatedClusterConfig,
role_group_configs: BTreeMap<HbaseRole, BTreeMap<RoleGroupName, HbaseRoleGroupConfig>>,
role_configs: BTreeMap<HbaseRole, ValidatedRoleConfig>,
) -> Self {
// `app_version_label_value` is constructed to be a valid label value, so it is also a
// valid `ProductVersion`.
let product_version = ProductVersion::from_str(&image.app_version_label_value)
.expect("the app version label value is a valid product version");
Self {
metadata: ObjectMeta {
name: Some(name.to_string()),
namespace: Some(namespace.to_string()),
uid: Some(uid.to_string()),
..ObjectMeta::default()
},
name,
namespace,
uid,
image,
product_version,
cluster_config,
role_group_configs,
role_configs,
}
}
/// The Kubernetes role name for an [`HbaseRole`] (e.g. `master`, `regionserver`,
/// `restserver`).
pub fn role_name(hbase_role: &HbaseRole) -> RoleName {
RoleName::from_str(&hbase_role.to_string()).expect("an HbaseRole name is a valid role name")
}
/// Type-safe names for the resources of a given role group.
pub(crate) fn resource_names(
&self,
hbase_role: &HbaseRole,
role_group_name: &RoleGroupName,
) -> ResourceNames {
ResourceNames {
cluster_name: self.name.clone(),
role_name: Self::role_name(hbase_role),
role_group_name: role_group_name.clone(),
}
}
/// Recommended labels for a role-group resource.
pub fn recommended_labels(
&self,
hbase_role: &HbaseRole,
role_group_name: &RoleGroupName,
) -> Labels {
recommended_labels(
self,
&product_name(),
&self.product_version,
&operator_name(),
&controller_name(),
&Self::role_name(hbase_role),
role_group_name,
)
}
/// Selector labels matching the pods of a role group.
pub fn role_group_selector(
&self,
hbase_role: &HbaseRole,
role_group_name: &RoleGroupName,
) -> Labels {
role_group_selector(
self,
&product_name(),
&Self::role_name(hbase_role),
role_group_name,
)
}
/// Returns an [`ObjectMetaBuilder`] pre-filled with the namespace, an owner reference back to
/// this cluster, and the recommended labels for a resource named `name` in `role_group_name`.
///
/// Consolidates the metadata chain repeated by the child-resource builders. Call sites that
/// need extra labels/annotations chain them onto the returned builder.
pub(crate) fn object_meta(
&self,
name: impl Into<String>,
hbase_role: &HbaseRole,
role_group_name: &RoleGroupName,
) -> ObjectMetaBuilder {
let mut builder = ObjectMetaBuilder::new();
builder
.name_and_namespace(self)
.name(name)
.ownerreference(ownerreference_from_resource(self, None, Some(true)))
.with_labels(self.recommended_labels(hbase_role, role_group_name));
builder
}
/// Whether Kerberos is enabled for this cluster.
pub fn has_kerberos_enabled(&self) -> bool {
self.cluster_config.kerberos_secret_class.is_some()
}
/// Whether HTTPS is enabled for this cluster.
///
/// Derived from the validated config (a TLS `SecretClass` was configured).
pub fn has_https_enabled(&self) -> bool {
self.cluster_config.https_secret_class.is_some()
}
}
impl Resource for ValidatedCluster {
type DynamicType = <v1alpha1::HbaseCluster as Resource>::DynamicType;
type Scope = <v1alpha1::HbaseCluster as Resource>::Scope;
fn group(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
v1alpha1::HbaseCluster::group(dt)
}
fn version(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
v1alpha1::HbaseCluster::version(dt)
}
fn kind(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
v1alpha1::HbaseCluster::kind(dt)
}
fn plural(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
v1alpha1::HbaseCluster::plural(dt)
}
fn meta(&self) -> &ObjectMeta {
&self.metadata
}
fn meta_mut(&mut self) -> &mut ObjectMeta {
&mut self.metadata
}
}
impl HasName for ValidatedCluster {
fn to_name(&self) -> String {
self.name.to_string()
}
}
impl HasUid for ValidatedCluster {
fn to_uid(&self) -> Uid {
self.uid.clone()
}
}
impl NameIsValidLabelValue for ValidatedCluster {
fn to_label_value(&self) -> String {
self.name.to_label_value()
}
}
/// Cluster-wide settings resolved once during validation.
#[derive(Clone, Debug)]
pub struct ValidatedClusterConfig {
// Pre-resolved OPA connection configuration.
pub hbase_opa_config: Option<HbaseOpaConfig>,
/// The Kerberos `SecretClass` name, if Kerberos is enabled.
pub kerberos_secret_class: Option<SecretClassName>,
/// The HTTPS/TLS `SecretClass` name, if HTTPS is enabled.
pub https_secret_class: Option<SecretClassName>,
/// The HDFS discovery ConfigMap name the cluster connects to.
pub hdfs_config_map_name: ConfigMapName,
// Pre-resolved zookeeper connection settings.
pub zookeeper_connection_information: ZookeeperConnectionInformation,
}
/// Per-role configuration extracted during validation.
#[derive(Clone, Debug)]
pub struct ValidatedRoleConfig {
pub pdb: stackable_operator::commons::pdb::PdbConfig,
}
/// The validated per-rolegroup product configuration: the merged CRD config and the resolved
/// logging settings. The merged (role <- role group) `configOverrides`, `envOverrides` and
/// `podOverrides` live on the enclosing [`HbaseRoleGroupConfig`] (the `RoleGroupConfig` wrapper),
/// not here.
#[derive(Clone, Debug)]
pub struct ValidatedHbaseConfig {
/// The merged, role-specific product config.
pub config: AnyServiceConfig,
/// The validated logging configuration (HBase + optional Vector container), resolved up-front
/// during validation.
pub logging: validate::ValidatedLogging,
}
pub type HbaseRoleGroupConfig = stackable_operator::v2::role_utils::RoleGroupConfig<
ValidatedHbaseConfig,
stackable_operator::v2::role_utils::JavaCommonConfig,
v1alpha1::HbaseConfigOverrides,
>;