Skip to content

Commit d9d8c6e

Browse files
committed
history: refactor rbac as per todo
1 parent 6a2f32a commit d9d8c6e

File tree

1 file changed

+23
-65
lines changed

1 file changed

+23
-65
lines changed

rust/operator-binary/src/history/history_controller.rs

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ use stackable_operator::{
2424
commons::{
2525
listener::{Listener, ListenerPort},
2626
product_image_selection::ResolvedProductImage,
27+
rbac::build_rbac_resources,
2728
},
2829
k8s_openapi::{
2930
DeepMerge,
3031
api::{
3132
apps::v1::{StatefulSet, StatefulSetSpec},
3233
core::v1::{ConfigMap, PodSecurityContext, ServiceAccount},
33-
rbac::v1::{ClusterRole, RoleBinding, RoleRef, Subject},
3434
},
3535
apimachinery::pkg::apis::meta::v1::LabelSelector,
3636
},
@@ -60,10 +60,10 @@ use crate::{
6060
ACCESS_KEY_ID, APP_NAME, HISTORY_CONTROLLER_NAME, HISTORY_ROLE_NAME, HISTORY_UI_PORT,
6161
JVM_SECURITY_PROPERTIES_FILE, LISTENER_VOLUME_DIR, LISTENER_VOLUME_NAME,
6262
MAX_SPARK_LOG_FILES_SIZE, METRICS_PORT, OPERATOR_NAME, SECRET_ACCESS_KEY,
63-
SPARK_CLUSTER_ROLE, SPARK_DEFAULTS_FILE_NAME, SPARK_ENV_SH_FILE_NAME,
64-
SPARK_IMAGE_BASE_NAME, SPARK_UID, STACKABLE_TRUST_STORE, VOLUME_MOUNT_NAME_CONFIG,
65-
VOLUME_MOUNT_NAME_LOG, VOLUME_MOUNT_NAME_LOG_CONFIG, VOLUME_MOUNT_PATH_CONFIG,
66-
VOLUME_MOUNT_PATH_LOG, VOLUME_MOUNT_PATH_LOG_CONFIG,
63+
SPARK_DEFAULTS_FILE_NAME, SPARK_ENV_SH_FILE_NAME, SPARK_IMAGE_BASE_NAME, SPARK_UID,
64+
STACKABLE_TRUST_STORE, VOLUME_MOUNT_NAME_CONFIG, VOLUME_MOUNT_NAME_LOG,
65+
VOLUME_MOUNT_NAME_LOG_CONFIG, VOLUME_MOUNT_PATH_CONFIG, VOLUME_MOUNT_PATH_LOG,
66+
VOLUME_MOUNT_PATH_LOG_CONFIG,
6767
},
6868
history::{self, HistoryConfig, SparkHistoryServerContainer, v1alpha1},
6969
listener,
@@ -78,9 +78,9 @@ use crate::{
7878
#[strum_discriminants(derive(IntoStaticStr))]
7979
#[allow(clippy::enum_variant_names)]
8080
pub enum Error {
81-
#[snafu(display("failed to build object meta data"))]
82-
ObjectMeta {
83-
source: stackable_operator::builder::meta::Error,
81+
#[snafu(display("failed to build RBAC resources"))]
82+
BuildRbacResources {
83+
source: stackable_operator::commons::rbac::Error,
8484
},
8585

8686
#[snafu(display("failed to build spark history group listener"))]
@@ -113,8 +113,8 @@ pub enum Error {
113113
source: stackable_operator::builder::meta::Error,
114114
},
115115

116-
#[snafu(display("failed to update the history server deployment"))]
117-
ApplyDeployment {
116+
#[snafu(display("failed to update the history server stateful set"))]
117+
ApplyStatefulSet {
118118
source: stackable_operator::cluster_resources::Error,
119119
},
120120

@@ -123,11 +123,6 @@ pub enum Error {
123123
source: stackable_operator::cluster_resources::Error,
124124
},
125125

126-
#[snafu(display("failed to update history server service"))]
127-
ApplyService {
128-
source: stackable_operator::cluster_resources::Error,
129-
},
130-
131126
#[snafu(display("failed to apply role ServiceAccount"))]
132127
ApplyServiceAccount {
133128
source: stackable_operator::cluster_resources::Error,
@@ -275,14 +270,20 @@ pub async fn reconcile(
275270
.context(LogDirSnafu)?;
276271

277272
// Use a dedicated service account for history server pods.
278-
let (serviceaccount, rolebinding) =
279-
build_history_role_serviceaccount(shs, &resolved_product_image.app_version_label)?;
280-
let serviceaccount = cluster_resources
281-
.add(client, serviceaccount)
273+
let (service_account, role_binding) = build_rbac_resources(
274+
shs,
275+
APP_NAME,
276+
cluster_resources
277+
.get_required_labels()
278+
.context(GetRequiredLabelsSnafu)?,
279+
)
280+
.context(BuildRbacResourcesSnafu)?;
281+
let service_account = cluster_resources
282+
.add(client, service_account)
282283
.await
283284
.context(ApplyServiceAccountSnafu)?;
284285
cluster_resources
285-
.add(client, rolebinding)
286+
.add(client, role_binding)
286287
.await
287288
.context(ApplyRoleBindingSnafu)?;
288289

@@ -322,12 +323,12 @@ pub async fn reconcile(
322323
&rgr,
323324
&log_dir,
324325
&merged_config,
325-
&serviceaccount,
326+
&service_account,
326327
)?;
327328
cluster_resources
328329
.add(client, sts)
329330
.await
330-
.context(ApplyDeploymentSnafu)?;
331+
.context(ApplyStatefulSetSnafu)?;
331332

332333
let rg_group_listener = build_group_listener(
333334
shs,
@@ -674,49 +675,6 @@ fn build_stateful_set(
674675
})
675676
}
676677

677-
// TODO: This function should be replaced with operator-rs build_rbac_resources.
678-
// See: https://github.com/stackabletech/spark-k8s-operator/issues/499
679-
#[allow(clippy::result_large_err)]
680-
fn build_history_role_serviceaccount(
681-
shs: &v1alpha1::SparkHistoryServer,
682-
app_version_label: &str,
683-
) -> Result<(ServiceAccount, RoleBinding), Error> {
684-
let sa = ServiceAccount {
685-
metadata: ObjectMetaBuilder::new()
686-
.name_and_namespace(shs)
687-
.ownerreference_from_resource(shs, None, Some(true))
688-
.context(ObjectMissingMetadataForOwnerRefSnafu)?
689-
.with_recommended_labels(labels(shs, app_version_label, HISTORY_CONTROLLER_NAME))
690-
.context(MetadataBuildSnafu)?
691-
.build(),
692-
..ServiceAccount::default()
693-
};
694-
let binding = RoleBinding {
695-
metadata: ObjectMetaBuilder::new()
696-
.name_and_namespace(shs)
697-
.ownerreference_from_resource(shs, None, Some(true))
698-
.context(ObjectMissingMetadataForOwnerRefSnafu)?
699-
.with_recommended_labels(labels(shs, app_version_label, HISTORY_CONTROLLER_NAME))
700-
.context(MetadataBuildSnafu)?
701-
.build(),
702-
role_ref: RoleRef {
703-
api_group: <ClusterRole as stackable_operator::k8s_openapi::Resource>::GROUP // need to fully qualify because of "Resource" name clash
704-
.to_string(),
705-
kind: <ClusterRole as stackable_operator::k8s_openapi::Resource>::KIND.to_string(),
706-
name: SPARK_CLUSTER_ROLE.to_string(),
707-
},
708-
subjects: Some(vec![Subject {
709-
api_group: Some(
710-
<ServiceAccount as stackable_operator::k8s_openapi::Resource>::GROUP.to_string(),
711-
),
712-
kind: <ServiceAccount as stackable_operator::k8s_openapi::Resource>::KIND.to_string(),
713-
name: sa.name_any(),
714-
namespace: sa.namespace(),
715-
}]),
716-
};
717-
Ok((sa, binding))
718-
}
719-
720678
#[allow(clippy::result_large_err)]
721679
fn spark_defaults(
722680
shs: &v1alpha1::SparkHistoryServer,

0 commit comments

Comments
 (0)