@@ -580,6 +580,7 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
580
580
if hz == nil {
581
581
return
582
582
}
583
+
583
584
fn := func () {
584
585
err := hz .Run ()
585
586
if err != nil {
@@ -593,6 +594,39 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater) {
593
594
go wait .Until (fn , 5 * time .Second , wait .NeverStop )
594
595
}
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
+
596
630
// Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
597
631
// 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.
598
632
func (s * ProxyServer ) Run () error {
@@ -618,27 +652,7 @@ func (s *ProxyServer) Run() error {
618
652
serveHealthz (s .HealthzServer )
619
653
620
654
// 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 )
642
656
643
657
// Tune conntrack, if requested
644
658
// Conntracker is always nil for windows
0 commit comments