Skip to content

Commit 15632b1

Browse files
committed
Clean up kube-proxy metrics startup
1 parent 8747ba9 commit 15632b1

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

cmd/kube-proxy/app/server.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
580580
if hz == nil {
581581
return
582582
}
583+
583584
fn := func() {
584585
err := hz.Run()
585586
if err != nil {
@@ -593,6 +594,39 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
593594
go wait.Until(fn, 5*time.Second, wait.NeverStop)
594595
}
595596

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+
596630
// Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
597631
// 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.
598632
func (s *ProxyServer) Run() error {
@@ -618,27 +652,7 @@ func (s *ProxyServer) Run() error {
618652
serveHealthz(s.HealthzServer)
619653

620654
// Start up a metrics server if requested
621-
if len(s.MetricsBindAddress) > 0 {
622-
proxyMux := mux.NewPathRecorderMux("kube-proxy")
623-
healthz.InstallHandler(proxyMux)
624-
proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
625-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
626-
w.Header().Set("X-Content-Type-Options", "nosniff")
627-
fmt.Fprintf(w, "%s", s.ProxyMode)
628-
})
629-
//lint:ignore SA1019 See the Metrics Stability Migration KEP
630-
proxyMux.Handle("/metrics", legacyregistry.Handler())
631-
if s.EnableProfiling {
632-
routes.Profiling{}.Install(proxyMux)
633-
}
634-
configz.InstallHandler(proxyMux)
635-
go wait.Until(func() {
636-
err := http.ListenAndServe(s.MetricsBindAddress, proxyMux)
637-
if err != nil {
638-
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
639-
}
640-
}, 5*time.Second, wait.NeverStop)
641-
}
655+
serveMetrics(s.MetricsBindAddress, s.ProxyMode, s.EnableProfiling)
642656

643657
// Tune conntrack, if requested
644658
// 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",

0 commit comments

Comments
 (0)