Skip to content

K8SPSMDB-1595: set container security context for pbm-init container#2283

Open
shajia-deshaw wants to merge 6 commits intopercona:mainfrom
shajia-deshaw:shajia/pbm-csc
Open

K8SPSMDB-1595: set container security context for pbm-init container#2283
shajia-deshaw wants to merge 6 commits intopercona:mainfrom
shajia-deshaw:shajia/pbm-csc

Conversation

@shajia-deshaw
Copy link
Copy Markdown

CHANGE DESCRIPTION

Problem:
The pbm-init init container added during physical/PITR restore is missing a SecurityContext, causing pod admission failures in namespaces which has PSA in restricted mode. More details in #2250

Cause:
updateStatefulSetForPhysicalRestore() calls EntrypointInitContainer() to create the pbm-init container but never assigns SecurityContext after creation - unlike the other caller, InitContainers() in init.go, which does apply it.

Solution:
Assign cluster.Spec.InitContainerSecurityContext to the pbm-init container after creation (for CR version ≥ 1.23.0 - idk if the version guard that I have here is the right version but per the discussion in the issue, the idea was to allow this for >= 1.23.0 versions? Please feel free to correct me here.), mirroring the existing pattern.

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?
  • Are OpenShift compare files changed for E2E tests (compare/*-oc.yml)?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported MongoDB version?
  • Does the change support oldest and newest supported Kubernetes version?

Copilot AI review requested due to automatic review settings March 13, 2026 04:49
@pull-request-size pull-request-size bot added the size/M 30-99 lines label Mar 13, 2026
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 13, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes pod admission failures under Kubernetes Pod Security Admission (PSA) restricted by ensuring the pbm-init init container created during physical/PITR restore receives the configured container SecurityContext.

Changes:

  • Set pbm-init.SecurityContext from cluster.Spec.InitContainerSecurityContext during physical restore.
  • Add a unit test asserting pbm-init gets the expected SecurityContext.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/controller/perconaservermongodbrestore/physical.go Applies InitContainerSecurityContext to the pbm-init init container during updateStatefulSetForPhysicalRestore().
pkg/controller/perconaservermongodbrestore/physical_test.go Adds a unit test validating the pbm-init container’s SecurityContext is set.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

egegunes
egegunes previously approved these changes Mar 13, 2026
@egegunes egegunes added this to the v1.23.0 milestone Mar 13, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses pod admission failures under Kubernetes Pod Security Admission (PSA) restricted by ensuring the pbm-init init container added during physical/PITR restore receives the configured container SecurityContext, matching existing init-container behavior elsewhere in the operator.

Changes:

  • Set pbm-init container SecurityContext from cluster.Spec.InitContainerSecurityContext during physical restore (version-gated).
  • Add a unit test asserting pbm-init receives the configured SecurityContext.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/controller/perconaservermongodbrestore/physical.go Apply init container security context to pbm-init during physical restore.
pkg/controller/perconaservermongodbrestore/physical_test.go Add unit test coverage for pbm-init security context propagation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

egegunes
egegunes previously approved these changes Mar 16, 2026
@shajia-deshaw
Copy link
Copy Markdown
Author

@egegunes - anything I can do from my side to move this forward?

@hors
Copy link
Copy Markdown
Collaborator

hors commented Mar 25, 2026

Hi @shajia-deshaw , sorry, we are at KubeCon and partially available. We will review your PR tomorrow. Thanks for the contribution.

@shajia-deshaw
Copy link
Copy Markdown
Author

Hi @shajia-deshaw , sorry, we are at KubeCon and partially available. We will review your PR tomorrow. Thanks for the contribution.

Thanks for the quick reply and no worries!

}

func TestUpdateStatefulSetForPhysicalRestoreSecurityContext(t *testing.T) {
ctx := context.Background()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since go 1.24, we can use t.Context() instead of context.Background()

assert.Equal(t, expectedURI, lastEnvVar.Value)
}

func TestUpdateStatefulSetForPhysicalRestoreSecurityContext(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a whole new test for only asserting the security context, mostly the same as the existing, more generic test, why don't we incorporate the SC assertion in the existing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the new test that I added and added the additional CSP assertions into the existing one so there's minimal changes now.

Copilot AI review requested due to automatic review settings March 27, 2026 10:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses Pod Security Admission (PSA) failures during physical/PITR restores by ensuring the pbm-init init container receives a SecurityContext, aligning it with how other init containers are configured elsewhere in the operator.

Changes:

  • Apply cluster.Spec.InitContainerSecurityContext to the pbm-init init container during physical restore StatefulSet mutation (with a version gate).
  • Add a unit test asserting pbm-init receives the configured SecurityContext.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/controller/perconaservermongodbrestore/physical.go Sets pbm-init.SecurityContext from cluster.Spec.InitContainerSecurityContext during physical restore StatefulSet updates.
pkg/controller/perconaservermongodbrestore/physical_test.go Adds a unit test validating pbm-init inherits the init container security context from the CR spec.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

cluster.Spec.ImagePullPolicy,
cmd,
)
if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version gate for applying InitContainerSecurityContext to pbm-init is >= 1.23.0, but the same field is already applied to other init containers for >= 1.14.0 (see pkg/psmdb/init.go where init.SecurityContext = cr.Spec.InitContainerSecurityContext is guarded by CompareVersion("1.14.0")). With the current 1.23.0 threshold, clusters running physical restore with spec.crVersion between 1.14.0 and 1.22.x can still miss the init container security context and continue to fail PSA restricted admission. Consider lowering/removing this version guard to match the existing init-container behavior so the setting is applied consistently across supported CR versions.

Suggested change
if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {
if cluster.CompareVersion("1.14.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {

Copilot uses AI. Check for mistakes.
@pull-request-size pull-request-size bot added size/S 10-29 lines and removed size/M 30-99 lines labels Mar 27, 2026
@shajia-deshaw shajia-deshaw requested a review from gkech March 27, 2026 10:08
Copilot AI review requested due to automatic review settings March 27, 2026 10:27
@pull-request-size pull-request-size bot added size/L 100-499 lines and removed size/S 10-29 lines labels Mar 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +44 to +54
{
name: "1_22_with_InitContainerSecurityContext_ignored",
crVersion: "1.22.0",
clusterInitSC: initSC,
wantPbmInitSC: nil,
},
{
name: "1_22_without_InitContainerSecurityContext",
crVersion: "1.22.0",
clusterInitSC: nil,
wantPbmInitSC: nil,
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These test cases encode that InitContainerSecurityContext should be ignored for CRVersion 1.22.0. That expectation looks inconsistent with existing behavior where init containers already apply spec.initContainerSecurityContext for versions >= 1.14.0 (see pkg/psmdb/init.go). If the restore path should mirror that behavior (and avoid PSA restricted failures for 1.14–1.22 clusters), update the test matrix/expected values accordingly.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because:

updateStatefulSetForPhysicalRestore() calls EntrypointInitContainer() to create the pbm-init container but never assigns SecurityContext after creation - unlike the other caller, InitContainers() in init.go, which does apply it.

currently but we're going to be enabling that from v1.23.0

@shajia-deshaw shajia-deshaw requested a review from egegunes March 27, 2026 10:34
@JNKPercona
Copy link
Copy Markdown
Collaborator

Test Name Result Time
arbiter failure 00:09:36
balancer passed 00:18:16
cross-site-sharded passed 00:18:36
custom-replset-name passed 00:15:10
custom-tls passed 00:14:52
custom-users-roles passed 00:10:41
custom-users-roles-sharded passed 00:11:45
data-at-rest-encryption passed 00:14:49
data-sharded passed 00:28:48
demand-backup passed 00:38:35
demand-backup-eks-credentials-irsa passed 00:00:08
demand-backup-fs passed 00:27:17
demand-backup-if-unhealthy passed 00:14:20
demand-backup-incremental-aws failure 00:06:27
demand-backup-incremental-azure passed 00:11:13
demand-backup-incremental-gcp-native passed 00:12:05
demand-backup-incremental-gcp-s3 passed 00:11:17
demand-backup-incremental-minio passed 00:28:38
demand-backup-incremental-sharded-aws passed 00:21:09
demand-backup-incremental-sharded-azure passed 00:20:47
demand-backup-incremental-sharded-gcp-native passed 00:20:55
demand-backup-incremental-sharded-gcp-s3 failure 00:12:36
demand-backup-incremental-sharded-minio passed 00:28:02
demand-backup-logical-minio-native-tls passed 00:08:43
demand-backup-physical-parallel passed 00:08:26
demand-backup-physical-aws passed 00:12:18
demand-backup-physical-azure passed 00:12:09
demand-backup-physical-gcp-s3 passed 00:11:58
demand-backup-physical-gcp-native passed 00:12:18
demand-backup-physical-minio failure 00:43:53
demand-backup-physical-minio-native passed 00:30:01
demand-backup-physical-minio-native-tls passed 00:20:33
demand-backup-physical-sharded-parallel passed 00:12:32
demand-backup-physical-sharded-aws passed 00:19:11
demand-backup-physical-sharded-azure passed 00:19:44
demand-backup-physical-sharded-gcp-native passed 00:17:47
demand-backup-physical-sharded-minio passed 00:18:45
demand-backup-physical-sharded-minio-native passed 00:18:07
demand-backup-sharded passed 00:27:25
demand-backup-snapshot passed 00:37:11
demand-backup-snapshot-vault failure 00:13:41
disabled-auth passed 00:17:21
expose-sharded failure 00:06:40
finalizer passed 00:11:31
ignore-labels-annotations passed 00:07:59
init-deploy passed 00:15:07
ldap passed 00:11:22
ldap-tls passed 00:12:48
limits passed 00:06:12
liveness passed 00:09:39
mongod-major-upgrade passed 00:12:06
mongod-major-upgrade-sharded passed 00:23:54
monitoring-2-0 passed 00:29:39
monitoring-pmm3 passed 00:30:08
multi-cluster-service passed 00:14:34
multi-storage passed 00:19:47
non-voting-and-hidden failure 00:08:15
one-pod passed 00:08:48
operator-self-healing-chaos passed 00:12:56
pitr passed 00:33:10
pitr-physical passed 01:05:26
pitr-sharded passed 00:25:58
pitr-to-new-cluster passed 00:26:10
pitr-physical-backup-source passed 00:56:12
preinit-updates passed 00:04:56
pvc-auto-resize passed 00:14:56
pvc-resize passed 00:17:17
recover-no-primary passed 00:26:51
replset-overrides passed 00:18:14
replset-remapping passed 00:17:16
replset-remapping-sharded passed 00:18:09
rs-shard-migration passed 00:14:38
scaling passed 00:11:39
scheduled-backup passed 00:18:21
security-context passed 00:07:13
self-healing-chaos passed 00:15:33
service-per-pod passed 00:19:23
serviceless-external-nodes passed 00:08:17
smart-update passed 00:08:46
split-horizon passed 00:14:11
stable-resource-version passed 00:04:48
storage passed 00:07:41
tls-issue-cert-manager passed 00:30:56
unsafe-psa passed 00:07:55
upgrade passed 00:10:21
upgrade-consistency passed 00:06:54
upgrade-consistency-sharded-tls passed 00:54:51
upgrade-sharded passed 00:20:32
upgrade-partial-backup passed 00:16:42
users passed 00:17:11
users-vault passed 00:13:20
version-service passed 00:25:43
Summary Value
Tests Run 92/92
Job Duration 03:55:47
Total Test Time 27:39:47

commit: e32dd8b
image: perconalab/percona-server-mongodb-operator:PR-2283-e32dd8b3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L 100-499 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants