Skip to content

[Bug]: High memory usage/OOMKill of operator if there are many large secrets in the namespace #10924

Open

Description

@bergdavi

Is there an existing issue already for this bug?

  • I have searched for an existing issue, and could not find anything. I believe this is a new bug.

I have read the troubleshooting guide

  • I have read the troubleshooting guide and I think this is a new bug.

I am running a supported version of CloudNativePG

  • I have read the troubleshooting guide and I think this is a new bug.

Contact Details

[email protected]

Version

1.29 (latest patch)

What version of Kubernetes are you using?

1.35

What is your Kubernetes environment?

Self-managed: kind (evaluation)

How did you install the operator?

YAML manifest

What happened?

Issue

When there are many large secrets in a namespace that the CNPG operator watches the memory usage of the operator becomes high, and eventually reaches the configured limit and gets OOMKilled.

This can realistically happen if there is a long helm release history of large charts in the namespace (e.g. 400 chart versions stored). It could probably happen with other kind of resources that the operator watches, but helm secrets is a typical scenario of many large resources.

Steps to reproduce

  1. Install CNPG operator based on the quick start guide (operand not needed)
kubectl apply --server-side -f \
  https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.29/releases/cnpg-1.29.1.yaml
  1. Check operator memory usage
kubectl top -n cnpg-system pod
NAME                                       CPU(cores)   MEMORY(bytes)
cnpg-controller-manager-7d9fd94fb7-xtjfc   2m           15Mi
  1. Create many large secrets
#!/bin/bash
# Create 200 large fake helm release secrets (~100KB each)
# Helm release secrets have label "owner=helm" and type "helm.sh/release.v1"

NAMESPACE="cnpg-system"
LARGE_DATA=$(head -c 100000 /dev/urandom | base64 | tr -d '\n')

for i in $(seq 1 200); do
  kubectl -n "$NAMESPACE" create secret generic "sh.helm.release.v1.fake-release.v${i}" \
    --from-literal=release="$LARGE_DATA" \
    --dry-run=client -o yaml | \
    kubectl label --local -f - owner=helm app.kubernetes.io/managed-by=Helm --dry-run=client -o yaml | \
    kubectl apply -f - > /dev/null 2>&1
  if [ $((i % 50)) -eq 0 ]; then
    echo "Created $i secrets..."
  fi
done
echo "Done creating 200 helm release secrets"
  1. Check operator memory usage
kubectl top -n cnpg-system pod
NAME                                       CPU(cores)   MEMORY(bytes)
cnpg-controller-manager-7d9fd94fb7-xtjfc   6m           109Mi

Creating 500 secrets with the above script causes OOMKill with the default operator memory limit (200Mi), and the operator will go into a crash loop.

Likely root cause

Controller runtime cache is configured for the watched namespaces: multinamespaced_cache.go.

Once a resource kind is accessed by the operator (e.g. in this case a Secret) controller runtime will cache all resources of that kind in the namespace (e.g. in this case all Secrets, including unrelated ones from Helm), eating up the memory

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    triage
    on Jun 16, 2026
  2. dosubot commented on Jun 16, 2026

    @dosubot

    Hi @bergdavi! I'm Dosu and I'm helping the CloudNativePG team.

    This is a valid issue. Your root cause analysis is correct — the operator's main controller manager caches all Secrets in watched namespaces because multiple controllers (ClusterReconciler, PoolerReconciler, DatabaseRoleReconciler, PluginReconciler) use .Watches(&corev1.Secret{}, ...) [1] [2] [3] [4]. While these controllers use predicates to filter which Secret events trigger reconciliation, the underlying informer still caches every Secret in the namespace regardless of predicates [5].

    A few relevant details:

    • The instance manager already uses DisableFor to exclude Secrets from its cache [6], but the main operator manager does not have equivalent filtering.
    • The multinamespaced cache builder (DelegatingMultiNamespacedCacheBuilder) currently creates caches without label or field selectors for Secrets [7], so there's no mechanism today to exclude unrelated Secrets at the cache level.
    • There is ongoing work in PR #9412 to replace the custom multinamespace watcher with controller-runtime's built-in cache.Options.DefaultNamespaces, which could open the door to using cache.ByObject with label/field selectors to filter Secrets more granularly.

    A potential fix could involve configuring cache.ByObject for Secrets with a label selector (e.g., only cache Secrets owned by CNPG or with specific labels), though this would need careful consideration to ensure all referenced Secrets (like user-provided credential Secrets) are still accessible — possibly via direct API reads for non-cached Secrets.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about cloudnative-pg Share Dosu with your team

  3. added
    bug 🐛
    and removed on Jun 21, 2026
  4. Utkarsh-sharma47 commented on Jun 24, 2026

    @Utkarsh-sharma47
    Contributor

    Hi @bergdavi,

    I'd like to investigate this issue if nobody is already working on it.

    I'll first look into the controller-runtime cache configuration and the operator's Secret watches to understand the reported memory growth before proposing a fix.

    Thanks!

  5. bergdavi commented on Jun 25, 2026

    @bergdavi
    Author

    Hi @Utkarsh-sharma47,

    We had a brief discussion on this during the last office hours meeting. We might be able to solve this by modifying the caching for secrets via controller-runtime ByObject and TransformFunc to only cache metadata for secrets instead of the whole object. It would mean that if we want to read a secret we need to go to the API server instead of the cache, but that might be an acceptable tradeoff.

    I'm planning to do a PoC on the above during this week, and I'll share the results here.

    If you have some other idea on how this could be fixed you're very welcome to investigate it :)

  6. Utkarsh-sharma47 commented on Jun 26, 2026

    @Utkarsh-sharma47
    Contributor

    Thanks for the update, @bergdavi.

    That sounds like an interesting approach. I'll wait to see the results of your PoC before digging further into the implementation.

    If I come across a different approach or anything that might complement your findings, I'll share it here.

    Looking forward to the update!

  7. bergdavi commented on Jun 29, 2026

    @bergdavi
    Author

    @Utkarsh-sharma47 I've created a PoC here: #11047. Based on my testing it does solve the high memory issue.

    The code is not very clean, and it has some issues:

    • It would conflict with an ongoing refactor in refactor(multicache): use controller-runtime DefaultNamespaces #9412 (that can be probably resolved)
    • It makes the operator more sensitive to K8S API server issues (previously since all secret reads were from the cache if the API server was down when a read happened it was served from the cache, now it would give an error), I need to check how serious of an impact this is
      • This can probably be improved to still cache secrets with CNPG labels
    • Not sure if it's a good pattern to handle this fallback to the API server during reads in the Get function of the cache, it might be cleaner to replace the Get calls in the code with direct API access, I'm also still thinking about it
  8. Utkarsh-sharma47 commented on Jun 30, 2026

    @Utkarsh-sharma47
    Contributor

    Thanks for sharing the PoC. Looking through the implementation, it seems the main tradeoff is replacing cached Secret reads with direct API reads via apiReader.

    Would it be feasible to cache metadata-only for all Secrets but retain full caching for Secrets managed by CNPG (or matching specific labels), so that frequently accessed CNPG Secrets remain available during temporary API-server issues while still avoiding the memory impact from unrelated Helm release Secrets?

  9. github-actions commented on Oct 12, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 60 days with no activity.

  10. wizardist commented on Oct 26, 2026

    @wizardist

    The problem is not Secrets though. And filtering Helm installation secrets is a legit work-around, but it's very narrow.

    The case we've run into is we have clusters with tens of namespaces, and maybe a couple might have a Cluster. And other namespaces may have thousands of objects without a single Cluster.

    Today it's possible to whitelist namespaces by setting WATCH_NAMESPACE. It works well for multi-tenant setups with low trust. For us a label on a namespace would work better. I think I should file this as a feature request to discuss.

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

Metadata

Metadata

Assignees

Labels

bug 🐛triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions