Skip to content

Commit 4b03c21

Browse files
committed
enhancement: add livez endpoint
Add a `livez` endpoint to identify network outages. This helps in restarting the binary if such as case is observed. Signed-off-by: Pranshu Srivastava <[email protected]>
1 parent 669b501 commit 4b03c21

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/app/server.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/prometheus/common/version"
4040
"github.com/prometheus/exporter-toolkit/web"
4141
"gopkg.in/yaml.v3"
42+
"k8s.io/client-go/kubernetes"
4243
_ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins.
4344
"k8s.io/client-go/tools/clientcmd"
4445
"k8s.io/klog/v2"
@@ -59,6 +60,7 @@ import (
5960
const (
6061
metricsPath = "/metrics"
6162
healthzPath = "/healthz"
63+
livezPath = "/livez"
6264
)
6365

6466
// promLogger implements promhttp.Logger
@@ -321,7 +323,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
321323
WebConfigFile: &tlsConfig,
322324
}
323325

324-
metricsMux := buildMetricsServer(m, durationVec)
326+
metricsMux := buildMetricsServer(m, durationVec, kubeClient)
325327
metricsServerListenAddress := net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
326328
metricsServer := http.Server{
327329
Handler: metricsMux,
@@ -390,7 +392,7 @@ func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux {
390392
return mux
391393
}
392394

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 {
394396
mux := http.NewServeMux()
395397

396398
// TODO: This doesn't belong into serveMetrics
@@ -400,8 +402,23 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
400402
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
401403
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
402404

405+
// Add metricsPath
403406
mux.Handle(metricsPath, promhttp.InstrumentHandlerDuration(durationObserver, m))
404407

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+
405422
// Add healthzPath
406423
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, _ *http.Request) {
407424
w.WriteHeader(http.StatusOK)
@@ -422,6 +439,10 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
422439
Address: healthzPath,
423440
Text: "Healthz",
424441
},
442+
{
443+
Address: livezPath,
444+
Text: "Livez",
445+
},
425446
},
426447
}
427448
landingPage, err := web.NewLandingPage(landingConfig)

0 commit comments

Comments
 (0)