diff --git a/cmd/livenessprobe/main.go b/cmd/livenessprobe/main.go index 2dec1329..006a2b9c 100644 --- a/cmd/livenessprobe/main.go +++ b/cmd/livenessprobe/main.go @@ -19,8 +19,10 @@ package main import ( "context" "flag" + "fmt" "net" "net/http" + "os" "sync" "time" @@ -32,12 +34,17 @@ import ( "github.com/kubernetes-csi/csi-lib-utils/rpc" ) +const ( + defaultHealthzPort = "9808" +) + // Command line flags var ( - probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds") + probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds.") csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.") - healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests") - metricsAddress = flag.String("metrics-address", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.") + healthzPort = flag.String("health-port", defaultHealthzPort, fmt.Sprintf("(deprecated) TCP ports for listening healthz requests. The default is `%s`. If set, `--http-endpoint` cannot be set.", defaultHealthzPort)) + metricsAddress = flag.String("metrics-address", "", "(deprecated) The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled. If set, `--http-endpoint` cannot be set, and the address cannot resolve to localhost + the port from `--health-port`.") + httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including CSI driver health check and metrics. The default is empty string, which means the server is disabled. If set, `--health-port` and `--metrics-address` cannot be explicitly set.") metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.") ) @@ -115,6 +122,22 @@ func main() { klog.InitFlags(nil) flag.Set("logtostderr", "true") flag.Parse() + + if *healthzPort != defaultHealthzPort && *httpEndpoint != "" { + klog.Error("only one of `--health-port` and `--http-endpoint` can be explicitly set.") + os.Exit(1) + } + if *metricsAddress != "" && *httpEndpoint != "" { + klog.Error("only one of `--metrics-address` and `--http-endpoint` can be explicitly set.") + os.Exit(1) + } + var addr string + if *httpEndpoint != "" { + addr = *httpEndpoint + } else { + addr = net.JoinHostPort("0.0.0.0", *healthzPort) + } + metricsManager := metrics.NewCSIMetricsManager("" /* driverName */) csiConn, err := acquireConnection(context.Background(), metricsManager) if err != nil { @@ -137,12 +160,27 @@ func main() { } mux := http.NewServeMux() - addr := net.JoinHostPort("0.0.0.0", *healthzPort) - metricsManager.RegisterToServer(mux, *metricsPath) metricsManager.SetDriverName(csiDriverName) + if *metricsAddress == "" { + if *httpEndpoint != "" { + metricsManager.RegisterToServer(mux, *metricsPath) + } + } else { + // Remove once --metrics-address is removed + metricsMux := http.NewServeMux() + metricsManager.RegisterToServer(metricsMux, *metricsPath) + go func() { + klog.Infof("Separate metrics ServeMux listening at %q", *metricsAddress) + err := http.ListenAndServe(*metricsAddress, metricsMux) + if err != nil { + klog.Fatalf("Failed to start prometheus metrics endpoint on specified address (%q) and path (%q): %s", *metricsAddress, *metricsPath, err) + } + }() + } + mux.HandleFunc("/healthz", hp.checkProbe) - klog.Infof("Serving requests to /healthz on: %s", addr) + klog.Infof("ServeMux listening at %q", addr) err = http.ListenAndServe(addr, mux) if err != nil { klog.Fatalf("failed to start http server with error: %v", err)