Skip to content

Commit 5c8847b

Browse files
committed
Added ownerreference to build_rbac_resources (#579)
## Description *Please add a description here. This will become the commit message of the merge request later.* Co-authored-by: Malte Sander <malte.sander.it@gmail.com>
1 parent 26e921c commit 5c8847b

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- [BREAKING]: Added ownerreferences and labels to `build_rbac_resources` ([#579]).
10+
711
## [0.39.1] - 2023-04-07
812

913
### Fixed
@@ -13,6 +17,7 @@ All notable changes to this project will be documented in this file.
1317
log events ([#577]).
1418

1519
[#577]: https://github.com/stackabletech/operator-rs/pull/577
20+
[#579]: https://github.com/stackabletech/operator-rs/pull/579
1621

1722
## [0.39.0] - 2023-03-31
1823

src/cluster_resources.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,21 @@ impl ClusterResources {
367367
})
368368
}
369369

370+
/// Return required labels for cluster resources to be uniquely identified for clean up.
371+
// TODO: This is a (quick-fix) helper method but should be replaced by better label handling
372+
pub fn get_required_labels(&self) -> BTreeMap<String, String> {
373+
vec![
374+
(
375+
APP_INSTANCE_LABEL.to_string(),
376+
self.app_instance.to_string(),
377+
),
378+
(APP_MANAGED_BY_LABEL.to_string(), self.manager.to_string()),
379+
(APP_NAME_LABEL.to_string(), self.app_name.to_string()),
380+
]
381+
.into_iter()
382+
.collect()
383+
}
384+
370385
/// Adds a resource to the cluster resources.
371386
///
372387
/// The resource will be patched and the patched resource will be returned.

src/commons/rbac.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
use crate::builder::ObjectMetaBuilder;
2+
use crate::error::OperatorResult;
23
use crate::k8s_openapi::api::core::v1::ServiceAccount;
34
use crate::k8s_openapi::api::rbac::v1::{RoleBinding, RoleRef, Subject};
45
use kube::{Resource, ResourceExt};
6+
use std::collections::BTreeMap;
57

68
/// Build RBAC objects for the product workloads.
79
/// The `rbac_prefix` is meant to be the product name, for example: zookeeper, airflow, etc.
810
/// and it is a assumed that a ClusterRole named `{rbac_prefix}-clusterrole` exists.
9-
pub fn build_rbac_resources<T: Resource>(
11+
pub fn build_rbac_resources<T: Clone + Resource<DynamicType = ()>>(
1012
resource: &T,
1113
rbac_prefix: &str,
12-
) -> (ServiceAccount, RoleBinding) {
14+
labels: BTreeMap<String, String>,
15+
) -> OperatorResult<(ServiceAccount, RoleBinding)> {
1316
let sa_name = format!("{rbac_prefix}-sa");
1417
let service_account = ServiceAccount {
1518
metadata: ObjectMetaBuilder::new()
1619
.name_and_namespace(resource)
1720
.name(sa_name.clone())
21+
.ownerreference_from_resource(resource, None, Some(true))?
22+
.with_labels(labels.clone())
1823
.build(),
1924
..ServiceAccount::default()
2025
};
@@ -23,6 +28,8 @@ pub fn build_rbac_resources<T: Resource>(
2328
metadata: ObjectMetaBuilder::new()
2429
.name_and_namespace(resource)
2530
.name(format!("{rbac_prefix}-rolebinding"))
31+
.ownerreference_from_resource(resource, None, Some(true))?
32+
.with_labels(labels)
2633
.build(),
2734
role_ref: RoleRef {
2835
kind: "ClusterRole".to_string(),
@@ -37,7 +44,7 @@ pub fn build_rbac_resources<T: Resource>(
3744
}]),
3845
};
3946

40-
(service_account, role_binding)
47+
Ok((service_account, role_binding))
4148
}
4249

4350
#[cfg(test)]
@@ -46,6 +53,7 @@ mod tests {
4653
use kube::CustomResource;
4754
use schemars::{self, JsonSchema};
4855
use serde::{Deserialize, Serialize};
56+
use std::collections::BTreeMap;
4957

5058
const CLUSTER_NAME: &str = "simple-cluster";
5159
const RESOURCE_NAME: &str = "test-resource";
@@ -64,6 +72,7 @@ mod tests {
6472
metadata:
6573
name: {CLUSTER_NAME}
6674
namespace: {CLUSTER_NAME}-ns
75+
uid: 12345
6776
spec:
6877
test: 100
6978
"
@@ -74,7 +83,8 @@ mod tests {
7483
#[test]
7584
fn test_build_rbac() {
7685
let cluster = build_test_resource();
77-
let (rbac_sa, rbac_rolebinding) = build_rbac_resources(&cluster, RESOURCE_NAME);
86+
let (rbac_sa, rbac_rolebinding) =
87+
build_rbac_resources(&cluster, RESOURCE_NAME, BTreeMap::new()).unwrap();
7888

7989
assert_eq!(
8090
Some(format!("{RESOURCE_NAME}-sa")),

0 commit comments

Comments
 (0)