Clean up kube-proxy metrics startup

This commit is contained in:
Tim Hockin 2020-03-27 16:50:44 -07:00
parent 8747ba9370
commit 15632b10cb
2 changed files with 35 additions and 22 deletions

View File

@ -580,6 +580,7 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
if hz == nil {
return
}
fn := func() {
err := hz.Run()
if err != nil {
@ -593,6 +594,39 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
go wait.Until(fn, 5*time.Second, wait.NeverStop)
}
func serveMetrics(bindAddress string, proxyMode string, enableProfiling bool) {
if len(bindAddress) == 0 {
return
}
proxyMux := mux.NewPathRecorderMux("kube-proxy")
healthz.InstallHandler(proxyMux)
proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
fmt.Fprintf(w, "%s", proxyMode)
})
//lint:ignore SA1019 See the Metrics Stability Migration KEP
proxyMux.Handle("/metrics", legacyregistry.Handler())
if enableProfiling {
routes.Profiling{}.Install(proxyMux)
}
configz.InstallHandler(proxyMux)
fn := func() {
err := http.ListenAndServe(bindAddress, proxyMux)
if err != nil {
// For historical reasons we do not abort on errors here. We may
// change that in the future.
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
}
}
go wait.Until(fn, 5*time.Second, wait.NeverStop)
}
// Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
// 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.
func (s *ProxyServer) Run() error {
@ -618,27 +652,7 @@ func (s *ProxyServer) Run() error {
serveHealthz(s.HealthzServer)
// Start up a metrics server if requested
if len(s.MetricsBindAddress) > 0 {
proxyMux := mux.NewPathRecorderMux("kube-proxy")
healthz.InstallHandler(proxyMux)
proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
fmt.Fprintf(w, "%s", s.ProxyMode)
})
//lint:ignore SA1019 See the Metrics Stability Migration KEP
proxyMux.Handle("/metrics", legacyregistry.Handler())
if s.EnableProfiling {
routes.Profiling{}.Install(proxyMux)
}
configz.InstallHandler(proxyMux)
go wait.Until(func() {
err := http.ListenAndServe(s.MetricsBindAddress, proxyMux)
if err != nil {
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
}
}, 5*time.Second, wait.NeverStop)
}
serveMetrics(s.MetricsBindAddress, s.ProxyMode, s.EnableProfiling)
// Tune conntrack, if requested
// Conntracker is always nil for windows

View File

@ -20,7 +20,6 @@ go_library(
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//vendor/github.com/lithammer/dedent:go_default_library",
"//vendor/k8s.io/klog:go_default_library",