From 5a672975e4bed0731c3910c3dd5d640fc87cec2f Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Thu, 3 Dec 2020 19:28:38 -0800 Subject: [PATCH] Introduce http-endpoint flag --- README.md | 2 +- cmd/csi-attacher/main.go | 20 ++++++++++++++++---- deploy/kubernetes/deployment.yaml | 6 +++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4fa2ba445..0b251d4b3 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ When CSI driver supports `LIST_VOLUMES` and `LIST_VOLUMES_PUBLISHED_NODES` capab ### HTTP endpoint -The external-attacher optionally exposes an HTTP endpoint at address:port specified by `--metrics-address` argument. When set, these two paths are exposed: +The external-attacher optionally exposes an HTTP endpoint at address:port specified by `--http-endpoint` argument. When set, these two paths are exposed: * Metrics path, as set by `--metrics-path` argument (default is `/metrics`). * Leader election health check at `/healthz/leader-election`. It is recommended to run a liveness probe against this endpoint when leader election is used to kill external-attacher leader that fails to connect to the API server to renew its leadership. See https://github.com/kubernetes-csi/csi-lib-utils/issues/66 for details. diff --git a/cmd/csi-attacher/main.go b/cmd/csi-attacher/main.go index 100d28c72..7e1d18247 100644 --- a/cmd/csi-attacher/main.go +++ b/cmd/csi-attacher/main.go @@ -67,7 +67,8 @@ var ( reconcileSync = flag.Duration("reconcile-sync", 1*time.Minute, "Resync interval of the VolumeAttachment reconciler.") - 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.") + 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. Only one of `--metrics-address` and `--http-endpoint` can be set.") + httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including metrics and leader election health check, will listen (example: :8080). The default is empty string, which means the server is disabled. Only one of `--metrics-address` and `--http-endpoint` can be set.") metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.") ) @@ -91,6 +92,15 @@ func main() { } klog.Infof("Version: %s", version) + if *metricsAddress != "" && *httpEndpoint != "" { + klog.Error("only one of `--metrics-address` and `--http-endpoint` can be set.") + os.Exit(1) + } + addr := *metricsAddress + if addr == "" { + addr = *httpEndpoint + } + // Create the client config. Use kubeconfig if given, otherwise assume in-cluster. config, err := buildConfig(*kubeconfig) if err != nil { @@ -138,11 +148,11 @@ func main() { // Prepare http endpoint for metrics + leader election healthz mux := http.NewServeMux() - if *metricsAddress != "" { + if addr != "" { metricsManager.RegisterToServer(mux, *metricsPath) metricsManager.SetDriverName(csiAttacher) go func() { - err := http.ListenAndServe(*metricsAddress, mux) + err := http.ListenAndServe(addr, mux) if err != nil { klog.Fatalf("Failed to start Prometheus metrics endpoint on specified address (%q) and path (%q): %s", *metricsAddress, *metricsPath, err) } @@ -219,7 +229,9 @@ func main() { // Name of config map with leader election lock lockName := "external-attacher-leader-" + csiAttacher le := leaderelection.NewLeaderElection(leClientset, lockName, run) - le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout) + if *httpEndpoint != "" { + le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout) + } if *leaderElectionNamespace != "" { le.WithNamespace(*leaderElectionNamespace) diff --git a/deploy/kubernetes/deployment.yaml b/deploy/kubernetes/deployment.yaml index efc462b97..9c7872c40 100644 --- a/deploy/kubernetes/deployment.yaml +++ b/deploy/kubernetes/deployment.yaml @@ -38,7 +38,7 @@ spec: - "--v=5" - "--csi-address=$(ADDRESS)" - "--leader-election" - - "--metrics-address=:8080" + - "--http-endpoint=:8080" env: - name: MY_NAME valueFrom: @@ -52,13 +52,13 @@ spec: mountPath: /var/lib/csi/sockets/pluginproxy/ ports: - containerPort: 8080 - name: metrics + name: http-endpoint protocol: TCP livenessProbe: failureThreshold: 1 httpGet: path: /healthz/leader-election - port: metrics + port: http-endpoint initialDelaySeconds: 10 timeoutSeconds: 10 periodSeconds: 20