Skip to content

Commit e7e39b0

Browse files
committed
added role/From impl for single nifi role plus parsing test
1 parent a9d21db commit e7e39b0

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

rust/operator-binary/src/controller/build.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ pub enum Error {
6767
/// Does not need a Kubernetes client: every reference to another Kubernetes resource is already
6868
/// dereferenced and validated by this point, so the errors returned here are resource-assembly
6969
/// failures only.
70-
///
71-
/// `service_account_name` is the name of the RBAC `ServiceAccount` the role-group Pods run under
72-
/// (RBAC resources are built and applied separately, in the reconcile step).
7370
pub fn build(cluster: &ValidatedCluster) -> Result<KubernetesResources, Error> {
7471
let mut stateful_sets = vec![];
7572
let mut services = vec![];

rust/operator-binary/src/controller/build/resource/pdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn build_pdb(
2323
let pdb = pod_disruption_budget_builder_with_role(
2424
cluster,
2525
&product_name(),
26-
&ValidatedCluster::role_name(),
26+
&NifiRole::Node.into(),
2727
&operator_name(),
2828
&controller_name(),
2929
)

rust/operator-binary/src/controller/mod.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,6 @@ impl ValidatedCluster {
253253
}
254254
}
255255

256-
/// The single NiFi role name (`node`).
257-
pub fn role_name() -> RoleName {
258-
RoleName::from_str(&NifiRole::Node.to_string())
259-
.expect("the node role name is a valid role name")
260-
}
261-
262256
/// Type-safe names for the per-cluster RBAC resources: the ServiceAccount shared by all
263257
/// Pods, its (namespaced) RoleBinding, and the operator-deployed ClusterRole it binds.
264258
pub fn cluster_resource_names(&self) -> role_utils::ResourceNames {
@@ -275,14 +269,14 @@ impl ValidatedCluster {
275269
) -> ResourceNames {
276270
ResourceNames {
277271
cluster_name: self.name.clone(),
278-
role_name: Self::role_name(),
272+
role_name: NifiRole::Node.into(),
279273
role_group_name: role_group_name.clone(),
280274
}
281275
}
282276

283277
/// Recommended labels for a role-group resource.
284278
pub fn recommended_labels(&self, role_group_name: &RoleGroupName) -> Labels {
285-
self.recommended_labels_for(&Self::role_name(), role_group_name)
279+
self.recommended_labels_for(&NifiRole::Node.into(), role_group_name)
286280
}
287281

288282
/// Recommended labels for a resource that is not tied to a concrete role, using a free-form role/role-group label value.
@@ -299,7 +293,7 @@ impl ValidatedCluster {
299293
pub fn unversioned_recommended_labels(&self, role_group_name: &RoleGroupName) -> Labels {
300294
self.recommended_labels_with(
301295
&UNVERSIONED_PRODUCT_VERSION,
302-
&Self::role_name(),
296+
&NifiRole::Node.into(),
303297
role_group_name,
304298
)
305299
}
@@ -323,7 +317,12 @@ impl ValidatedCluster {
323317

324318
/// Selector labels matching the pods of a role group.
325319
pub fn role_group_selector(&self, role_group_name: &RoleGroupName) -> Labels {
326-
role_group_selector(self, &product_name(), &Self::role_name(), role_group_name)
320+
role_group_selector(
321+
self,
322+
&product_name(),
323+
&NifiRole::Node.into(),
324+
role_group_name,
325+
)
327326
}
328327

329328
/// Returns an [`ObjectMetaBuilder`](stackable_operator::builder::meta::ObjectMetaBuilder)
@@ -418,3 +417,21 @@ impl Resource for ValidatedCluster {
418417
&mut self.metadata
419418
}
420419
}
420+
421+
#[cfg(test)]
422+
mod tests {
423+
use stackable_operator::v2::types::operator::RoleName;
424+
use strum::IntoEnumIterator;
425+
426+
use crate::crd::NifiRole;
427+
428+
/// Locks the invariant behind the `expect` in the `From<NifiRole> for RoleName` impls:
429+
/// every `NifiRole` variant (present and future) must serialise to a valid `RoleName`.
430+
#[test]
431+
fn every_nifi_role_serialises_to_a_valid_role_name() {
432+
for role in NifiRole::iter() {
433+
let _: RoleName = (&role).into();
434+
let _: RoleName = role.into();
435+
}
436+
}
437+
}

rust/operator-binary/src/crd/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use stackable_operator::{
3636
role_utils::JavaCommonConfig,
3737
types::{
3838
kubernetes::{ConfigMapName, ListenerClassName, SecretClassName},
39-
operator::ProductVersion,
39+
operator::{ProductVersion, RoleName},
4040
},
4141
},
4242
versioned::versioned,
@@ -224,7 +224,17 @@ pub fn default_allow_all() -> bool {
224224
}
225225

226226
#[derive(
227-
Clone, Debug, Deserialize, Eq, JsonSchema, Ord, PartialEq, PartialOrd, Serialize, strum::Display,
227+
Clone,
228+
Debug,
229+
Deserialize,
230+
Eq,
231+
JsonSchema,
232+
Ord,
233+
PartialEq,
234+
PartialOrd,
235+
Serialize,
236+
strum::Display,
237+
strum::EnumIter,
228238
)]
229239
#[serde(rename_all = "camelCase")]
230240
#[strum(serialize_all = "camelCase")]
@@ -233,6 +243,18 @@ pub enum NifiRole {
233243
Node,
234244
}
235245

246+
impl From<NifiRole> for RoleName {
247+
fn from(value: NifiRole) -> Self {
248+
RoleName::from_str(&value.to_string()).expect("a NifiRole is a valid role name")
249+
}
250+
}
251+
252+
impl From<&NifiRole> for RoleName {
253+
fn from(value: &NifiRole) -> Self {
254+
RoleName::from_str(&value.to_string()).expect("a NifiRole is a valid role name")
255+
}
256+
}
257+
236258
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, Serialize)]
237259
pub struct NifiStatus {
238260
pub deployed_version: Option<ProductVersion>,

0 commit comments

Comments
 (0)