Skip to content

Commit 8e27a99

Browse files
committed
feat!: Add PodSecurityContextBuilder::with_stackable_defaults
1 parent 7a5f0c3 commit 8e27a99

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

crates/stackable-operator/CHANGELOG.md

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

55
## [Unreleased]
66

7+
### Changed
8+
9+
- BREAKING: `PodSecurityContextBuilder::new` was removed in favor of `PodSecurityContextBuilder::with_stackable_defaults`.
10+
This function already sets up some defaults we want to use across the platform.
11+
- BREAKING: `PodSecurityContextBuilder::run_as_non_root` now takes a `bool` instead of assuming consumers always want to set it to `true`.
12+
This is needed to allow users setting it to `false` in case the new `with_stackable_defaults` functions set's it to `true`.
13+
714
## [0.111.1] - 2026-04-28
815

916
### Added

crates/stackable-operator/src/builder/pod/security.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,42 @@ impl SecurityContextBuilder {
144144
}
145145
}
146146

147-
#[derive(Clone, Default)]
147+
/// A builder to construct a [`PodSecurityContext`].
148+
///
149+
/// # Basic usage
150+
///
151+
/// ```
152+
/// use stackable_operator::builder::pod::security::PodSecurityContextBuilder;
153+
///
154+
/// let _ = PodSecurityContextBuilder::with_stackable_defaults()
155+
/// // Configure any arbitrary fields
156+
/// .run_as_user(1234)
157+
/// .build();
158+
/// ```
159+
#[derive(Clone, Debug)]
148160
pub struct PodSecurityContextBuilder {
149161
pod_security_context: PodSecurityContext,
150162
}
151163

152164
impl PodSecurityContextBuilder {
153-
pub fn new() -> Self {
154-
Self::default()
165+
/// Construct a new [`PodSecurityContextBuilder`] that is pre-filled with Stackable's defaults.
166+
pub fn with_stackable_defaults() -> Self {
167+
Self {
168+
pod_security_context: Self::stackable_default_pod_security_context(),
169+
}
170+
}
171+
172+
/// The Stackable's defaults for a [`PodSecurityContext`].
173+
///
174+
/// It is recommended to use the [`PodSecurityContextBuilder::with_stackable_defaults`] instead
175+
/// (if possible).
176+
pub fn stackable_default_pod_security_context() -> PodSecurityContext {
177+
todo!("Lars needs to define the exact settings he wants");
178+
179+
PodSecurityContext {
180+
run_as_non_root: Some(true),
181+
..Default::default()
182+
}
155183
}
156184

157185
pub fn build(&self) -> PodSecurityContext {
@@ -173,8 +201,8 @@ impl PodSecurityContextBuilder {
173201
self
174202
}
175203

176-
pub fn run_as_non_root(&mut self) -> &mut Self {
177-
self.pod_security_context.run_as_non_root = Some(true);
204+
pub fn run_as_non_root(&mut self, non_root: bool) -> &mut Self {
205+
self.pod_security_context.run_as_non_root = Some(non_root);
178206
self
179207
}
180208

@@ -381,13 +409,13 @@ mod tests {
381409

382410
#[test]
383411
fn security_context_builder() {
384-
let mut builder = PodSecurityContextBuilder::new();
412+
let mut builder = PodSecurityContextBuilder::with_stackable_defaults();
385413
let context = builder
386414
.fs_group(1000)
387415
.fs_group_change_policy("policy")
388416
.run_as_user(1001)
389417
.run_as_group(1001)
390-
.run_as_non_root()
418+
.run_as_non_root(true)
391419
.supplemental_groups(&[1002, 1003])
392420
.se_linux_level("level")
393421
.se_linux_role("role")

0 commit comments

Comments
 (0)