Repository navigation
Description
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
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
- 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
- Check operator memory usage
kubectl top -n cnpg-system pod
NAME CPU(cores) MEMORY(bytes)
cnpg-controller-manager-7d9fd94fb7-xtjfc 2m 15Mi
- 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"
- 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
dosubot commentedon Jun 16, 2026
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:
DisableForto exclude Secrets from its cache [6], but the main operator manager does not have equivalent filtering.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.cache.Options.DefaultNamespaces, which could open the door to usingcache.ByObjectwith label/field selectors to filter Secrets more granularly.A potential fix could involve configuring
cache.ByObjectfor 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.
Utkarsh-sharma47 commentedon Jun 24, 2026
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!
bergdavi commentedon Jun 25, 2026
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 :)
Utkarsh-sharma47 commentedon Jun 26, 2026
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!
bergdavi commentedon Jun 29, 2026
@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:
Utkarsh-sharma47 commentedon Jun 30, 2026
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?
fix: stop caching Helm release secrets in the operator (#11211)
fix: stop caching Helm release secrets in the operator (#11211)
fix: stop caching Helm release secrets in the operator (#11211)
github-actions commentedon Oct 12, 2026
This issue is stale because it has been open for 60 days with no activity.
wizardist commentedon Oct 26, 2026
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.