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:

https://github.com/kubernetes/kubernetes/blob/6a074997c960757de911780f250ecd9931917366/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go#L427-L437

IIUC when an error occurs, an ā€œerror eventā€ is sent back to the reflector through a channel:

https://github.com/kubernetes/kubernetes/blob/6a074997c960757de911780f250ecd9931917366/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go#L469-L474

But IIUC we keep processing other incoming events? This leads me to wonder if there is not a situation where:

  1. etcd watcher temporary fails to decrypt a Secret with resourceVersion=V, the error is sent to the reflector:

https://github.com/kubernetes/kubernetes/blob/6a074997c960757de911780f250ecd9931917366/staging/src/k8s.io/client-go/tools/cache/reflector.go#L737-L743

  1. Assuming that case apierrors.IsInternalError(err) && retry.ShouldRetry() is true the reflector is going to run the watchHandler again:

https://github.com/kubernetes/kubernetes/blob/6a074997c960757de911780f250ecd9931917366/staging/src/k8s.io/client-go/tools/cache/reflector.go#L466-L468

  1. Meanwhile, let’s suppose that the etcd watcher managed to decrypt another Secret, with a different name and with resourceVersion=V+1, the result is sent to the watcher which calls setLastSyncResourceVersion with V+1 as a value here:

https://github.com/kubernetes/kubernetes/blob/6a074997c960757de911780f250ecd9931917366/staging/src/k8s.io/client-go/tools/cache/reflector.go#L791

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

* EKS (eks.23 / K8s 1.30)

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)

Most upvoted comments

@barkbay this is a great finding, here’s a reproduction of the issue on an HA cluster:

  1. Issue a watch to apiserver1 on secrets.
  2. Make the KMS plugin unhealthy for apiserver1 to prevent it from decrypting and caching any new DEK seeds (seeds that it hasn’t seen prior to this point or any newly generated seed by a peer apiserver).
  3. Restart apiserver2 to make it generate a new DEK seed.
  4. Issue a watch against apiserver2 on secrets.
  5. Create a new secret against apiserver2 (in the example below this is demo-secret2 which was written with a DEK seed that apiserver1 doesn’t have cached) and delete an existing secret against apiserver2 (in the example below this is demo-secret1 which apiserver1 does have cached).

Watch events for a client against apiserver1

% kubectl get --raw "/api/v1/namespaces/default/secrets?watch=true"
...
{"type":"ADDED","object":{"kind":"Secret","apiVersion":"v1","metadata":{"name":"demo-secret1","namespace":"default","uid":"1652748e-a307-4cea-a3b2-aa7282c95667","resourceVersion":"7434736","creationTimestamp":"2025-03-20T19:31:56Z","managedFields":[{"manager":"kubectl-create","operation":"Update","apiVersion":"v1","time":"2025-03-20T19:31:56Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:dbPassword":{},"f:dbUser":{}},"f:type":{}}}]},"data":{"dbPassword":"cDRzc3cwcmQ=","dbUser":"cG9zdGdyZXM="},"type":"Opaque"}}
{"type":"DELETED","object":{"kind":"Secret","apiVersion":"v1","metadata":{"name":"demo-secret1","namespace":"default","uid":"1652748e-a307-4cea-a3b2-aa7282c95667","resourceVersion":"7435455","creationTimestamp":"2025-03-20T19:31:56Z","managedFields":[{"manager":"kubectl-create","operation":"Update","apiVersion":"v1","time":"2025-03-20T19:31:56Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:dbPassword":{},"f:dbUser":{}},"f:type":{}}}]},"data":{"dbPassword":"cDRzc3cwcmQ=","dbUser":"cG9zdGdyZXM="},"type":"Opaque"}}

Watch events for a client against apiserver2

% kubectl get --raw "/api/v1/namespaces/default/secrets?watch=true"
...
{"type":"ADDED","object":{"kind":"Secret","apiVersion":"v1","metadata":{"name":"demo-secret1","namespace":"default","uid":"1652748e-a307-4cea-a3b2-aa7282c95667","resourceVersion":"7434736","creationTimestamp":"2025-03-20T19:31:56Z","managedFields":[{"manager":"kubectl-create","operation":"Update","apiVersion":"v1","time":"2025-03-20T19:31:56Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:dbPassword":{},"f:dbUser":{}},"f:type":{}}}]},"data":{"dbPassword":"cDRzc3cwcmQ=","dbUser":"cG9zdGdyZXM="},"type":"Opaque"}}
{"type":"ADDED","object":{"kind":"Secret","apiVersion":"v1","metadata":{"name":"demo-secret2","namespace":"default","uid":"29efa04f-2df0-4edb-9a07-124201c37604","resourceVersion":"7435425","creationTimestamp":"2025-03-20T19:35:58Z","managedFields":[{"manager":"kubectl-create","operation":"Update","apiVersion":"v1","time":"2025-03-20T19:35:58Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:dbPassword":{},"f:dbUser":{}},"f:type":{}}}]},"data":{"dbPassword":"cDRzc3cwcmQ=","dbUser":"cG9zdGdyZXM="},"type":"Opaque"}}
{"type":"DELETED","object":{"kind":"Secret","apiVersion":"v1","metadata":{"name":"demo-secret1","namespace":"default","uid":"1652748e-a307-4cea-a3b2-aa7282c95667","resourceVersion":"7435455","creationTimestamp":"2025-03-20T19:31:56Z","managedFields":[{"manager":"kubectl-create","operation":"Update","apiVersion":"v1","time":"2025-03-20T19:31:56Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:dbPassword":{},"f:dbUser":{}},"f:type":{}}}]},"data":{"dbPassword":"cDRzc3cwcmQ=","dbUser":"cG9zdGdyZXM="},"type":"Opaque"}}

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):

E0320 20:46:51.747007      12 transformer.go:164] "failed to decrypt data" err="failed to decrypt DEK, error: rpc error: code = Canceled desc = grpc: the client connection is closing"
E0320 20:46:51.747169      12 watcher.go:567] failed to prepare current and previous objects: failed to decrypt DEK, error: rpc error: code = Canceled desc = grpc: the client connection is closing
I0320 20:46:51.747281      12 reflector.go:489] 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

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