pkg/proxy/healthcheck: consolidate IsHealthy and isHealthy

Signed-off-by: Daman Arora <aroradaman@gmail.com>
This commit is contained in:
Daman Arora
2024-12-18 18:23:03 +05:30
parent 1c1fc73616
commit 3274dc40ed
3 changed files with 11 additions and 16 deletions

View File

@@ -143,8 +143,9 @@ type fakeProxyHealthChecker struct {
healthy bool
}
func (fake fakeProxyHealthChecker) IsHealthy() bool {
return fake.healthy
func (fake fakeProxyHealthChecker) Health() (bool, time.Time) {
// we only need "healthy" field for testing service healthchecks.
return fake.healthy, time.Time{}
}
func TestServer(t *testing.T) {

View File

@@ -101,14 +101,8 @@ func (hs *ProxyHealthServer) QueuedUpdate(ipFamily v1.IPFamily) {
}
}
// IsHealthy returns only the proxier's health state, following the same
// definition the HTTP server defines, but ignoring the state of the Node.
func (hs *ProxyHealthServer) IsHealthy() bool {
isHealthy, _ := hs.isHealthy()
return isHealthy
}
func (hs *ProxyHealthServer) isHealthy() (bool, time.Time) {
// Health returns only the proxier's health state and last updated time.
func (hs *ProxyHealthServer) Health() (bool, time.Time) {
hs.lock.RLock()
defer hs.lock.RUnlock()
@@ -188,7 +182,7 @@ type healthzHandler struct {
func (h healthzHandler) ServeHTTP(resp http.ResponseWriter, _ *http.Request) {
nodeEligible := h.hs.NodeEligible()
healthy, lastUpdated := h.hs.isHealthy()
healthy, lastUpdated := h.hs.Health()
currentTime := h.hs.clock.Now()
healthy = healthy && nodeEligible
@@ -215,7 +209,7 @@ type livezHandler struct {
}
func (h livezHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
healthy, lastUpdated := h.hs.isHealthy()
healthy, lastUpdated := h.hs.Health()
currentTime := h.hs.clock.Now()
resp.Header().Set("Content-Type", "application/json")
resp.Header().Set("X-Content-Type-Options", "nosniff")

View File

@@ -24,6 +24,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/lithammer/dedent"
@@ -53,9 +54,8 @@ type ServiceHealthServer interface {
}
type proxyHealthChecker interface {
// IsHealthy returns the proxy's health state, following the same
// definition the HTTP server defines.
IsHealthy() bool
// Health returns the proxy's health state and last updated time.
Health() (bool, time.Time)
}
func newServiceHealthServer(hostname string, recorder events.EventRecorder, listener listener, factory httpServerFactory, nodePortAddresses *proxyutil.NodePortAddresses, healthzServer proxyHealthChecker) ServiceHealthServer {
@@ -231,7 +231,7 @@ func (h hcHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
}
count := svc.endpoints
h.hcs.lock.RUnlock()
kubeProxyHealthy := h.hcs.healthzServer.IsHealthy()
kubeProxyHealthy, _ := h.hcs.healthzServer.Health()
resp.Header().Set("Content-Type", "application/json")
resp.Header().Set("X-Content-Type-Options", "nosniff")