Skip to content

Commit

Permalink
Merge pull request #99 from verult/http-endpoint-nodefault
Browse files Browse the repository at this point in the history
Introducing http-endpoint
  • Loading branch information
k8s-ci-robot committed Dec 15, 2020
2 parents 6d88548 + d9f8a40 commit f79c282
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions cmd/livenessprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"

Expand All @@ -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`.")
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit f79c282

Please sign in to comment.