Skip to content

Commit 007c2be

Browse files
committed
add impls for role/from and parsing test. Fixed comment
1 parent fcf1fc7 commit 007c2be

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ pub enum Error {
5151
///
5252
/// Does not need a Kubernetes client: every reference to another Kubernetes resource is already
5353
/// dereferenced and validated by this point, so the errors returned here are resource-assembly
54-
/// failures only. `cluster_info` is static cluster metadata (not a client call), and
55-
/// `service_account_name` is the name of the RBAC `ServiceAccount` the role-group Pods run under
56-
/// (RBAC resources are built and applied separately, in the reconcile step).
54+
/// failures only. `cluster_info` is static cluster metadata (not a client call).
5755
pub fn build(
5856
cluster: &ValidatedCluster,
5957
cluster_info: &KubernetesClusterInfo,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn build_pdb(
2727
let pdb = pod_disruption_budget_builder_with_role(
2828
cluster,
2929
&product_name(),
30-
&ValidatedCluster::role_name(role),
30+
&role.into(),
3131
&operator_name(),
3232
&controller_name(),
3333
)

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ impl ValidatedCluster {
130130
}
131131
}
132132

133-
/// The Kubernetes role name for an [`HbaseRole`] (e.g. `master`, `regionserver`,
134-
/// `restserver`).
135-
pub fn role_name(hbase_role: &HbaseRole) -> RoleName {
136-
RoleName::from_str(&hbase_role.to_string()).expect("an HbaseRole name is a valid role name")
137-
}
138-
139133
/// Type-safe names for the per-cluster RBAC resources: the ServiceAccount shared by all
140134
/// Pods, its (namespaced) RoleBinding, and the operator-deployed ClusterRole it binds.
141135
pub fn cluster_resource_names(&self) -> role_utils::ResourceNames {
@@ -153,14 +147,14 @@ impl ValidatedCluster {
153147
) -> ResourceNames {
154148
ResourceNames {
155149
cluster_name: self.name.clone(),
156-
role_name: Self::role_name(hbase_role),
150+
role_name: hbase_role.into(),
157151
role_group_name: role_group_name.clone(),
158152
}
159153
}
160154

161155
/// Recommended labels for a role-group resource.
162156
pub fn recommended_labels(&self, role: &HbaseRole, role_group_name: &RoleGroupName) -> Labels {
163-
self.recommended_labels_for(&Self::role_name(role), role_group_name)
157+
self.recommended_labels_for(&role.into(), role_group_name)
164158
}
165159

166160
/// Recommended labels for a resource that is not tied to a concrete [`HbaseRole`] (e.g. the
@@ -196,12 +190,7 @@ impl ValidatedCluster {
196190
hbase_role: &HbaseRole,
197191
role_group_name: &RoleGroupName,
198192
) -> Labels {
199-
role_group_selector(
200-
self,
201-
&product_name(),
202-
&Self::role_name(hbase_role),
203-
role_group_name,
204-
)
193+
role_group_selector(self, &product_name(), &hbase_role.into(), role_group_name)
205194
}
206195

207196
/// Returns an [`ObjectMetaBuilder`] pre-filled with the namespace, an owner reference back to
@@ -326,17 +315,18 @@ pub type HbaseRoleGroupConfig = stackable_operator::v2::role_utils::RoleGroupCon
326315

327316
#[cfg(test)]
328317
mod tests {
318+
use stackable_operator::v2::types::operator::RoleName;
329319
use strum::IntoEnumIterator;
330320

331-
use super::ValidatedCluster;
332321
use crate::crd::HbaseRole;
333322

334-
/// Locks the invariant behind the `expect` in [`ValidatedCluster::role_name`]: every
335-
/// `HbaseRole` variant (present and future) must serialise to a valid `RoleName`.
323+
/// Locks the invariant behind the `expect` in the `From<HbaseRole> for RoleName` impls:
324+
/// every `HbaseRole` variant (present and future) must serialise to a valid `RoleName`.
336325
#[test]
337326
fn every_hbase_role_serialises_to_a_valid_role_name() {
338327
for role in HbaseRole::iter() {
339-
ValidatedCluster::role_name(&role);
328+
let _: RoleName = (&role).into();
329+
let _: RoleName = role.into();
340330
}
341331
}
342332
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use stackable_operator::{
3030
types::{
3131
common::Port,
3232
kubernetes::{ConfigMapName, ListenerClassName, SecretClassName, VolumeName},
33+
operator::RoleName,
3334
},
3435
},
3536
versioned::versioned,
@@ -262,6 +263,18 @@ pub enum HbaseRole {
262263
RestServer,
263264
}
264265

266+
impl From<HbaseRole> for RoleName {
267+
fn from(value: HbaseRole) -> Self {
268+
RoleName::from_str(&value.to_string()).expect("an HbaseRole name is a valid role name")
269+
}
270+
}
271+
272+
impl From<&HbaseRole> for RoleName {
273+
fn from(value: &HbaseRole) -> Self {
274+
RoleName::from_str(&value.to_string()).expect("an HbaseRole name is a valid role name")
275+
}
276+
}
277+
265278
impl HbaseRole {
266279
const DEFAULT_MASTER_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(20);
267280
// Auto TLS certificate lifetime

0 commit comments

Comments
 (0)