@@ -39,6 +39,7 @@ import (
39
39
"github.com/prometheus/common/version"
40
40
"github.com/prometheus/exporter-toolkit/web"
41
41
"gopkg.in/yaml.v3"
42
+ "k8s.io/client-go/kubernetes"
42
43
_ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins.
43
44
"k8s.io/client-go/tools/clientcmd"
44
45
"k8s.io/klog/v2"
@@ -59,6 +60,7 @@ import (
59
60
const (
60
61
metricsPath = "/metrics"
61
62
healthzPath = "/healthz"
63
+ livezPath = "/livez"
62
64
)
63
65
64
66
// promLogger implements promhttp.Logger
@@ -321,7 +323,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
321
323
WebConfigFile : & tlsConfig ,
322
324
}
323
325
324
- metricsMux := buildMetricsServer (m , durationVec )
326
+ metricsMux := buildMetricsServer (m , durationVec , kubeClient )
325
327
metricsServerListenAddress := net .JoinHostPort (opts .Host , strconv .Itoa (opts .Port ))
326
328
metricsServer := http.Server {
327
329
Handler : metricsMux ,
@@ -390,7 +392,7 @@ func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux {
390
392
return mux
391
393
}
392
394
393
- func buildMetricsServer (m * metricshandler.MetricsHandler , durationObserver prometheus.ObserverVec ) * http.ServeMux {
395
+ func buildMetricsServer (m * metricshandler.MetricsHandler , durationObserver prometheus.ObserverVec , client kubernetes. Interface ) * http.ServeMux {
394
396
mux := http .NewServeMux ()
395
397
396
398
// TODO: This doesn't belong into serveMetrics
@@ -400,8 +402,23 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
400
402
mux .Handle ("/debug/pprof/symbol" , http .HandlerFunc (pprof .Symbol ))
401
403
mux .Handle ("/debug/pprof/trace" , http .HandlerFunc (pprof .Trace ))
402
404
405
+ // Add metricsPath
403
406
mux .Handle (metricsPath , promhttp .InstrumentHandlerDuration (durationObserver , m ))
404
407
408
+ // Add livezPath
409
+ mux .Handle (livezPath , http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
410
+
411
+ // Query the Kube API to make sure we are not affected by a network outage.
412
+ got := client .CoreV1 ().RESTClient ().Get ().AbsPath ("/apis/" ).Do (context .Background ())
413
+ if got .Error () != nil {
414
+ w .WriteHeader (http .StatusServiceUnavailable )
415
+ w .Write ([]byte (http .StatusText (http .StatusServiceUnavailable )))
416
+ return
417
+ }
418
+ w .WriteHeader (http .StatusOK )
419
+ w .Write ([]byte (http .StatusText (http .StatusOK )))
420
+ }))
421
+
405
422
// Add healthzPath
406
423
mux .HandleFunc (healthzPath , func (w http.ResponseWriter , _ * http.Request ) {
407
424
w .WriteHeader (http .StatusOK )
@@ -422,6 +439,10 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
422
439
Address : healthzPath ,
423
440
Text : "Healthz" ,
424
441
},
442
+ {
443
+ Address : livezPath ,
444
+ Text : "Livez" ,
445
+ },
425
446
},
426
447
}
427
448
landingPage , err := web .NewLandingPage (landingConfig )
0 commit comments