Repository navigation
feat: add WithSecretsFieldSelector to filter the core cache's Secrets - #596
Merged
Merged
Conversation
## Summary
`KubeCoreCache` lists and watches every Secret in the namespaces it watches. Clusters often hold many Secrets the consumer never reads, such as Helm release storage (`helm.sh/release.v1`) and ServiceAccount tokens. The API server still sends all of them, and the cache keeps each one in memory for the life of the process.
This adds `WithSecretsFieldSelector`, an option for `NewKubeCoreCacheWithOptions`:
cache.NewKubeCoreCacheWithOptions(ctx, clientset, resync, namespaces,
cache.WithSecretsFieldSelector(fields.AndSelectors(
fields.OneTermNotEqualSelector("type", "helm.sh/release.v1"),
fields.OneTermNotEqualSelector("type", "kubernetes.io/service-account-token"),
)))
The selector is added to every LIST and WATCH request the cache makes for Secrets. The API server applies it, so excluded Secrets are never sent or stored.
## Motivation
Gloo Edge uses `KubeCoreCache`. It downloads and holds every Secret.
## Behavior
- **Compatibility:** `NewKubeCoreCacheWithOptions` gains a variadic `opts ...Option` parameter, so existing calls compile unchanged.
- **Default:** without the option, or with a nil or empty selector, the cache stores every Secret, as before.
- **Scope:** Secrets only. Pods, Services, ConfigMaps and Namespaces are unaffected.
- **Effect on the kube secret client:** the secret listers, and the kube secret client's `List` and `Watch`, skip excluded Secrets. `Read` fetches a Secret by name from the API server, so it still returns them.
- **Supported fields:** Secrets can only be selected by `metadata.name`, `metadata.namespace` and `type`. Any other field is rejected by the API server, which leaves the cache unable to sync.
## Testing
- **New unit tests:** `pkg/api/v1/clients/kube/cache/secrets_field_selector_test.go` uses a fake clientset to record the namespace and field selector of every list and watch request. It covers:
- no option, a nil selector and an empty selector
- one term and two terms
- the last of two options winning
- a cache watching two namespaces, which sends the selector in both
- no other resource getting a selector
- **Both sync paths:** the fake can't serve watch-list streams, so the test rejects them and client-go falls back to LIST and WATCH. The selector is checked on all three kinds of request.
- **Race detector and feature gate:** the tests pass with `-race`, with `KUBE_FEATURE_WatchListClient` both on and off.
- **Mutation check:** with the filter removed, the selector cases fail.
- **Downstream:** gloo's callers of `NewKubeCoreCacheWithOptions` compile against this branch.
## Changelog
`changelog/v0.41.3/secrets-field-selector.yaml` (NEW_FEATURE). The change is additive and opt-in, so it ships as a patch.
Signed-off-by: David L. Chandler <david.chandler@solo.io>
|
Issues linked to changelog: |
andy-fong
reviewed
Nov 6, 2026
Signed-off-by: David L. Chandler <david.chandler@solo.io>
|
Issues linked to changelog: |
andy-fong
approved these changes
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KubeCoreCachelists and watches every Secret in the namespaces it watches. Clusters often hold many Secrets the consumer never reads, such as Helm release storage (helm.sh/release.v1) and ServiceAccount tokens. The API server still sends all of them, and the cache keeps each one in memory for the life of the process.This adds
WithSecretsFieldSelector, an option forNewKubeCoreCacheWithOptions:The selector is added to every LIST and WATCH request the cache makes for Secrets. The API server applies it, so excluded Secrets are never sent or stored.
Motivation
Gloo Edge uses
KubeCoreCache. It downloads and holds every Secret.Behavior
NewKubeCoreCacheWithOptionsgains a variadicopts ...Optionparameter, so existing calls compile unchanged.ListandWatch, skip excluded Secrets.Readfetches a Secret by name from the API server, so it still returns them.metadata.name,metadata.namespaceandtype. Any other field is rejected by the API server, which leaves the cache unable to sync.Testing
pkg/api/v1/clients/kube/cache/secrets_field_selector_test.gouses a fake clientset to record the namespace and field selector of every list and watch request. It covers:-race, withKUBE_FEATURE_WatchListClientboth on and off.NewKubeCoreCacheWithOptionscompile against this branch.