Logic Overview
April 15, 2026 ยท View on GitHub
Four primary flows starting in cmd/kuberhealthy:
Startup and Configuration
mainloads configuration viasetUp, which reads environment variables into theGlobalConfigstruct defined incmd/kuberhealthy/config.go.- Kubernetes REST clients and the controller-runtime client are created once
so the process can watch
HealthCheckresources and interact with pods, events, and other cluster state. kuberhealthy.Newcreates the controller instance, wiring in the context and optional shutdown notifier channel used later during graceful termination.Globals.kh.StartBaseinitializes controller state without starting leader-only loops.StartWebServerbuilds the HTTP multiplexer and starts HTTP/TLS listeners for status pages, reporting, metrics, and helper endpoints on every replica.- When leader election is enabled, each replica attempts to acquire the Lease.
The leader runs
StartLeaderTasksto launch scheduling, timeout recovery, and CRD watches. Non-leaders continue to serve HTTP only. - When leader election is disabled,
StartLeaderTasksis invoked directly.
Scheduling and Running Checks
startScheduleLoopininternal/kuberhealthyevaluates known checks and determines whether any need to run based on the stored status and the configured intervals.- When a run is due, the controller creates a
Podderived from the check's embeddedPodSpec, annotating it with identifying labels and run metadata. - The controller monitors the pod lifecycle. It enforces timeouts with a small grace period and records Kubernetes events that describe failures.
- Once a pod exits successfully, the controller waits for a report from the pod before marking the run complete.
Reporting Results
- Check pods submit their status to the
/checkendpoint on the HTTP server. checkReportHandlervalidates the payload, verifies the run UUID, and writes the success flag and any error strings into thestatusblock of theHealthCheckresource.internal/kuberhealthyrecords the run metadata (duration, timestamps, and failure counts) so subsequent scheduling decisions can skip recent runs and inform the Prometheus exporter.
Pod Reaping
- The reaper runs once per minute to remove stale checker pods.
- Pods that remain running, pending, or unknown beyond twice the timeout (with a five minute minimum) are deleted as timed out.
- Completed pods are trimmed by
KH_MAX_COMPLETED_POD_COUNT, with0deleting all completed pods on the next sweep. - Failed pods are trimmed by
KH_MAX_ERROR_POD_COUNTand byKH_ERROR_POD_RETENTION_TIMEwhen configured. KH_MAX_CHECK_POD_AGEdeletes any checker pod that exceeds the configured age regardless of phase.
Metrics and Status Surfaces
- The
/jsonendpoint renders a JSON document summarizing each known check using the data persisted in thestatusblock. internal/metrics/exporter.goconstructs Prometheus metrics from the stored results, exposingkuberhealthy_checkand related series via the/metricsendpoint.- Optional helper endpoints allow triggering runs on-demand, streaming pod
logs, and downloading the OpenAPI schema, all of which reuse the shared
Globalsclients.
Legacy Conversion
Kuberhealthy v3 does not ship conversion webhooks or backward compatibility
paths. Legacy v2 custom resources (including the old KuberhealthyCheck kind,
KuberhealthyJob, and KuberhealthyState) must be migrated to
HealthCheck objects.