From e24238ef4e6290b4b26c5caf78a26ce3ac40367e Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Wed, 2 Dec 2026 00:35:37 +0100 Subject: [PATCH 1/4] feat: Harden every container by default --- crates/stackable-operator/CHANGELOG.md | 10 ++++++ .../src/builder/pod/container.rs | 36 ++++++++++++++++++- .../stackable-operator/src/builder/pod/mod.rs | 10 ++++++ .../src/builder/pod/security.rs | 5 +-- ..._tests__multiple_git_syncs_containers.snap | 15 ++++++++ ...s__multiple_git_syncs_init_containers.snap | 15 ++++++++ ...l__tests__git_sync_ca_cert_containers.snap | 5 +++ ...sts__git_sync_ca_cert_init_containers.snap | 5 +++ ..._impl__tests__git_sync_ssh_containers.snap | 5 +++ ...__tests__git_sync_ssh_init_containers.snap | 5 +++ ..._tests__multiple_git_syncs_containers.snap | 15 ++++++++ ...s__multiple_git_syncs_init_containers.snap | 15 ++++++++ .../src/v2/product_logging/framework.rs | 6 ++++ 13 files changed, 144 insertions(+), 3 deletions(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index e85fddeb7..813d70519 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Changed + +- BREAKING: `ContainerBuilder::new` starts every container with `SecurityContextBuilder::with_stackable_defaults`, + so containers set `allowPrivilegeEscalation: false` and drop all capabilities. A container that needs to be + privileged or needs a capability has to replace the security context with `ContainerBuilder::security_context`. + `podOverrides` that set `privileged: true` or add the `CAP_SYS_ADMIN` capability on a container now produce a Pod + that Kubernetes rejects, unless they also set `allowPrivilegeEscalation: true`. A container that runs as root + through `podOverrides` no longer has the runtime's default capabilities such as `CHOWN` or `SETUID`. + `capabilities.drop: []` in the `podOverrides` restores them. + ## [0.123.0] - 2026-12-02 ### Added diff --git a/crates/stackable-operator/src/builder/pod/container.rs b/crates/stackable-operator/src/builder/pod/container.rs index a5c4f780f..d7c6d3f13 100644 --- a/crates/stackable-operator/src/builder/pod/container.rs +++ b/crates/stackable-operator/src/builder/pod/container.rs @@ -11,6 +11,7 @@ use snafu::{ResultExt as _, Snafu}; use {k8s_openapi::api::core::v1::PodSpec, std::collections::BTreeMap}; use crate::{ + builder::pod::security::SecurityContextBuilder, commons::product_image_selection::ResolvedProductImage, validation::{self, is_lowercase_rfc_1123_label}, }; @@ -65,10 +66,14 @@ pub struct ContainerBuilder { } impl ContainerBuilder { + /// The container starts with [`SecurityContextBuilder::with_stackable_defaults`]. A privileged + /// container has to replace it via [`Self::security_context`], as Kubernetes rejects + /// `privileged: true` together with `allowPrivilegeEscalation: false`. pub fn new(name: &str) -> Result { Self::validate_container_name(name)?; Ok(Self { name: name.to_string(), + security_context: Some(SecurityContextBuilder::with_stackable_defaults().build()), ..Self::default() }) } @@ -320,6 +325,7 @@ impl ContainerBuilder { self } + /// Replaces the whole security context, including the defaults set by [`Self::new`]. pub fn security_context(&mut self, context: SecurityContext) -> &mut Self { self.security_context = Some(context); self @@ -438,7 +444,7 @@ impl fmt::Display for FieldPathEnvVar { #[cfg(test)] mod tests { - use k8s_openapi::api::core::v1::ExecAction; + use k8s_openapi::api::core::v1::{Capabilities, ExecAction}; use super::*; use crate::{ @@ -450,6 +456,34 @@ mod tests { validation::LOWERCASE_RFC_1123_LABEL_FMT, }; + #[test] + fn security_context_defaults_and_override() { + let container = ContainerBuilder::new("hardened") + .expect("valid container name") + .build(); + assert_eq!( + container.security_context, + Some(SecurityContext { + allow_privilege_escalation: Some(false), + capabilities: Some(Capabilities { + drop: Some(vec!["ALL".to_owned()]), + ..Default::default() + }), + ..Default::default() + }) + ); + + let privileged = SecurityContext { + privileged: Some(true), + ..Default::default() + }; + let container = ContainerBuilder::new("privileged") + .expect("valid container name") + .security_context(privileged.clone()) + .build(); + assert_eq!(container.security_context, Some(privileged)); + } + #[test] fn builder() { let container_port: i32 = 10000; diff --git a/crates/stackable-operator/src/builder/pod/mod.rs b/crates/stackable-operator/src/builder/pod/mod.rs index 303682c4d..08f6b3bca 100644 --- a/crates/stackable-operator/src/builder/pod/mod.rs +++ b/crates/stackable-operator/src/builder/pod/mod.rs @@ -368,6 +368,11 @@ impl PodBuilder { /// affinity: {} /// containers: /// - name: container + /// securityContext: + /// allowPrivilegeEscalation: false + /// capabilities: + /// drop: + /// - ALL /// volumeMounts: /// - mountPath: /path/to/volume /// name: listener @@ -458,6 +463,11 @@ impl PodBuilder { /// affinity: {} /// containers: /// - name: container + /// securityContext: + /// allowPrivilegeEscalation: false + /// capabilities: + /// drop: + /// - ALL /// volumeMounts: /// - mountPath: /path/to/volume /// name: listener diff --git a/crates/stackable-operator/src/builder/pod/security.rs b/crates/stackable-operator/src/builder/pod/security.rs index 846297f8e..c4a217c9e 100644 --- a/crates/stackable-operator/src/builder/pod/security.rs +++ b/crates/stackable-operator/src/builder/pod/security.rs @@ -19,8 +19,9 @@ impl SecurityContextBuilder { /// * `allowPrivilegeEscalation: false` /// * `capabilities.drop: [ALL]` /// - /// Neither field exists on [`PodSecurityContext`], - /// so both have to be set on every container to take effect. + /// Neither field exists on [`PodSecurityContext`], so both have to be set on every container + /// to take effect. [`ContainerBuilder::new`](crate::builder::pod::container::ContainerBuilder::new) + /// starts every container with them. /// /// We intentionally don't set `runAsNonRoot` or `seccompProfile`, as we set those in /// [`PodSecurityContextBuilder::with_stackable_defaults`] already and don't want to confuse diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap index 9e98acbf1..88a85d8b3 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap @@ -80,6 +80,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 @@ -170,6 +175,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-1 @@ -255,6 +265,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-2 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap index d85323c3e..831bed9b5 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap @@ -40,6 +40,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 @@ -90,6 +95,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-1 @@ -135,6 +145,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-2 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap index a7e9b5de5..1640c9744 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap @@ -78,6 +78,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap index 1d349aa78..10da90e62 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap @@ -38,6 +38,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap index 45bff4240..24bb6ac33 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap @@ -82,6 +82,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap index 57453cd59..4e738b82a 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap @@ -42,6 +42,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap index 624508786..3eea39bfb 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap @@ -80,6 +80,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 @@ -170,6 +175,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-1 @@ -255,6 +265,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-2 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap index 64ba69b04..6971aaa48 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap @@ -40,6 +40,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-0 @@ -90,6 +95,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-1 @@ -135,6 +145,11 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) requests: cpu: 100m memory: 64Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL volumeMounts: - mountPath: /tmp/git name: content-from-git-2 diff --git a/crates/stackable-operator/src/v2/product_logging/framework.rs b/crates/stackable-operator/src/v2/product_logging/framework.rs index 3a2d865f6..7ed072226 100644 --- a/crates/stackable-operator/src/v2/product_logging/framework.rs +++ b/crates/stackable-operator/src/v2/product_logging/framework.rs @@ -499,6 +499,12 @@ mod tests { "memory": "128Mi", }, }, + "securityContext": { + "allowPrivilegeEscalation": false, + "capabilities": { + "drop": ["ALL"], + }, + }, "volumeMounts": [ { "mountPath": "/stackable/config/vector.yaml", From d57a5a022d0120428fc77f3d163f0429f95b75fd Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Thu, 3 Dec 2026 08:48:47 +0100 Subject: [PATCH 2/4] chore: Add the PR number to the changelog entry --- crates/stackable-operator/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index 813d70519..e118b7c8c 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -12,7 +12,9 @@ All notable changes to this project will be documented in this file. `podOverrides` that set `privileged: true` or add the `CAP_SYS_ADMIN` capability on a container now produce a Pod that Kubernetes rejects, unless they also set `allowPrivilegeEscalation: true`. A container that runs as root through `podOverrides` no longer has the runtime's default capabilities such as `CHOWN` or `SETUID`. - `capabilities.drop: []` in the `podOverrides` restores them. + `capabilities.drop: []` in the `podOverrides` restores them ([#1347]). + +[#1347]: https://github.com/stackabletech/operator-rs/pull/1347 ## [0.123.0] - 2026-12-02 From 328197419395136beb0f67326fd753eac16094d7 Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Fri, 4 Dec 2026 15:26:00 +0100 Subject: [PATCH 3/4] refactor: Rename ContainerBuilder::new to with_stackable_defaults --- crates/stackable-operator/CHANGELOG.md | 14 +- .../src/builder/pod/container.rs | 125 ++++++++++-------- .../stackable-operator/src/builder/pod/mod.rs | 8 +- .../src/builder/pod/security.rs | 5 +- .../src/crd/git_sync/v1alpha1_impl.rs | 2 +- .../src/crd/git_sync/v1alpha2_impl.rs | 2 +- .../src/database_connections/tests.rs | 4 +- .../src/product_logging/framework.rs | 8 +- .../src/v2/builder/pod/container.rs | 5 +- 9 files changed, 98 insertions(+), 75 deletions(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index e118b7c8c..e05b2cb73 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -6,13 +6,13 @@ All notable changes to this project will be documented in this file. ### Changed -- BREAKING: `ContainerBuilder::new` starts every container with `SecurityContextBuilder::with_stackable_defaults`, - so containers set `allowPrivilegeEscalation: false` and drop all capabilities. A container that needs to be - privileged or needs a capability has to replace the security context with `ContainerBuilder::security_context`. - `podOverrides` that set `privileged: true` or add the `CAP_SYS_ADMIN` capability on a container now produce a Pod - that Kubernetes rejects, unless they also set `allowPrivilegeEscalation: true`. A container that runs as root - through `podOverrides` no longer has the runtime's default capabilities such as `CHOWN` or `SETUID`. - `capabilities.drop: []` in the `podOverrides` restores them ([#1347]). +- BREAKING: `ContainerBuilder::new` was removed in favor of `ContainerBuilder::with_stackable_defaults` ([#1347]). + `ContainerBuilder::default` was removed as well. + This function already sets up some defaults we want to use across the platform, currently this is + the `securityContext` from `SecurityContextBuilder::with_stackable_defaults`, which might cause + product Pods to crash and require changes. + This also applies to containers built by `v2::builder::pod::container::new_container_builder`, + `product_logging::framework::vector_container` (v1 and v2) and git-sync, whose signatures did not change. [#1347]: https://github.com/stackabletech/operator-rs/pull/1347 diff --git a/crates/stackable-operator/src/builder/pod/container.rs b/crates/stackable-operator/src/builder/pod/container.rs index d7c6d3f13..f54ca01e5 100644 --- a/crates/stackable-operator/src/builder/pod/container.rs +++ b/crates/stackable-operator/src/builder/pod/container.rs @@ -45,7 +45,7 @@ pub enum Error { /// We are also choosing it over a [`BTreeMap`], because it is easier to debug for users, as logically /// grouped volumeMounts (e.g. all volumeMounts related to S3) are near each other in the list instead of "just" being /// sorted alphabetically. -#[derive(Clone, Default)] +#[derive(Clone)] pub struct ContainerBuilder { args: Option>, container_ports: Option>, @@ -66,16 +66,38 @@ pub struct ContainerBuilder { } impl ContainerBuilder { - /// The container starts with [`SecurityContextBuilder::with_stackable_defaults`]. A privileged - /// container has to replace it via [`Self::security_context`], as Kubernetes rejects - /// `privileged: true` together with `allowPrivilegeEscalation: false`. - pub fn new(name: &str) -> Result { - Self::validate_container_name(name)?; - Ok(Self { - name: name.to_string(), - security_context: Some(SecurityContextBuilder::with_stackable_defaults().build()), - ..Self::default() - }) + /// Construct a new [`ContainerBuilder`] that is pre-filled with Stackable's defaults. + /// + /// Currently the defaults are: + /// + /// * `securityContext` is set to the contents of [`SecurityContextBuilder::with_stackable_defaults`] + pub fn with_stackable_defaults(name: impl Into) -> Result { + let name = name.into(); + Self::validate_container_name(&name)?; + + // We are using the builder functions to ensure that builder functions exist to override these settings. + let mut builder = Self { + name, + args: None, + container_ports: None, + command: None, + env: None, + image: None, + image_pull_policy: None, + resources: None, + volume_mounts: IndexMap::new(), + readiness_probe: None, + liveness_probe: None, + startup_probe: None, + lifecycle: None, + security_context: None, + }; + + // Reason: We want to ship the securityContext by default and don't rely on operators to + // explicitly set it. + builder.security_context(SecurityContextBuilder::with_stackable_defaults().build()); + + Ok(builder) } pub fn image(&mut self, image: impl Into) -> &mut Self { @@ -325,7 +347,6 @@ impl ContainerBuilder { self } - /// Replaces the whole security context, including the defaults set by [`Self::new`]. pub fn security_context(&mut self, context: SecurityContext) -> &mut Self { self.security_context = Some(context); self @@ -456,34 +477,6 @@ mod tests { validation::LOWERCASE_RFC_1123_LABEL_FMT, }; - #[test] - fn security_context_defaults_and_override() { - let container = ContainerBuilder::new("hardened") - .expect("valid container name") - .build(); - assert_eq!( - container.security_context, - Some(SecurityContext { - allow_privilege_escalation: Some(false), - capabilities: Some(Capabilities { - drop: Some(vec!["ALL".to_owned()]), - ..Default::default() - }), - ..Default::default() - }) - ); - - let privileged = SecurityContext { - privileged: Some(true), - ..Default::default() - }; - let container = ContainerBuilder::new("privileged") - .expect("valid container name") - .security_context(privileged.clone()) - .build(); - assert_eq!(container.security_context, Some(privileged)); - } - #[test] fn builder() { let container_port: i32 = 10000; @@ -499,7 +492,7 @@ mod tests { .with_resource(ResourceRequirementsType::Limits, "nvidia.com/gpu", "1") .build(); - let container = ContainerBuilder::new("testcontainer") + let container = ContainerBuilder::with_stackable_defaults("testcontainer") .expect("ContainerBuilder not created") .add_env_var("foo", "bar") .add_env_var_from_config_map("envFromConfigMap", "my-configmap", "my-key") @@ -561,7 +554,7 @@ mod tests { }), ..Default::default() }; - let container = ContainerBuilder::new("testcontainer") + let container = ContainerBuilder::with_stackable_defaults("testcontainer") .expect("ContainerBuilder not created") .lifecycle_post_start(post_start.clone()) .lifecycle_pre_stop(pre_stop.clone()) @@ -614,7 +607,7 @@ mod tests { let long_container_name = "lengthexceededlengthexceededlengthexceededlengthexceededlengthex"; assert_eq!(long_container_name.len(), 64); // 63 characters is the limit for container names - let result = ContainerBuilder::new(long_container_name); + let result = ContainerBuilder::with_stackable_defaults(long_container_name); if let Error::InvalidContainerName { container_name, source, @@ -631,37 +624,37 @@ mod tests { // One characters shorter name is valid let max_len_container_name: String = long_container_name.chars().skip(1).collect(); assert_eq!(max_len_container_name.len(), 63); - assert!(ContainerBuilder::new(&max_len_container_name).is_ok()); + assert!(ContainerBuilder::with_stackable_defaults(&max_len_container_name).is_ok()); } #[test] fn name_alphabet_only() { - ContainerBuilder::new("okname").unwrap(); + ContainerBuilder::with_stackable_defaults("okname").unwrap(); } #[test] fn name_hyphen() { - assert!(ContainerBuilder::new("name-with-hyphen").is_ok()); + assert!(ContainerBuilder::with_stackable_defaults("name-with-hyphen").is_ok()); assert_container_builder_err( - ContainerBuilder::new("ends-with-hyphen-"), + ContainerBuilder::with_stackable_defaults("ends-with-hyphen-"), &format!(r#"regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""#), ); assert_container_builder_err( - ContainerBuilder::new("-starts-with-hyphen"), + ContainerBuilder::with_stackable_defaults("-starts-with-hyphen"), &format!(r#"regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""#), ); } #[test] fn name_contains_number() { - assert!(ContainerBuilder::new("1name-0-name1").is_ok()); + assert!(ContainerBuilder::with_stackable_defaults("1name-0-name1").is_ok()); } #[test] fn name_contains_underscore() { - assert!(ContainerBuilder::new("name_name").is_err()); + assert!(ContainerBuilder::with_stackable_defaults("name_name").is_err()); assert_container_builder_err( - ContainerBuilder::new("name_name"), + ContainerBuilder::with_stackable_defaults("name_name"), &format!( r#"(e.g. "example-label", or "1-label-1", regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""# ), @@ -678,7 +671,7 @@ mod tests { .with_resource(ResourceRequirementsType::Limits, "nvidia.com/gpu", "1") .build(); - let container = ContainerBuilder::new("testcontainer") + let container = ContainerBuilder::with_stackable_defaults("testcontainer") .expect("ContainerBuilder not created") .resources(resources.clone()) .build(); @@ -686,6 +679,34 @@ mod tests { assert_eq!(container.resources, Some(resources)); } + #[test] + fn security_context_defaults_and_override() { + let container = ContainerBuilder::with_stackable_defaults("hardened") + .expect("valid container name") + .build(); + assert_eq!( + container.security_context, + Some(SecurityContext { + allow_privilege_escalation: Some(false), + capabilities: Some(Capabilities { + drop: Some(vec!["ALL".to_owned()]), + ..Default::default() + }), + ..Default::default() + }) + ); + + let privileged = SecurityContext { + privileged: Some(true), + ..Default::default() + }; + let container = ContainerBuilder::with_stackable_defaults("privileged") + .expect("valid container name") + .security_context(privileged.clone()) + .build(); + assert_eq!(container.security_context, Some(privileged)); + } + /// Panics if given container builder constructor result is not [Err] with error message /// containing expected violation. fn assert_container_builder_err( diff --git a/crates/stackable-operator/src/builder/pod/mod.rs b/crates/stackable-operator/src/builder/pod/mod.rs index 08f6b3bca..70684065a 100644 --- a/crates/stackable-operator/src/builder/pod/mod.rs +++ b/crates/stackable-operator/src/builder/pod/mod.rs @@ -348,7 +348,7 @@ impl PodBuilder { /// let pod = PodBuilder::new() /// .metadata_default() /// .add_container( - /// ContainerBuilder::new("container") + /// ContainerBuilder::with_stackable_defaults("container") /// .unwrap() /// .add_volume_mount("listener", "/path/to/volume") /// .unwrap() @@ -443,7 +443,7 @@ impl PodBuilder { /// let pod = PodBuilder::new() /// .metadata_default() /// .add_container( - /// ContainerBuilder::new("container") + /// ContainerBuilder::with_stackable_defaults("container") /// .unwrap() /// .add_volume_mount("listener", "/path/to/volume") /// .unwrap() @@ -671,7 +671,7 @@ mod tests { .with_memory_limit("128Mi") .build(); - ContainerBuilder::new("container") + ContainerBuilder::with_stackable_defaults("container") .expect("ContainerBuilder not created") .image("private-company/product:2.4.14") .resources(resources) @@ -728,7 +728,7 @@ mod tests { #[rstest] fn builder(pod_affinity: PodAffinity, dummy_container: Container) { - let init_container = ContainerBuilder::new("init-containername") + let init_container = ContainerBuilder::with_stackable_defaults("init-containername") .expect("ContainerBuilder not created") .image("stackable/zookeeper:2.4.14") .build(); diff --git a/crates/stackable-operator/src/builder/pod/security.rs b/crates/stackable-operator/src/builder/pod/security.rs index c4a217c9e..83d91ee92 100644 --- a/crates/stackable-operator/src/builder/pod/security.rs +++ b/crates/stackable-operator/src/builder/pod/security.rs @@ -20,8 +20,9 @@ impl SecurityContextBuilder { /// * `capabilities.drop: [ALL]` /// /// Neither field exists on [`PodSecurityContext`], so both have to be set on every container - /// to take effect. [`ContainerBuilder::new`](crate::builder::pod::container::ContainerBuilder::new) - /// starts every container with them. + /// to take effect. [`ContainerBuilder::with_stackable_defaults`] starts every container with them. + /// + /// [`ContainerBuilder::with_stackable_defaults`]: crate::builder::pod::container::ContainerBuilder::with_stackable_defaults /// /// We intentionally don't set `runAsNonRoot` or `seccompProfile`, as we set those in /// [`PodSecurityContextBuilder::with_stackable_defaults`] already and don't want to confuse diff --git a/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs b/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs index 141a94355..cab694e4b 100644 --- a/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs +++ b/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs @@ -218,7 +218,7 @@ impl GitSyncResources { volume_mounts: &[VolumeMount], container_log_config: &ContainerLogConfig, ) -> Result { - let container = ContainerBuilder::new(container_name) + let container = ContainerBuilder::with_stackable_defaults(container_name) .context(InvalidContainerNameSnafu)? .image_from_product_image(resolved_product_image) .command(vec![ diff --git a/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs b/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs index 9d7ab487c..bcb4e219e 100644 --- a/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +++ b/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs @@ -336,7 +336,7 @@ impl GitSyncResources { container_log_config: &ContainerLogConfig, ca_cert_path: Option<&str>, ) -> Result { - let container = ContainerBuilder::new(container_name) + let container = ContainerBuilder::with_stackable_defaults(container_name) .context(InvalidContainerNameSnafu)? .image_from_product_image(resolved_product_image) .command(vec![ diff --git a/crates/stackable-operator/src/database_connections/tests.rs b/crates/stackable-operator/src/database_connections/tests.rs index 13a8b5acd..a3c64f07d 100644 --- a/crates/stackable-operator/src/database_connections/tests.rs +++ b/crates/stackable-operator/src/database_connections/tests.rs @@ -72,7 +72,7 @@ fn test_dummy_jdbc_database_usage() { let jdbc_connection_details = dummy_jdbc_connection .jdbc_connection_details("persistence") .unwrap(); - let mut container_builder = ContainerBuilder::new("my-container").unwrap(); + let mut container_builder = ContainerBuilder::with_stackable_defaults("my-container").unwrap(); jdbc_connection_details.add_to_container(&mut container_builder); let container = container_builder.build(); @@ -107,7 +107,7 @@ fn test_dummy_celery_database_usage() { "worker_queue", &TemplatingMechanism::BashEnvSubstitution, ); - let mut container_builder = ContainerBuilder::new("my-container").unwrap(); + let mut container_builder = ContainerBuilder::with_stackable_defaults("my-container").unwrap(); celery_connection_details.add_to_container(&mut container_builder); let container = container_builder.build(); diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index 4cb0b0d2e..e25994b62 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -196,7 +196,7 @@ stackable_wait_for_tee() { /// } /// args.push("echo Test".into()); /// -/// let init_container = ContainerBuilder::new("init") +/// let init_container = ContainerBuilder::with_stackable_defaults("init") /// .unwrap() /// .command(vec!["bash".to_string(), "-c".to_string()]) /// .args(vec![args.join(" && ")]) @@ -1444,7 +1444,7 @@ sinks: /// .build(); /// /// pod_builder.add_container( -/// ContainerBuilder::new("application") +/// ContainerBuilder::with_stackable_defaults("application") /// .unwrap() /// .image_from_product_image(&resolved_product_image) /// .args(vec![format!( @@ -1491,7 +1491,7 @@ pub fn vector_container( LogLevel::INFO }; - Ok(ContainerBuilder::new("vector") + Ok(ContainerBuilder::with_stackable_defaults("vector") .context(CreateContainerSnafu)? .image_from_product_image(image) .command(vec![ @@ -1549,7 +1549,7 @@ kill $vector_pid /// product_logging::framework::create_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// ]; /// -/// let container = ContainerBuilder::new("init") +/// let container = ContainerBuilder::with_stackable_defaults("init") /// .unwrap() /// .image("oci.stackable.tech/sdp/my-product:1.0.0-stackable1.0.0") /// .command(vec!["bash".to_string(), "-c".to_string()]) diff --git a/crates/stackable-operator/src/v2/builder/pod/container.rs b/crates/stackable-operator/src/v2/builder/pod/container.rs index 0f79e178e..3b4312124 100644 --- a/crates/stackable-operator/src/v2/builder/pod/container.rs +++ b/crates/stackable-operator/src/v2/builder/pod/container.rs @@ -45,9 +45,10 @@ pub enum Error { }, } -/// Infallible variant of [`crate::builder::pod::container::ContainerBuilder::new`] +/// Infallible variant of [`crate::builder::pod::container::ContainerBuilder::with_stackable_defaults`] pub fn new_container_builder(container_name: &ContainerName) -> ContainerBuilder { - ContainerBuilder::new(container_name.as_ref()).expect("should be a valid container name") + ContainerBuilder::with_stackable_defaults(container_name.as_ref()) + .expect("should be a valid container name") } attributed_string_type! { From 1cac5d05ce9358e0c38be89d2aecb6c423c2114e Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Fri, 4 Dec 2026 15:47:40 +0100 Subject: [PATCH 4/4] docs: Shorten the changelog entry and state the constructor's contract --- crates/stackable-operator/CHANGELOG.md | 12 +++++------- .../stackable-operator/src/builder/pod/container.rs | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index e05b2cb73..5131dead6 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -6,13 +6,11 @@ All notable changes to this project will be documented in this file. ### Changed -- BREAKING: `ContainerBuilder::new` was removed in favor of `ContainerBuilder::with_stackable_defaults` ([#1347]). - `ContainerBuilder::default` was removed as well. - This function already sets up some defaults we want to use across the platform, currently this is - the `securityContext` from `SecurityContextBuilder::with_stackable_defaults`, which might cause - product Pods to crash and require changes. - This also applies to containers built by `v2::builder::pod::container::new_container_builder`, - `product_logging::framework::vector_container` (v1 and v2) and git-sync, whose signatures did not change. +- BREAKING: `ContainerBuilder::new` and `ContainerBuilder::default` are removed. Use + `ContainerBuilder::with_stackable_defaults`, which sets the container `securityContext` to + `SecurityContextBuilder::with_stackable_defaults`. Vector, git-sync and + `v2::builder::pod::container::new_container_builder` go through it. `ContainerBuilder::security_context` + replaces it ([#1347]). [#1347]: https://github.com/stackabletech/operator-rs/pull/1347 diff --git a/crates/stackable-operator/src/builder/pod/container.rs b/crates/stackable-operator/src/builder/pod/container.rs index f54ca01e5..8ad3e0bd6 100644 --- a/crates/stackable-operator/src/builder/pod/container.rs +++ b/crates/stackable-operator/src/builder/pod/container.rs @@ -66,16 +66,15 @@ pub struct ContainerBuilder { } impl ContainerBuilder { - /// Construct a new [`ContainerBuilder`] that is pre-filled with Stackable's defaults. + /// Starts the container with [`SecurityContextBuilder::with_stackable_defaults`] as its security + /// context. [`Self::security_context`] replaces the whole context, so a caller that needs another + /// field starts from [`SecurityContextBuilder::with_stackable_defaults`] and adds it. /// - /// Currently the defaults are: - /// - /// * `securityContext` is set to the contents of [`SecurityContextBuilder::with_stackable_defaults`] + /// Returns an error if `name` is not a lowercase RFC 1123 label. pub fn with_stackable_defaults(name: impl Into) -> Result { let name = name.into(); Self::validate_container_name(&name)?; - // We are using the builder functions to ensure that builder functions exist to override these settings. let mut builder = Self { name, args: None, @@ -93,8 +92,7 @@ impl ContainerBuilder { security_context: None, }; - // Reason: We want to ship the securityContext by default and don't rely on operators to - // explicitly set it. + // Set through the builder function, so that the same function can override it. builder.security_context(SecurityContextBuilder::with_stackable_defaults().build()); Ok(builder)