Skip to content

feat: CNPG deployed namespaced - #9350

Closed
maxlengdell wants to merge 16 commits into
cloudnative-pg:mainfrom
maxlengdell:dev/9346
Closed

maxlengdell wants to merge 16 commits into
cloudnative-pg:mainfrom
maxlengdell:dev/9346

Conversation

@maxlengdell

@maxlengdell maxlengdell commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor

CNPG currently relies on the access to cluster nodes. Such ClusterRole should be limited and avoided to harden the CNPG operator deployment. CNPG still relies on webhooks, but that will be addressed in a separate feature request.

With the namespaced deployment of CNPG, the early switchover during a node drain will not be possible. Meaning if someone does a node drain where the primary is deployed, the primary will become unavailable and a replica will be promoted. This is slower than the automatically initiated promotion, but can be mitigated with PodDistruptionBudget settings and topology deployment constraints. So it would be a reasonable trade-off, which should be documented.

Feature request: #9346

Changes

Only watch Nodes and ClusterImageCatalog when deployed with namespaced disabled (default)

Signed-off-by: Max Lengdell [email protected]

@maxlengdell
maxlengdell requested a review from a team December 2, 2025 11:23
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Dec 2, 2025
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 labels Dec 2, 2025
@github-actions

github-actions Bot commented Dec 2, 2025

Copy link
Copy Markdown
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 the enhancement 🪄 New feature or request label Dec 2, 2025
Comment thread internal/configuration/configuration.go Resolved
@maxlengdell
maxlengdell force-pushed the dev/9346 branch 3 times, most recently from 9dcc79d to 99a36c6 Compare December 8, 2025 18:42
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Dec 8, 2025
@maxlengdell
maxlengdell force-pushed the dev/9346 branch 2 times, most recently from 68c886a to fe6e973 Compare December 11, 2025 13:49
@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 11, 2025
@maxlengdell
maxlengdell force-pushed the dev/9346 branch 5 times, most recently from d9ff042 to 3f46d72 Compare December 16, 2025 12:56
@maxlengdell

Copy link
Copy Markdown
Contributor Author

Sorry for the force push. Had issues with my gpg key. Should be sorted now. Will resolve merge conflicts once reviews are made to not cause further messy branch

@jsilvela jsilvela left a comment

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.

It's good overall, but I've left a bunch of comments, mostly on documentation and the use of Context within the Ginkgo tests.

Comment thread docs/src/operator_conf.md Resolved
Comment thread docs/src/operator_conf.md Resolved
Comment thread docs/src/operator_conf.md Resolved
Comment thread docs/src/operator_conf.md Resolved
Comment thread docs/src/operator_conf.md Resolved
cluster.Status.TargetPrimary == cluster.Status.CurrentPrimary {
isPrimaryOnUnschedulableNode, err := r.isNodeUnschedulableOrBeingDrained(ctx, primary.Node)
if err != nil {
contextLogger.Error(err, "while checking if current primary is on an unschedulable node")

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.

It was already complex when you came here, but now it's a 3-level conditional.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, would it make more sense to break out everything in step 1?

	// First step: check if the current primary is running in an unschedulable node
	// and issue a switchover if that's the case
	// Can not check node settings in namespaced deployment
	if !configuration.Current.Namespaced {
      checkSchedulableNodes()
    }

Comment thread tests/e2e/namespaced_deployment_test.go Resolved
Comment thread tests/e2e/namespaced_deployment_test.go Resolved
Comment thread tests/utils/namespaced/doc.go Resolved
@dosubot dosubot Bot removed the lgtm This PR has been approved by a maintainer label Feb 17, 2026
@jsilvela

Copy link
Copy Markdown
Contributor

Sorry @maxlengdell , I see that dosu removed the "lgtm" label because of my request for changes.
I have to say, dosu has no backbone.

@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Feb 20, 2026
    Fixing minor test changes and documentation updates
    One minor change left is to address the comments in
    cluster_status_test.go

Signed-off-by: Max Lengdell <[email protected]>
Fix comments so cluster_status_test getPodsTopology is in its own
describe bracket

Signed-off-by: Max Lengdell <[email protected]>

@jsilvela jsilvela left a comment •

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.

One optional comment on the use of context in the E2E.
Another one, a suggestion but not a blocker, is to add some content in the document docs/src/e2e.md. Flaunt your E2E!
EDIT: come to think of it, you should also strongly consider adding some text in the security.md file.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Feb 25, 2026
Fix the context comment in the namespaced test.
Add some description to the cluster-role dependencies when namespaced
mode is enabled.

Signed-off-by: Max Lengdell <[email protected]>
Comment thread docs/src/security.md
#### Why Are ClusterRole Permissions Needed?

The operator currently requires `ClusterRole` permissions to read `nodes` and
In cluster-wide mode, the operator requires `ClusterRole` permissions to read `nodes` and

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.

Worth mentioning that cluster-wide is the default mode.

@sxd

sxd commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

There's a couple of things that I've notice here while reviewing this PR and also I would like to give my opinion on the overall related to this:

The operator currently does have the capability to watch over one namespace, that it could be his own namespace, when you set the OPERATOR_NAMESPACE and WATCH_NAMESPACE to the same namespace, that's something that it's already capable of doing, so this is not adding a "deployed namespaced" capability, because by definition, that can be done already, it's not impossible to do it. What this patch it's doing is adding the capability of running without the Node and ClusterImageCatalog permissions to the operator when some conditions are met, in this case OPERATOR_NAMESPACE and WATCH_NAMESPACE need to be the same string.

I'm not against the patch but more against the intention and implementation, I think that:

  • This patch shouldn't be about "deployed namespaced" but about "allowing run without permissions"
  • We're over-complicating by requesting a set of condition that are actually valid, I may want to have the same OPERATOR_NAMESPACE and WATCH_NAMESPACE but able to check on the nodes and with access to the ClusterImageCatalog, that's a valid configuration
  • This should be an option in the operator that says something like "disable cluster wide object" or something like that, obviously the name need to be discussed but yeah, that's the point

The final intention here is to allow the operator to run without access to global objects inside a Kubernetes cluster, that it's half managed here, and should be a PR explicitly to that, one for Node access and another for ClusterImageCatalog, that will split the work and also make it easy to review.

The e2e tests here are amazing and I think it should be preserved but distributed in a new work that will allow having these two options.

The deployment of the operator it's something that must and can be managed externally with other tools like ArgoCD or FluxCD or Helm, the permission for the ServiceAccount used by the operator, along side with the roles, should be managed there, the operator will be configure to run without access to those permissions.

In general, I think we should proceed with a work related to allow the operator to run without these privileges as an option (or self detect that, which can be more difficult but not impossible) and not depend on a configuration that it will be valid for some environments

Those are my two cents.

@maxlengdell

Copy link
Copy Markdown
Contributor Author

Hi @sxd, I see your point on that "namespaced deployment" already exist. The PR was probably misdirected. Indeed the only goal for this PR is to make the operator work without dependency on rbac to nodes or ClusterImageCatalog. Agree with your points in the rest of the comment as well.

I'll create 3 new pull requests:

  1. Make CNPG operator run and work without crashing when the SA does not have access to the nodes. To start; I'll make it configurable by a parameter, after that maybe we can find a auto-detect logic. To be discussed in that separate PR.
  2. Make CNPG operator run and work without crashing when the SA does not have access to the ClusterImageCatalog. Same assumption here, making it configurable. I'll do my best to come up with suitable names (the hardest part)

Glad you liked the e2e tests. I'll port those to the separate PRs. This PR can stay open to preserve the state for now and once the new PRs are open we can take it from there if that's ok 😃

I'll update the feature request so that "namespaced deployment" isnt used, since indeed that's already existing. Thanks for the review!

@maxlengdell

maxlengdell commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor Author

But just to be clear; there's nothing in the code which prevents setting OPERATOR_NAMESPACE and WATCH_NAMESPACE to the same. That would mean the operator still checks nodes and clusterImageCatalog. It was proposed by @mnencia to have the validation of these two fields in some meeting in December on this topic. Only when setting NAMESPACED to true will make the operator not watch the cluster wide resources.
So reading your feedback again, its more about the documentation aspect of this PR? I can update the description, labels, documentation as needed. Apart from the framing of the PR, I see no feedback on the src code itself. Is that correct?

@ardentperf

Copy link
Copy Markdown
Contributor

The final intention here is to allow the operator to run without access to global objects inside a Kubernetes cluster, that it's half managed here, and should be a PR explicitly to that, one for Node access and another for ClusterImageCatalog, that will split the work and also make it easy to review.

I think it's worth pointing out that there's no way around needing to manage CRDs as global objects. So we can never eliminate all global requirements. This seems more to me about "what are the minimum global requirements we can get away with" - and will users fully understand the implications of disabling other global requirements (webhooks/certs, node access, clusterimagecatalog)

@maxlengdell

Copy link
Copy Markdown
Contributor Author

Indeed CRDs are still cluster resources, and there's no way around that at the moment.
For CNPG it would be nice to provide a functionality where the deployment engineer doesnt need admin rights. We can unfortunately not have all engineers with such rights. I think the limitations of not having access to Nodes and ClusterImageCatalog shall be described well so that integrators of CNPG can make a conscious decision. Hopefully I did that in this commit and the other node one

@mnencia

mnencia commented Jun 23, 2026

Copy link
Copy Markdown
Member

Closing this in favor of #10135, which carries forward the Node-watching work (and the runtime RBAC-splitting package) split out of here, as we discussed above. The "namespaced" framing is being dropped since namespaced operation already exists through OPERATOR_NAMESPACE/WATCH_NAMESPACE; the remaining piece, running without ClusterImageCatalog access, can come as its own opt-in PR.

I know you'd asked to keep this open as a reference until the split PRs landed. It's been a few months and the branch has since drifted into merge conflicts, so I think it's cleaner to close it and track the work in #10135. The diff and the e2e tests stay fully visible here for the ClusterImageCatalog follow-up. Thanks for the work, the e2e coverage especially.

@mnencia mnencia closed this Jun 23, 2026
@github-actions

Copy link
Copy Markdown
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

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

Labels

backport-requested ◀️ This pull request should be backported to all supported releases enhancement 🪄 New feature or request lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged release-1.28 size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants