11use crate :: util:: { get_job_state, JobState } ;
22
3- use crate :: superset_controller:: DOCKER_IMAGE_BASE_NAME ;
3+ use crate :: { rbac , superset_controller:: DOCKER_IMAGE_BASE_NAME , APP_NAME } ;
44use snafu:: { OptionExt , ResultExt , Snafu } ;
55use stackable_operator:: commons:: product_image_selection:: ResolvedProductImage ;
66use stackable_operator:: {
7- builder:: { ContainerBuilder , ObjectMetaBuilder } ,
7+ builder:: { ContainerBuilder , ObjectMetaBuilder , PodSecurityContextBuilder } ,
88 client:: Client ,
99 k8s_openapi:: api:: {
1010 batch:: v1:: { Job , JobSpec } ,
@@ -72,6 +72,14 @@ pub enum Error {
7272 source : stackable_superset_crd:: druidconnection:: Error ,
7373 druid_connection : ObjectRef < DruidConnection > ,
7474 } ,
75+ #[ snafu( display( "failed to patch service account" ) ) ]
76+ ApplyServiceAccount {
77+ source : stackable_operator:: error:: Error ,
78+ } ,
79+ #[ snafu( display( "failed to patch role binding" ) ) ]
80+ ApplyRoleBinding {
81+ source : stackable_operator:: error:: Error ,
82+ } ,
7583}
7684
7785type Result < T , E = Error > = std:: result:: Result < T , E > ;
@@ -96,6 +104,8 @@ impl ReconcilerError for Error {
96104 Error :: DruidConnectionNoNamespace {
97105 druid_connection, ..
98106 } => Some ( druid_connection. clone ( ) . erase ( ) ) ,
107+ Error :: ApplyServiceAccount { .. } => None ,
108+ Error :: ApplyRoleBinding { .. } => None ,
99109 }
100110 }
101111}
@@ -108,6 +118,21 @@ pub async fn reconcile_druid_connection(
108118
109119 let client = & ctx. client ;
110120
121+ let ( rbac_sa, rbac_rolebinding) =
122+ rbac:: build_rbac_resources ( druid_connection. as_ref ( ) , APP_NAME ) ;
123+ client
124+ . apply_patch ( DRUID_CONNECTION_CONTROLLER_NAME , & rbac_sa, & rbac_sa)
125+ . await
126+ . context ( ApplyServiceAccountSnafu ) ?;
127+ client
128+ . apply_patch (
129+ DRUID_CONNECTION_CONTROLLER_NAME ,
130+ & rbac_rolebinding,
131+ & rbac_rolebinding,
132+ )
133+ . await
134+ . context ( ApplyRoleBindingSnafu ) ?;
135+
111136 if let Some ( ref s) = druid_connection. status {
112137 match s. condition {
113138 DruidConnectionStatusCondition :: Pending => {
@@ -172,6 +197,7 @@ pub async fn reconcile_druid_connection(
172197 & druid_connection,
173198 & resolved_product_image,
174199 & sqlalchemy_str,
200+ & rbac_sa. name_any ( ) ,
175201 )
176202 . await ?;
177203 client
@@ -266,6 +292,7 @@ async fn build_import_job(
266292 druid_connection : & DruidConnection ,
267293 resolved_product_image : & ResolvedProductImage ,
268294 sqlalchemy_str : & str ,
295+ sa_name : & str ,
269296) -> Result < Job > {
270297 let mut commands = vec ! [ ] ;
271298
@@ -301,6 +328,13 @@ async fn build_import_job(
301328 containers : vec ! [ container] ,
302329 image_pull_secrets : resolved_product_image. pull_secrets . clone ( ) ,
303330 restart_policy : Some ( "Never" . to_string ( ) ) ,
331+ service_account : Some ( sa_name. to_string ( ) ) ,
332+ security_context : Some (
333+ PodSecurityContextBuilder :: new ( )
334+ . run_as_user ( 1000 )
335+ . run_as_group ( 0 )
336+ . build ( ) ,
337+ ) ,
304338 ..Default :: default ( )
305339 } ) ,
306340 } ;
0 commit comments