Skip to content

Commit d9f8a40

Browse files
committed
Introducing http-endpoint
1 parent 6d88548 commit d9f8a40

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

cmd/livenessprobe/main.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package main
1919
import (
2020
"context"
2121
"flag"
22+
"fmt"
2223
"net"
2324
"net/http"
25+
"os"
2426
"sync"
2527
"time"
2628

@@ -32,12 +34,17 @@ import (
3234
"github.com/kubernetes-csi/csi-lib-utils/rpc"
3335
)
3436

37+
const (
38+
defaultHealthzPort = "9808"
39+
)
40+
3541
// Command line flags
3642
var (
37-
probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds")
43+
probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds.")
3844
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
39-
healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests")
40-
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.")
45+
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))
46+
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`.")
47+
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.")
4148
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
4249
)
4350

@@ -115,6 +122,22 @@ func main() {
115122
klog.InitFlags(nil)
116123
flag.Set("logtostderr", "true")
117124
flag.Parse()
125+
126+
if *healthzPort != defaultHealthzPort && *httpEndpoint != "" {
127+
klog.Error("only one of `--health-port` and `--http-endpoint` can be explicitly set.")
128+
os.Exit(1)
129+
}
130+
if *metricsAddress != "" && *httpEndpoint != "" {
131+
klog.Error("only one of `--metrics-address` and `--http-endpoint` can be explicitly set.")
132+
os.Exit(1)
133+
}
134+
var addr string
135+
if *httpEndpoint != "" {
136+
addr = *httpEndpoint
137+
} else {
138+
addr = net.JoinHostPort("0.0.0.0", *healthzPort)
139+
}
140+
118141
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
119142
csiConn, err := acquireConnection(context.Background(), metricsManager)
120143
if err != nil {
@@ -137,12 +160,27 @@ func main() {
137160
}
138161

139162
mux := http.NewServeMux()
140-
addr := net.JoinHostPort("0.0.0.0", *healthzPort)
141-
metricsManager.RegisterToServer(mux, *metricsPath)
142163
metricsManager.SetDriverName(csiDriverName)
143164

165+
if *metricsAddress == "" {
166+
if *httpEndpoint != "" {
167+
metricsManager.RegisterToServer(mux, *metricsPath)
168+
}
169+
} else {
170+
// Remove once --metrics-address is removed
171+
metricsMux := http.NewServeMux()
172+
metricsManager.RegisterToServer(metricsMux, *metricsPath)
173+
go func() {
174+
klog.Infof("Separate metrics ServeMux listening at %q", *metricsAddress)
175+
err := http.ListenAndServe(*metricsAddress, metricsMux)
176+
if err != nil {
177+
klog.Fatalf("Failed to start prometheus metrics endpoint on specified address (%q) and path (%q): %s", *metricsAddress, *metricsPath, err)
178+
}
179+
}()
180+
}
181+
144182
mux.HandleFunc("/healthz", hp.checkProbe)
145-
klog.Infof("Serving requests to /healthz on: %s", addr)
183+
klog.Infof("ServeMux listening at %q", addr)
146184
err = http.ListenAndServe(addr, mux)
147185
if err != nil {
148186
klog.Fatalf("failed to start http server with error: %v", err)

0 commit comments

Comments
 (0)