From ceeddf6473703c43b3c8f1d14c97444e0687a9e7 Mon Sep 17 00:00:00 2001 From: 181192 Date: Sun, 2 Jul 2023 22:06:03 +0200 Subject: [PATCH] Always serve /healthz endpoint Rename metrics_port env to http_port --- cmd/azure-keyvault-controller/main.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/azure-keyvault-controller/main.go b/cmd/azure-keyvault-controller/main.go index 219ebff8..a177fcbc 100644 --- a/cmd/azure-keyvault-controller/main.go +++ b/cmd/azure-keyvault-controller/main.go @@ -75,7 +75,7 @@ var ( func initConfig() { viper.SetDefault("auth_type", "azureCloudConfig") viper.SetDefault("metrics_enabled", false) - viper.SetDefault("metrics_port", "9000") + viper.SetDefault("http_port", "9000") viper.AutomaticEnv() } @@ -111,13 +111,9 @@ func main() { akv2k8s.LogVersion() authType := viper.GetString("auth_type") - serveMetrics := viper.GetBool("metrics_enabled") - metricsPort := viper.GetString("metrics_port") objectLabels := viper.GetString("object_labels") - if serveMetrics { - createMetricsServer(metricsPort) - } + createHttpServer() // set up signals so we handle the first shutdown signal gracefully stopCh := signals.SetupSignalHandler() @@ -227,12 +223,17 @@ func main() { controller.Run(stopCh) } -func createMetricsServer(metricsPort string) { +func createHttpServer() { + httpPort := viper.GetString("http_port") + serveMetrics := viper.GetBool("metrics_enabled") + router := mux.NewRouter() - httpURL := fmt.Sprintf(":%s", metricsPort) + httpURL := fmt.Sprintf(":%s", httpPort) - router.Handle("/metrics", promhttp.Handler()) - klog.InfoS("serving metrics endpoint", "path", fmt.Sprintf("%s/metrics", httpURL)) + if serveMetrics { + router.Handle("/metrics", promhttp.Handler()) + klog.InfoS("serving metrics endpoint", "path", fmt.Sprintf("%s/metrics", httpURL)) + } router.HandleFunc("/healthz", healthHandler) klog.InfoS("serving health endpoint", "path", fmt.Sprintf("%s/healthz", httpURL)) @@ -240,7 +241,7 @@ func createMetricsServer(metricsPort string) { go func() { err := http.ListenAndServe(httpURL, router) if err != nil { - klog.ErrorS(err, "error serving metrics", "url", httpURL) + klog.ErrorS(err, "error serving http server", "url", httpURL) os.Exit(1) } }()