apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: kubernetes discovery lost one service's endpoints after a watch failure and did not recover until restart

Open sawyerward opened this issue 1 month ago • 1 comments

Current Behavior

One of three APISIX replicas began returning 503 for one service after its Endpoints watch failed during a Kubernetes API server restart. It kept doing so for about 3.5 hours, until we restarted the pod. The service's pods were healthy the whole time, and the other two replicas served it normally. At least one other kubernetes-discovery service on the same replica kept working.

We haven't confirmed the cause, but this code in apisix/discovery/kubernetes/informer_factory.lua looks related. list() fetches later pages with a recursive call and ignores its return value:

informer.continue = data.metadata.continue
if informer.continue and informer.continue ~= "" then
    if informer.stop then
        return true
    end
    list(httpc, apiserver, informer)
end

return true

If a later page fails, list() would still return true, and post_list() would then delete the endpoints from the pages that never loaded. The retry was due about 40 seconds after the watch failure, at about 21:40:57 UTC. The API servers were returning etcd errors from 21:39:48 to 21:41:18, and etcd had no leader from 21:40:59 to 21:41:05, so a later page could have failed. Our cluster has about 1,170 Endpoints, which is about 10 pages of 120. In today's list order, the lost service is on a later page than a service that kept working. We don't know the order at the time of the incident, or whether other services were also removed.

Expected Behavior

A failed re-list should not remove endpoints, or should be retried until it completes.

Error Logs

The only discovery error on the affected replica between 21:39 and 21:50 UTC (we collect error-level logs only):

[error] informer_factory.lua:335: watch failed, kind: Endpoints, reason: ReadBodyError, message : closed, context: ngx.timer

From 21:41:04 until the restart, every request to the service failed with:

handle_upstream(): failed to set upstream: no valid upstream node: nil

No list failed or connect apiserver failed error was logged.

Steps to Reproduce

We hit this once in production and haven't reproduced it. What led up to it:

  1. Run APISIX with discovery.kubernetes (single cluster, Endpoints) in a cluster with more than 120 Endpoints, so the list spans several pages.
  2. Route to a service through kubernetes discovery.
  3. Restart the Kubernetes API servers so the Endpoints watch fails and APISIX re-lists while the API servers are returning errors. For us this was a managed AKS control-plane restart, during which etcd briefly lost its leader.
  4. One replica returned 503 with no valid upstream node for one service until the pod was restarted.

Environment

  • APISIX version: 3.18.0 (image apache/apisix:3.18.0-ubuntu, Helm chart 2.17.0). The code above is the same in 3.19.0 and master.
  • Operating system: Ubuntu 24.04.4, kernel 6.8.0-1064-azure (uname -a: Linux 6.8.0-1064-azure #72-Ubuntu SMP Mon Jul 20 21:42:25 UTC 2026 x86_64 GNU/Linux)
  • OpenResty version: openresty/1.29.2.4
  • etcd version: not used (config_provider: yaml, standalone mode)
  • Kubernetes: AKS v1.35.4, discovery.kubernetes in single-cluster mode with Endpoints

sawyerward avatar Oct 17 '26 16:10 sawyerward

Thanks for the detailed report. The analysis looks right to me: list() in informer_factory.lua drops the result of the recursive call for the next page, so a failed later page still returns true, and post_list() then deletes every endpoint that wasn't seen. Since the following watch only delivers deltas, those endpoints never come back until a restart.

I'd like to work on this. I'll propagate the error from the paginated call so list_watch reports list failed and retries a full re-list, and add a regression test that fails on a later page.

GitNimay avatar Oct 19 '26 17:10 GitNimay