kubernetes: API server reflector may lose events if TransformFromStorage fails
What happened?
Hi š ,
Iām investigating an issue where create or update operations on Secrets are randomly not reflected in the controller runtime client cache. We have observed this behaviour multiple times, in at least 3 different operators, all relying on the controller-runtime client, and one of them being the External Secrets Operator. As a result the controllers indefinitely attempt to reconcile the missed Secrets from their stale cache, perpetually getting 409 errors until they are manually restarted. These Secrets seem to be lost by the Watch mechanism, while they are successfully created (as confirmed by audit logs), and eventually available in a List result.
After some investigations I realized that when this problem occur we also have transient errors related to Secret decryption, for example:
watcher.go:471] failed to prepare current and previous objects: failed to decrypt DEK, error: rpc error: code = Canceled desc = grpc: the client connection is closing
reflector.go:467] storage/cacher.go:/secrets: retrying watch of *core.Secret internal error: Internal error occurred: failed to decrypt DEK, error: rpc error: code = Canceled desc = grpc: the client connection is closing
I have tried to understand how events are processed in storage/etcd3/watcher.go, more specifically here:
IIUC when an error occurs, an āerror eventā is sent back to the reflector through a channel:
But IIUC we keep processing other incoming events? This leads me to wonder if there is not a situation where:
- etcd watcher temporary fails to decrypt a Secret with
resourceVersion=V, the error is sent to the reflector:
- Assuming that
case apierrors.IsInternalError(err) && retry.ShouldRetry()is true the reflector is going to run thewatchHandleragain:
- Meanwhile, letās suppose that the etcd watcher managed to decrypt another
Secret, with a different name and withresourceVersion=V+1, the result is sent to the watcher which callssetLastSyncResourceVersionwithV+1as a value here:
Which feels wrong since Secret with resourceVersion V has not been observed yet?
I would like to know if this sounds like a possible scenario that may explain what we are observing. If not please let me know if you have any ideas that might explain it. Thanks!
We have observed this issue on K8s 1.30 (v1.30.10-eks-bc803b4) with the following libraries:
- k8s.io/api v0.32.2
- k8s.io/apimachinery v0.32.2
- k8s.io/client-go v0.32.2
- sigs.k8s.io/controller-runtime v0.20.2
What did you expect to happen?
I think I would expect the Secrets which suffered what could be a temporary decryption failure to be eventually reflected in the cache.
How can we reproduce it (as minimally and precisely as possible)?
Unfortunately, this happens randomly in our case š That being said if I run the API Server in a dev environment and temporarily return an error in the TransformFromStorage function for a specific Secret, I can observe that this Secret is never going to be sent to a client while other Secrets created at the same time are correctly reflected in the controller-runtime client.
Anything else we need to know?
This part of the code is (really) new to me but Iām happy to help, so please let me know if there is anything else I should investigate!
Kubernetes version
$ kubectl version
Server Version: v1.30.10-eks-bc803b4
Cloud provider
OS version
No response
Install tools
Container runtime (CRI) and version (if applicable)
Related plugins (CNI, CSI, ā¦) and versions (if applicable)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 22 (13 by maintainers)
@barkbay this is a great finding, hereās a reproduction of the issue on an HA cluster:
Watch events for a client against apiserver1
Watch events for a client against apiserver2
In this example, the client for apiserver1 misses the create event for demo-secret2. It will not pick up the missed event on a new watch request because it received a subsequent event for the delete secret1 event. It seems like the desired behavior is that all watchers on the given resource should be closed if it TransformFromStorage fails.
The probability of this occurring increases during apiserver process terminations in an HA setup. This can occur after launching new apiservers + terminating the old apiservers because the apiserver KMS grpc client shuts down before watches are closed (even if the KMS provider itself isnāt shutdown):
Without setting shutdown-watch-termination-grace-period, it looks like the grpc client will close after around 10-20s whereas watches are allows to stay open for 1 minute after the shutdown sequence. In addition to the underlying issue with missing watch events, a mitigation to this issue would be to ensure the apiserver KMS grpc client stays healthy until all watchers are closed.
Thanks for the repro @natherz97.
This issue has the potential for data loss in API server watch cache and clients downstream. Given this could happen even outside of the shutdown case, we need a robust way to address the risk in the watcher itself (same way we do not allow continuing on a compaction error today).
Besides the issue @liggitt pointed above on differentiating transient (e.g KMS unavailable) and non-transient (e.g bit flip) decode errors, thereās another serious issue here iiuc - watch events received from etcd may not be delivered to watch-cache in serial order when thereās an error in event processing. While processEvents is calling transform on events serially, any error while transforming is handled by sendError that writes to a buffered errChan that is later read/processed async by different goroutine. While we close the resultChan eventually after translating the error to a an event and writing to that channel, subsequent events may have already been written. I think we should stop any further writes to resultChan once sendError is called (except for the error event itself).
Thanks - so it seems this is exactly the thing that Shyam described above.
And this is exactly what my WIP PR is trying to fix: https://github.com/kubernetes/kubernetes/pull/131020
@liggitt - FYI, this isnāt watchcache-related - this is a bug in k8s-etcd-client layer, so youāre exposed to it even with watchcache disabled