@@ -576,6 +576,57 @@ func createClients(config componentbaseconfig.ClientConnectionConfiguration, mas
576
576
return client , eventClient .CoreV1 (), nil
577
577
}
578
578
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
+
579
630
// Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
580
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.
581
632
func (s * ProxyServer ) Run () error {
@@ -595,33 +646,13 @@ func (s *ProxyServer) Run() error {
595
646
s .Broadcaster .StartRecordingToSink (& v1core.EventSinkImpl {Interface : s .EventClient .Events ("" )})
596
647
}
597
648
649
+ // TODO(thockin): make it possible for healthz and metrics to be on the same port.
650
+
598
651
// Start up a healthz server if requested
599
- if s .HealthzServer != nil {
600
- s .HealthzServer .Run ()
601
- }
652
+ serveHealthz (s .HealthzServer )
602
653
603
654
// 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 )
625
656
626
657
// Tune conntrack, if requested
627
658
// Conntracker is always nil for windows
0 commit comments