Skip to content

Commit 9f7aaf4

Browse files
authored
Merge pull request kubernetes#89654 from thockin/proxy-cleanup
Proxy cleanup
2 parents f790f0a + 15632b1 commit 9f7aaf4

File tree

3 files changed

+74
-51
lines changed

3 files changed

+74
-51
lines changed

cmd/kube-proxy/app/server.go

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,57 @@ func createClients(config componentbaseconfig.ClientConnectionConfiguration, mas
576576
return client, eventClient.CoreV1(), nil
577577
}
578578

579+
func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
580+
if hz == nil {
581+
return
582+
}
583+
584+
fn := func() {
585+
err := hz.Run()
586+
if err != nil {
587+
// For historical reasons we do not abort on errors here. We may
588+
// change that in the future.
589+
klog.Errorf("healthz server failed: %v", err)
590+
} else {
591+
klog.Errorf("healthz server returned without error")
592+
}
593+
}
594+
go wait.Until(fn, 5*time.Second, wait.NeverStop)
595+
}
596+
597+
func serveMetrics(bindAddress string, proxyMode string, enableProfiling bool) {
598+
if len(bindAddress) == 0 {
599+
return
600+
}
601+
602+
proxyMux := mux.NewPathRecorderMux("kube-proxy")
603+
healthz.InstallHandler(proxyMux)
604+
proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
605+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
606+
w.Header().Set("X-Content-Type-Options", "nosniff")
607+
fmt.Fprintf(w, "%s", proxyMode)
608+
})
609+
610+
//lint:ignore SA1019 See the Metrics Stability Migration KEP
611+
proxyMux.Handle("/metrics", legacyregistry.Handler())
612+
613+
if enableProfiling {
614+
routes.Profiling{}.Install(proxyMux)
615+
}
616+
617+
configz.InstallHandler(proxyMux)
618+
619+
fn := func() {
620+
err := http.ListenAndServe(bindAddress, proxyMux)
621+
if err != nil {
622+
// For historical reasons we do not abort on errors here. We may
623+
// change that in the future.
624+
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
625+
}
626+
}
627+
go wait.Until(fn, 5*time.Second, wait.NeverStop)
628+
}
629+
579630
// Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
580631
// TODO: At the moment, Run() cannot return a nil error, otherwise it's caller will never exit. Update callers of Run to handle nil errors.
581632
func (s *ProxyServer) Run() error {
@@ -595,33 +646,13 @@ func (s *ProxyServer) Run() error {
595646
s.Broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: s.EventClient.Events("")})
596647
}
597648

649+
// TODO(thockin): make it possible for healthz and metrics to be on the same port.
650+
598651
// Start up a healthz server if requested
599-
if s.HealthzServer != nil {
600-
s.HealthzServer.Run()
601-
}
652+
serveHealthz(s.HealthzServer)
602653

603654
// Start up a metrics server if requested
604-
if len(s.MetricsBindAddress) > 0 {
605-
proxyMux := mux.NewPathRecorderMux("kube-proxy")
606-
healthz.InstallHandler(proxyMux)
607-
proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
608-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
609-
w.Header().Set("X-Content-Type-Options", "nosniff")
610-
fmt.Fprintf(w, "%s", s.ProxyMode)
611-
})
612-
//lint:ignore SA1019 See the Metrics Stability Migration KEP
613-
proxyMux.Handle("/metrics", legacyregistry.Handler())
614-
if s.EnableProfiling {
615-
routes.Profiling{}.Install(proxyMux)
616-
}
617-
configz.InstallHandler(proxyMux)
618-
go wait.Until(func() {
619-
err := http.ListenAndServe(s.MetricsBindAddress, proxyMux)
620-
if err != nil {
621-
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
622-
}
623-
}, 5*time.Second, wait.NeverStop)
624-
}
655+
serveMetrics(s.MetricsBindAddress, s.ProxyMode, s.EnableProfiling)
625656

626657
// Tune conntrack, if requested
627658
// Conntracker is always nil for windows

pkg/proxy/healthcheck/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ go_library(
2020
"//staging/src/k8s.io/api/core/v1:go_default_library",
2121
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
2222
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
23-
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
2423
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
2524
"//vendor/github.com/lithammer/dedent:go_default_library",
2625
"//vendor/k8s.io/klog:go_default_library",

pkg/proxy/healthcheck/proxier_health.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@ import (
2222
"sync/atomic"
2323
"time"
2424

25-
"k8s.io/klog"
26-
2725
"k8s.io/api/core/v1"
2826
"k8s.io/apimachinery/pkg/util/clock"
29-
"k8s.io/apimachinery/pkg/util/wait"
3027
"k8s.io/client-go/tools/record"
28+
"k8s.io/klog"
3129
api "k8s.io/kubernetes/pkg/apis/core"
3230
)
3331

34-
var proxierHealthzRetryInterval = 60 * time.Second
35-
3632
// ProxierHealthUpdater allows callers to update healthz timestamp only.
3733
type ProxierHealthUpdater interface {
3834
// QueuedUpdate should be called when the proxier receives a Service or Endpoints
@@ -43,8 +39,8 @@ type ProxierHealthUpdater interface {
4339
// rules to reflect the current state.
4440
Updated()
4541

46-
// Run starts the healthz http server and returns.
47-
Run()
42+
// Run starts the healthz HTTP server and blocks until it exits.
43+
Run() error
4844
}
4945

5046
var _ ProxierHealthUpdater = &proxierHealthServer{}
@@ -92,31 +88,28 @@ func (hs *proxierHealthServer) QueuedUpdate() {
9288
hs.lastQueued.Store(hs.clock.Now())
9389
}
9490

95-
// Run starts the healthz http server and returns.
96-
func (hs *proxierHealthServer) Run() {
91+
// Run starts the healthz HTTP server and blocks until it exits.
92+
func (hs *proxierHealthServer) Run() error {
9793
serveMux := http.NewServeMux()
9894
serveMux.Handle("/healthz", healthzHandler{hs: hs})
9995
server := hs.httpFactory.New(hs.addr, serveMux)
10096

101-
go wait.Until(func() {
102-
klog.V(3).Infof("Starting goroutine for proxier healthz on %s", hs.addr)
103-
104-
listener, err := hs.listener.Listen(hs.addr)
105-
if err != nil {
106-
msg := fmt.Sprintf("Failed to start proxier healthz on %s: %v", hs.addr, err)
107-
if hs.recorder != nil {
108-
hs.recorder.Eventf(hs.nodeRef, api.EventTypeWarning, "FailedToStartProxierHealthcheck", msg)
109-
}
110-
klog.Error(msg)
111-
return
97+
listener, err := hs.listener.Listen(hs.addr)
98+
if err != nil {
99+
msg := fmt.Sprintf("failed to start proxier healthz on %s: %v", hs.addr, err)
100+
// TODO(thockin): move eventing back to caller
101+
if hs.recorder != nil {
102+
hs.recorder.Eventf(hs.nodeRef, api.EventTypeWarning, "FailedToStartProxierHealthcheck", msg)
112103
}
104+
return fmt.Errorf("%v", msg)
105+
}
113106

114-
if err := server.Serve(listener); err != nil {
115-
klog.Errorf("Proxier healthz closed with error: %v", err)
116-
return
117-
}
118-
klog.Error("Unexpected proxier healthz closed.")
119-
}, proxierHealthzRetryInterval, wait.NeverStop)
107+
klog.V(3).Infof("starting healthz on %s", hs.addr)
108+
109+
if err := server.Serve(listener); err != nil {
110+
return fmt.Errorf("proxier healthz closed with error: %v", err)
111+
}
112+
return nil
120113
}
121114

122115
type healthzHandler struct {

0 commit comments

Comments
 (0)