From 7a0690cd592f6ae797a71c8757603a20defcbe41 Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Mon, 10 Feb 2020 09:41:09 -0800 Subject: [PATCH] Use ProxierHealthUpdater directly to avoid panic --- cmd/kube-proxy/app/server.go | 2 +- cmd/kube-proxy/app/server_others.go | 2 +- cmd/kube-proxy/app/server_windows.go | 2 +- pkg/proxy/healthcheck/proxier_health.go | 23 +++++++++++++---------- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cmd/kube-proxy/app/server.go b/cmd/kube-proxy/app/server.go index 1f87fc53d59..033c297d15c 100644 --- a/cmd/kube-proxy/app/server.go +++ b/cmd/kube-proxy/app/server.go @@ -532,7 +532,7 @@ type ProxyServer struct { UseEndpointSlices bool OOMScoreAdj *int32 ConfigSyncPeriod time.Duration - HealthzServer *healthcheck.ProxierHealthServer + HealthzServer healthcheck.ProxierHealthUpdater } // createClients creates a kube client and an event client from the given config and masterOverride. diff --git a/cmd/kube-proxy/app/server_others.go b/cmd/kube-proxy/app/server_others.go index 87e3ea3866c..23c977007f8 100644 --- a/cmd/kube-proxy/app/server_others.go +++ b/cmd/kube-proxy/app/server_others.go @@ -130,7 +130,7 @@ func newProxyServer( Namespace: "", } - var healthzServer *healthcheck.ProxierHealthServer + var healthzServer healthcheck.ProxierHealthUpdater if len(config.HealthzBindAddress) > 0 { healthzServer = healthcheck.NewProxierHealthServer(config.HealthzBindAddress, 2*config.IPTables.SyncPeriod.Duration, recorder, nodeRef) } diff --git a/cmd/kube-proxy/app/server_windows.go b/cmd/kube-proxy/app/server_windows.go index f94ec6c5297..a2e6d2c63e0 100644 --- a/cmd/kube-proxy/app/server_windows.go +++ b/cmd/kube-proxy/app/server_windows.go @@ -92,7 +92,7 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, cleanupAndExi Namespace: "", } - var healthzServer *healthcheck.ProxierHealthServer + var healthzServer healthcheck.ProxierHealthUpdater if len(config.HealthzBindAddress) > 0 { healthzServer = healthcheck.NewProxierHealthServer(config.HealthzBindAddress, 2*config.IPTables.SyncPeriod.Duration, recorder, nodeRef) } diff --git a/pkg/proxy/healthcheck/proxier_health.go b/pkg/proxy/healthcheck/proxier_health.go index e9a0002304a..afeb4fff657 100644 --- a/pkg/proxy/healthcheck/proxier_health.go +++ b/pkg/proxy/healthcheck/proxier_health.go @@ -42,13 +42,16 @@ type ProxierHealthUpdater interface { // Updated should be called when the proxier has successfully updated the service // rules to reflect the current state. Updated() + + // Run starts the healthz http server and returns. + Run() } -var _ ProxierHealthUpdater = &ProxierHealthServer{} +var _ ProxierHealthUpdater = &proxierHealthServer{} -// ProxierHealthServer returns 200 "OK" by default. It verifies that the delay between +// proxierHealthServer returns 200 "OK" by default. It verifies that the delay between // QueuedUpdate() calls and Updated() calls never exceeds healthTimeout. -type ProxierHealthServer struct { +type proxierHealthServer struct { listener listener httpFactory httpServerFactory clock clock.Clock @@ -63,12 +66,12 @@ type ProxierHealthServer struct { } // NewProxierHealthServer returns a proxier health http server. -func NewProxierHealthServer(addr string, healthTimeout time.Duration, recorder record.EventRecorder, nodeRef *v1.ObjectReference) *ProxierHealthServer { +func NewProxierHealthServer(addr string, healthTimeout time.Duration, recorder record.EventRecorder, nodeRef *v1.ObjectReference) ProxierHealthUpdater { return newProxierHealthServer(stdNetListener{}, stdHTTPServerFactory{}, clock.RealClock{}, addr, healthTimeout, recorder, nodeRef) } -func newProxierHealthServer(listener listener, httpServerFactory httpServerFactory, c clock.Clock, addr string, healthTimeout time.Duration, recorder record.EventRecorder, nodeRef *v1.ObjectReference) *ProxierHealthServer { - return &ProxierHealthServer{ +func newProxierHealthServer(listener listener, httpServerFactory httpServerFactory, c clock.Clock, addr string, healthTimeout time.Duration, recorder record.EventRecorder, nodeRef *v1.ObjectReference) *proxierHealthServer { + return &proxierHealthServer{ listener: listener, httpFactory: httpServerFactory, clock: c, @@ -80,17 +83,17 @@ func newProxierHealthServer(listener listener, httpServerFactory httpServerFacto } // Updated updates the lastUpdated timestamp. -func (hs *ProxierHealthServer) Updated() { +func (hs *proxierHealthServer) Updated() { hs.lastUpdated.Store(hs.clock.Now()) } // QueuedUpdate updates the lastQueued timestamp. -func (hs *ProxierHealthServer) QueuedUpdate() { +func (hs *proxierHealthServer) QueuedUpdate() { hs.lastQueued.Store(hs.clock.Now()) } // Run starts the healthz http server and returns. -func (hs *ProxierHealthServer) Run() { +func (hs *proxierHealthServer) Run() { serveMux := http.NewServeMux() serveMux.Handle("/healthz", healthzHandler{hs: hs}) server := hs.httpFactory.New(hs.addr, serveMux) @@ -117,7 +120,7 @@ func (hs *ProxierHealthServer) Run() { } type healthzHandler struct { - hs *ProxierHealthServer + hs *proxierHealthServer } func (h healthzHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {