Repository navigation
Answer /health from the health monitor's last check - #7001
Conversation
The transparent proxy answered every /health request by pinging the backend while the request waited, so a backend slow to answer GET / held the answer for up to the 5 s ping timeout. The operator's startup and readiness probes time out after 3 s and its liveness probe after 5 s, so such a backend failed the probes and the kubelet restarted a proxy whose backend was alive. With health checks enabled, the health checker now records the result of each background monitor check, and /health answers from the last one without pinging. Until the first check completes, /health reports the proxy as healthy without the mcp member. The monitor checks once when it starts and on every tick, also before the MCP server is initialized, and still counts failures toward shutdown only after initialization. The HTTP-SSE and streamable proxies keep building their answer per request, because their checks never wait on a backend. Signed-off-by: Aleksandr Filippov <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7001 +/- ##
==========================================
+ Coverage 80.29% 80.33% +0.04%
==========================================
Files 847 848 +1
Lines 86142 86334 +192
==========================================
+ Hits 69165 69354 +189
- Misses 16972 16975 +3
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jhrozek
left a comment
There was a problem hiding this comment.
Looks good, thanks. Answering /health from the monitor's last check is the right fix, and the tests show it.
One small non-blocking nit in docs/arch/03-transport-architecture.md: the staleness sentence reads as if the bound becomes two ping timeouts plus the retry delay after any failed check, but the retry only happens after initialization. Before that, a failed result just waits for the next tick, so the bound is really the larger of the two (interval + ping timeout vs. 2x ping timeout + retry delay). Both happen to be 15s with the defaults, so it only matters if someone raises the interval. Something like "at most about one interval plus one ping timeout old; once the MCP server is initialized, a failed check that is retried can instead stay current for two ping timeouts plus the retry delay, whichever is longer" would cover it.
Resolves conflicts with stacklok#7001, which made the health monitor check before the MCP server is initialized so /health answers from the last check. Remote workloads now count those checks from the start; local workloads keep counting only after initialization, so the test stacklok#7001 added for that behavior now runs against a local proxy. Adds the onHealthCheckRecovered constructor argument to the proxy calls main added, and keeps waitForShutdown for local-proxy shutdown alongside the remote tests' waitForCallback. Remote health checks now run by default, so the consent-cookie test's backend ignores the cookie-less health check requests. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Summary
The transparent proxy answers
/healthby pinging the backend while the request waits, so the answer takes as long as the backend'sGET /, up to the 5 s ping timeout. The operator's startup and readiness probes give up after 3 s and its liveness probe after 5 s, so a backend that is slow to answerGET /fails the probes, and three liveness failures make the kubelet restart a proxy whose backend is alive. The background monitor already pings the backend on its own interval, so/healthcan answer from that instead./healthpast a probe's timeout,pkg/healthcheckgains an opt-inWithLastResult()option: with it,CheckHealthrecords a copy of each result andServeHTTPanswers from the last one without pinging. The body shape and the status mapping stay as they are (healthyanddegradedanswer 200), andNewHealthCheckertakes the option as a variadic argument, so the other callers compile unchanged./healthhas a current answer from the start, the transparent proxy sets the option whenever health checks are enabled, andmonitorHealthchecks once when it starts and on every tick, before initialization too; failures still count toward shutdown only after the MCP server is initialized./healthanswers and when the monitor pings,docs/arch/03-transport-architecture.mddocuments both, and two comments that described the old skip are rewritten.The HTTP-SSE and streamable HTTP proxies (
pkg/transport/proxy/httpsse/http_proxy.go:254,pkg/transport/proxy/streamable/streamable_proxy.go:262) keep building their answer per request, since their checks never wait on a backend. The operator's probe values do not change: raising them above the ping timeout would also avoid the restart, but it ties two components' constants together and breaks again whenTOOLHIVE_HEALTH_CHECK_PING_TIMEOUTis raised.Closes #7000
Type of change
Test plan
task test)task test-e2e)task lint-fix)task lint-fix,task lintandtask testpass on Linux at this head on top of current main, with golangci-lint v2.15.0, the versionlint.ymlpins.New tests:
pkg/healthcheck/last_result_test.gocovers the answer before any result, a recordedhealthyresult, and a recordeddegradedone, with a pinger that blocks every ping after the first so that an inline ping could not answer in time; that the recorded result is a copy; and concurrentCheckHealthandServeHTTPcalls under-race.pkg/transport/proxy/transparent/health_monitor_test.gostarts the real proxy in front of a backend whoseGET /takes 7 s and expects/healthwithin 500 ms, expects the first check to be recorded at start with a one-hour interval, and checks that failures before initialization show asdegraded, neither stop the proxy nor carry over, and that failures after initialization still shut it down. Against main,TestTransparentProxy_HealthAnswersWhileBackendIsSlowandTestTransparentProxy_HealthCheckFailuresNotCountedBeforeInitializationfail and thepkg/healthchecktests do not compile. Dropping the start-up check, counting failures before initialization, recording a shallow copy, and pinging inline with the option set each fail at least one of the new tests. The reproduction in the issue passes: every probe gets 200 at once, the 4 s and 7 s backends included.Changes
pkg/healthcheck/healthcheck.goWithLastResultoption;CheckHealthrecords a copy of each result andServeHTTPanswers from itpkg/transport/proxy/transparent/transparent_proxy.gomonitorHealthchecks at start and on every tick and counts failures only after initializationpkg/healthcheck/last_result_test.go,pkg/transport/proxy/transparent/health_monitor_test.gopkg/transport/proxy/transparent/transparent_test.godocs/arch/03-transport-architecture.md/healthanswerstest/e2e/health_check_zombie_test.goDoes this introduce a user-facing change?
Yes. Before: with health checks enabled (local workloads, and remote ones that set
TOOLHIVE_REMOTE_HEALTHCHECKS), each/healthrequest pinged the backend and waited up to the ping timeout, so a backend slow to answerGET /made the operator's 3 s startup and readiness probes time out, and at 5 s the liveness probe too, and the kubelet restarted the proxy. After:/healthanswers at once from the monitor's last check, which is at most about one interval plus one ping timeout old, or two ping timeouts plus the retry delay after a failed check (15 s either way with the defaults); itstimestampandmcp.last_checkedsay when that check started, and a backend that missed the ping timeout shows asdegradedwith HTTP 200, as before. Until the first check completes,/healthreportshealthywithout themcpmember.Operators: the backend now receives health pings only from the monitor: one at start-up, one per interval before and after initialization, and the retry after a failed check once the server is initialized, as before; kubelet probes no longer reach it. The Debug line for a tick before initialization now reads
mcp server not initialized yet, not counting health checkand carries the check'sstatus.Special notes for reviewers
/healthanswershealthywithoutmcp, the same answer as with health checks disabled, rather than pinging inline once or answering 503: Kubernetes sees 200 in that window either way, as on main, wheredegradedalso answers 200, and the window lasts only until the monitor's first ping, started with the monitor, returns or times out.TestTransparentProxy_HealthCheckNotRunBeforeInitializationis removed because its premise, no check before initialization, no longer holds;TestTransparentProxy_HealthCheckFailuresNotCountedBeforeInitializationcovers the part that still holds, that failures before initialization never stop the proxy.TestPrefixHandlers_MountingAndRoutingandTestMetricsNotServedWithoutHandlernow passenableHealthCheck=false: the monitor's start-up ping reached their recording backend and read as a proxied request. Their routing assertions do not change.handleBackendHealth,pkg/vmcp/server/server.go:1514, readinghealthMon.GetAllBackendStates()at:1481). This change records insidepkg/healthcheckrather than reusingpkg/vmcp/health, becauseHealthCheckeris shared by the three proxies and already defines the/healthbody; the option only adds the stored result.monitorHealthso that remote workloads count failures before initialization; its condition applies unchanged to the counting check this PR keeps, and whichever PR lands second needs a small rebase.