Skip to content

feat(serviceaccount): support shared ServiceAccount across clusters and poolers#9287

Merged
leonardoce merged 20 commits intocloudnative-pg:mainfrom
bozkayasalihx:feat/shared-service-account
Mar 11, 2026
Merged

feat(serviceaccount): support shared ServiceAccount across clusters and poolers#9287
leonardoce merged 20 commits intocloudnative-pg:mainfrom
bozkayasalihx:feat/shared-service-account

Conversation

@bozkayasalihx
Copy link
Contributor

@bozkayasalihx bozkayasalihx commented Nov 22, 2025

Add an optional serviceAccountName field to both ClusterSpec and PoolerSpec that allows referencing a pre-existing ServiceAccount instead of having the operator create one per resource. This enables one-time IAM configuration (AWS IRSA, GCP Workload Identity, Azure Workload Identity) that works across all clusters and poolers using that ServiceAccount.

When specified, the operator validates the referenced ServiceAccount exists, skips creating an operator-managed one, and uses it in pod specs, job specs, deployments, and RoleBinding subjects. The field is immutable once set, mutually exclusive with serviceAccountTemplate on Cluster, and validated against DNS subdomain naming rules. No breaking changes — defaults to the resource name when not specified.

Closes #10276

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 22, 2025
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 release-1.26 release-1.27 labels Nov 22, 2025
@github-actions
Copy link
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@dosubot dosubot bot added enhancement 🪄 New feature or request ok to merge 👌 This PR can be merged labels Nov 22, 2025
@bozkayasalihx bozkayasalihx force-pushed the feat/shared-service-account branch from a4f2a4f to cdc0384 Compare November 23, 2025 19:47
@bozkayasalihx bozkayasalihx changed the title Feat/shared service account Feat: shared service account Nov 27, 2025
@bozkayasalihx bozkayasalihx changed the title Feat: shared service account feat: shared service account Nov 27, 2025
@armru armru force-pushed the feat/shared-service-account branch from cdc0384 to f263f28 Compare December 4, 2025 16:29
@armru
Copy link
Member

armru commented Dec 4, 2025

/test

@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2025

@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/19936223914

@armru
Copy link
Member

armru commented Dec 5, 2025

/test

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/19965529026

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Dec 5, 2025
@armru
Copy link
Member

armru commented Dec 5, 2025

@bozkayasalihx pushed a partial review, can you check my changes? Could you also add e2e test coverage for the pooler?

@bozkayasalihx
Copy link
Contributor Author

@armru I'll review your changes this weekend and add the pooler e2e test.

@beingvishalt
Copy link

bozkayasalihx

@bozkayasalihx , Have you got the chance to review this PR.
Can you please review this earliest, We all are waiting for this enhancement...

@bozkayasalihx
Copy link
Contributor Author

bozkayasalihx commented Dec 9, 2025

@armru i've reviewed your PR and thanks for adding additional tests. i've added pooler e2e test.
ps: sorry for the delay, i was busy with my other works :)

@bozkayasalihx
Copy link
Contributor Author

@armru Did you have a chance to look at the commit I sent?

@mnencia
Copy link
Member

mnencia commented Mar 10, 2026

@bozkayasalihx I pushed a few review commits on top of your work. The main changes are: added DNS name validation to the CRD fields, restructured e2e tests into unit tests keeping only slim smoke tests in e2e, and added a doc note about RBAC accumulation with shared ServiceAccounts. I also created #10276 to track the feature.

@mnencia mnencia force-pushed the feat/shared-service-account branch from 7c1fb34 to f725e65 Compare March 10, 2026 16:41
@mnencia
Copy link
Member

mnencia commented Mar 10, 2026

/test

@github-actions
Copy link
Contributor

@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/22913622673

bozkayasalihx and others added 20 commits March 11, 2026 09:40
Add optional ServiceAccountName field to ClusterSpec and PoolerSpec
to allow users to specify an existing ServiceAccount instead of having
the operator create a new one.

This enables sharing a single ServiceAccount across multiple clusters,
which is particularly useful for cloud IAM configurations where multiple
clusters need the same cloud resource access permissions.

The field is mutually exclusive with ServiceAccountTemplate. If not
specified, the operator will continue to create a ServiceAccount with
the cluster/pooler name as before.

Signed-off-by: Salih Bozkaya <[email protected]>
Add support for specifying existing ServiceAccounts for both Cluster
and Pooler resources, enabling multiple resources to share the same
ServiceAccount for cloud IAM configurations.

Signed-off-by: Salih Bozkaya <[email protected]>
Add comprehensive unit tests for the ServiceAccountName functionality
in both Cluster and Pooler resources.

Signed-off-by: Salih Bozkaya <[email protected]>
Add end-to-end tests to verify shared ServiceAccount functionality
across multiple clusters and error handling.

Test scenarios:
- Multiple clusters sharing a single ServiceAccount
- Verify pods use the shared ServiceAccount correctly
- Validate no auto-created ServiceAccount when custom name is provided
- Confirm error handling when specified ServiceAccount doesn't exist

Includes fixture files demonstrating real-world cloud IAM use case
with AWS IRSA annotation example.

Signed-off-by: Salih Bozkaya <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
…ability

Change Pooler's ServiceAccountName from *string to string to match the
Cluster API convention, eliminating inconsistent nil-checking patterns
and unnecessary pointer deepcopy logic. Add CEL immutability validation
(self == oldSelf) to prevent changes to serviceAccountName after
creation, consistent with the Cluster field behavior.

Also adds documentation for Pooler shared ServiceAccount usage in the
security docs

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Assisted-by: Claude Opus 4.6
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Add RoleBinding subjects verification to rolebinding_test.go and
pgbouncer/rbac_test.go, cluster controller RoleBinding test with
custom SA, and pooler controller tests for validateExistingServiceAccount
and updateRBAC with custom ServiceAccount.

Assisted-by: Claude Opus 4.6
Signed-off-by: Marco Nenciarini <[email protected]>
Replace 4 e2e test scenarios per resource type with a single smoke
test each that verifies the end-to-end happy path: create shared SA,
create cluster/pooler referencing it, verify pods use it, delete the
resource, and confirm the shared SA is preserved. Detailed assertions
(RoleBinding subjects, non-existent SA handling, mixed mode) are now
covered by unit tests.

Assisted-by: Claude Opus 4.6
Signed-off-by: Marco Nenciarini <[email protected]>
Add Pattern and MaxLength kubebuilder markers to ensure the
serviceAccountName field conforms to Kubernetes DNS subdomain
naming rules. Clarify in the field description that the
ServiceAccount must be in the same namespace.

Assisted-by: Claude Opus 4.6
Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia mnencia force-pushed the feat/shared-service-account branch from f725e65 to 6882561 Compare March 11, 2026 08:40
@leonardoce leonardoce added the do not backport This PR must not be backported - it will be in the next minor release label Mar 11, 2026
@leonardoce leonardoce merged commit 6f88e7d into cloudnative-pg:main Mar 11, 2026
35 checks passed
@bozkayasalihx
Copy link
Contributor Author

@bozkayasalihx I pushed a few review commits on top of your work. The main changes are: added DNS name validation to the CRD fields, restructured e2e tests into unit tests keeping only slim smoke tests in e2e, and added a doc note about RBAC accumulation with shared ServiceAccounts. I also created #10276 to track the feature.

LGTM!, Thanks

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

Labels

do not backport This PR must not be backported - it will be in the next minor release enhancement 🪄 New feature or request lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support shared ServiceAccount across clusters and poolers

6 participants