Skip to content

Commit 4790e49

Browse files
committed
added role/From impl for single zookeeper role plus parsing test
1 parent 164fa53 commit 4790e49

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use stackable_operator::{
3131
kubernetes::{
3232
ConfigMapName, ListenerClassName, ListenerName, NamespaceName, ServiceName,
3333
},
34+
operator::RoleName,
3435
},
3536
},
3637
versioned::versioned,
@@ -335,6 +336,18 @@ pub enum ZookeeperRole {
335336
Server,
336337
}
337338

339+
impl From<ZookeeperRole> for RoleName {
340+
fn from(value: ZookeeperRole) -> Self {
341+
RoleName::from_str(&value.to_string()).expect("a ZookeeperRole is a valid role name")
342+
}
343+
}
344+
345+
impl From<&ZookeeperRole> for RoleName {
346+
fn from(value: &ZookeeperRole) -> Self {
347+
RoleName::from_str(&value.to_string()).expect("a ZookeeperRole is a valid role name")
348+
}
349+
}
350+
338351
/// Reference to a single `Pod` that is a component of a [`v1alpha1::ZookeeperCluster`]
339352
///
340353
/// Used for service discovery.

rust/operator-binary/src/zk_controller/build/resource/discovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use stackable_operator::{
1515
};
1616

1717
use crate::{
18-
crd::{ZOOKEEPER_SERVER_PORT_NAME, security::ZookeeperSecurity},
18+
crd::{ZOOKEEPER_SERVER_PORT_NAME, ZookeeperRole, security::ZookeeperSecurity},
1919
zk_controller::{
2020
build::PLACEHOLDER_DISCOVERY_ROLE_GROUP,
2121
validate::{ValidatedCluster, operator_name, product_name},
@@ -150,7 +150,7 @@ fn build_discovery_configmap_for_owner(
150150
product_version,
151151
&operator_name(),
152152
&controller_name,
153-
&ValidatedCluster::role_name(),
153+
&ZookeeperRole::Server.into(),
154154
&role_group_name,
155155
))
156156
.build(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn build_pdb(
2424
let pdb = pod_disruption_budget_builder_with_role(
2525
cluster,
2626
&product_name(),
27-
&ValidatedCluster::role_name(),
27+
&ZookeeperRole::Server.into(),
2828
&operator_name(),
2929
&controller_name(),
3030
)

rust/operator-binary/src/zk_controller/validate.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,14 @@ impl ValidatedCluster {
295295
}
296296
}
297297

298-
/// The one ZooKeeper role name (`server`).
299-
pub fn role_name() -> RoleName {
300-
RoleName::from_str(&ZookeeperRole::Server.to_string())
301-
.expect("the server role name is a valid role name")
302-
}
303-
304298
/// Type-safe names for the resources of a given role group.
305299
pub(crate) fn role_group_resource_names(
306300
&self,
307301
role_group_name: &RoleGroupName,
308302
) -> ResourceNames {
309303
ResourceNames {
310304
cluster_name: self.name.clone(),
311-
role_name: Self::role_name(),
305+
role_name: ZookeeperRole::Server.into(),
312306
role_group_name: role_group_name.clone(),
313307
}
314308
}
@@ -323,7 +317,7 @@ impl ValidatedCluster {
323317
}
324318

325319
pub fn recommended_labels(&self, role_group_name: &RoleGroupName) -> Labels {
326-
self.recommended_labels_for(&Self::role_name(), role_group_name)
320+
self.recommended_labels_for(&ZookeeperRole::Server.into(), role_group_name)
327321
}
328322

329323
pub fn recommended_labels_for(
@@ -337,7 +331,7 @@ impl ValidatedCluster {
337331
pub fn unversioned_recommended_labels(&self, role_group_name: &RoleGroupName) -> Labels {
338332
self.recommended_labels_with(
339333
&UNVERSIONED_PRODUCT_VERSION,
340-
&Self::role_name(),
334+
&ZookeeperRole::Server.into(),
341335
role_group_name,
342336
)
343337
}
@@ -361,7 +355,12 @@ impl ValidatedCluster {
361355

362356
/// Selector labels matching the pods of a role group.
363357
pub fn role_group_selector(&self, role_group_name: &RoleGroupName) -> Labels {
364-
role_group_selector(self, &product_name(), &Self::role_name(), role_group_name)
358+
role_group_selector(
359+
self,
360+
&product_name(),
361+
&ZookeeperRole::Server.into(),
362+
role_group_name,
363+
)
365364
}
366365

367366
/// Returns an [`ObjectMetaBuilder`] pre-filled with the namespace, an owner reference back to
@@ -749,4 +748,14 @@ mod tests {
749748
Some(Quantity("3Gi".to_owned()))
750749
);
751750
}
751+
752+
/// Locks the invariant behind the `expect` in the `From<ZookeeperRole> for RoleName` impls:
753+
/// every `ZookeeperRole` variant (present and future) must serialise to a valid `RoleName`.
754+
#[test]
755+
fn every_zookeeper_role_serialises_to_a_valid_role_name() {
756+
for role in ZookeeperRole::iter() {
757+
let _: RoleName = (&role).into();
758+
let _: RoleName = role.into();
759+
}
760+
}
752761
}

0 commit comments

Comments
 (0)