Skip to content

Commit 43fe29e

Browse files
authored
feat!: Add (Pod)SecurityContextBuilder::with_stackable_defaults (#1205)
* feat!: Add `PodSecurityContextBuilder::with_stackable_defaults` * We decided on only runAsNonRoot for now * Remove stackable_default_pod_security_context fn * Use builder functions to ensure we can override * Do the same thing for SecurityContextBuilder * typo * changelog * Remove SecurityContextBuilder::run_as_root * fix: Remove SecurityContextBuilder::default * chore: Don't default to runAsNonRoot in SecurityContextBuilder * fix changelog * Update docs
1 parent 9128c34 commit 43fe29e

2 files changed

Lines changed: 55 additions & 17 deletions

File tree

crates/stackable-operator/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ 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+
(same for `SecurityContextBuilder`) ([#1205]).
11+
This function already sets up some defaults we want to use across the platform.
12+
Currently this is `runAsNonRoot: true` for `PodSecurityContextBuilder`, which might cause product Pods to crash and require changes.
13+
- BREAKING: `PodSecurityContextBuilder::run_as_non_root` now takes a `bool` instead of assuming consumers always want to set it to `true` ([#1205]).
14+
This is needed to allow users setting it to `false` in case the new `with_stackable_defaults` function sets it to `true`.
15+
- BREAKING: `SecurityContextBuilder::run_as_root` has been removed ([#1205]).
16+
17+
[#1205]: https://github.com/stackabletech/operator-rs/pull/1205
18+
719
## [0.114.0] - 2026-07-22
820

921
### Added

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

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ use k8s_openapi::api::core::v1::{
44
};
55

66
/// A builder for [`SecurityContext`] objects (not to be confused with `PodSecurityContext`).
7-
#[derive(Clone, Default)]
7+
#[derive(Clone)]
88
pub struct SecurityContextBuilder {
99
security_context: SecurityContext,
1010
}
1111

1212
impl SecurityContextBuilder {
13-
/// Convenience function for a wide use-case.
14-
pub fn run_as_root() -> SecurityContext {
15-
SecurityContext {
16-
run_as_user: Some(0),
17-
..SecurityContext::default()
13+
/// Construct a new [`SecurityContextBuilder`] that is pre-filled with Stackable's defaults.
14+
///
15+
/// We currently don't have any defaults we set.
16+
///
17+
/// We intentionally don't set `runAsNonRoot`, as we set that in
18+
/// [`PodSecurityContextBuilder::with_stackable_defaults`] already and don't want to confuse
19+
/// users by setting it on the Pod and container.
20+
pub fn with_stackable_defaults() -> Self {
21+
Self {
22+
security_context: SecurityContext::default(),
1823
}
1924
}
2025

21-
pub fn new() -> Self {
22-
Self::default()
23-
}
24-
2526
pub fn allow_privilege_escalation(&mut self, value: bool) -> &mut Self {
2627
self.security_context.allow_privilege_escalation = Some(value);
2728
self
@@ -144,14 +145,39 @@ impl SecurityContextBuilder {
144145
}
145146
}
146147

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

152165
impl PodSecurityContextBuilder {
153-
pub fn new() -> Self {
154-
Self::default()
166+
/// Construct a new [`PodSecurityContextBuilder`] that is pre-filled with Stackable's defaults.
167+
///
168+
/// Currently the defaults are:
169+
///
170+
/// * `runAsNonRoot: true`
171+
pub fn with_stackable_defaults() -> Self {
172+
// We are using the builder functions to ensure that builder functions exist to override these settings.
173+
let mut builder = Self {
174+
pod_security_context: PodSecurityContext::default(),
175+
};
176+
177+
// Reason: Running as root is bad
178+
builder.run_as_non_root(true);
179+
180+
builder
155181
}
156182

157183
pub fn build(&self) -> PodSecurityContext {
@@ -173,8 +199,8 @@ impl PodSecurityContextBuilder {
173199
self
174200
}
175201

176-
pub fn run_as_non_root(&mut self) -> &mut Self {
177-
self.pod_security_context.run_as_non_root = Some(true);
202+
pub fn run_as_non_root(&mut self, non_root: bool) -> &mut Self {
203+
self.pod_security_context.run_as_non_root = Some(non_root);
178204
self
179205
}
180206

@@ -381,13 +407,13 @@ mod tests {
381407

382408
#[test]
383409
fn security_context_builder() {
384-
let mut builder = PodSecurityContextBuilder::new();
410+
let mut builder = PodSecurityContextBuilder::with_stackable_defaults();
385411
let context = builder
386412
.fs_group(1000)
387413
.fs_group_change_policy("policy")
388414
.run_as_user(1001)
389415
.run_as_group(1001)
390-
.run_as_non_root()
416+
.run_as_non_root(true)
391417
.supplemental_groups(&[1002, 1003])
392418
.se_linux_level("level")
393419
.se_linux_role("role")

0 commit comments

Comments
 (0)