Skip to content

Commit 7f7b14d

Browse files
committed
Expose metrics endpoint for livenessprobe sidecar
`metricsAddress` and `metricsPort` can be used to configure livenessprobe sidecar metrics configuration Signed-off-by: Humble Chirammal <[email protected]>
1 parent 5399319 commit 7f7b14d

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ spec:
7575

7676
* `--probe-timeout <duration>`: Maximum duration of single `Probe()` call (default "1s").
7777

78+
* `--metrics-address <port>`: The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.
79+
80+
* `--metrics-path <path>`: The HTTP path where prometheus metrics will be exposed. Default is `/metrics`."
81+
7882
* All glog / klog arguments are supported, such as `-v <log level>` or `-alsologtostderr`.
7983

8084
## Community, discussion, contribution, and support

cmd/livenessprobe/livenessprobe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
csi "github.com/container-storage-interface/spec/lib/go/csi"
2929
"github.com/golang/mock/gomock"
30-
"github.com/kubernetes-csi/csi-test/driver"
30+
"github.com/kubernetes-csi/csi-test/v4/driver"
3131
)
3232

3333
const (

cmd/livenessprobe/main.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ import (
2424
"sync"
2525
"time"
2626

27+
"google.golang.org/grpc"
28+
"k8s.io/klog/v2"
29+
2730
connlib "github.com/kubernetes-csi/csi-lib-utils/connection"
31+
"github.com/kubernetes-csi/csi-lib-utils/metrics"
2832
"github.com/kubernetes-csi/csi-lib-utils/rpc"
29-
"google.golang.org/grpc"
30-
"k8s.io/klog"
3133
)
3234

3335
// Command line flags
3436
var (
35-
probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds")
36-
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
37-
healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests")
37+
probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds")
38+
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.")
41+
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
3842
)
3943

4044
type healthProbe struct {
@@ -45,7 +49,7 @@ func (h *healthProbe) checkProbe(w http.ResponseWriter, req *http.Request) {
4549
ctx, cancel := context.WithTimeout(req.Context(), *probeTimeout)
4650
defer cancel()
4751

48-
conn, err := acquireConnection(ctx)
52+
conn, err := acquireConnection(ctx, metrics.NewCSIMetricsManager(""))
4953
if err != nil {
5054
w.WriteHeader(http.StatusInternalServerError)
5155
w.Write([]byte(err.Error()))
@@ -77,12 +81,13 @@ func (h *healthProbe) checkProbe(w http.ResponseWriter, req *http.Request) {
7781

7882
// acquireConnection wraps the connlib.Connect but adding support to context
7983
// cancelation.
80-
func acquireConnection(ctx context.Context) (conn *grpc.ClientConn, err error) {
84+
func acquireConnection(ctx context.Context, metricsManager metrics.CSIMetricsManager) (conn *grpc.ClientConn, err error) {
85+
8186
var m sync.Mutex
8287
var canceled bool
8388
ready := make(chan bool)
8489
go func() {
85-
conn, err = connlib.Connect(*csiAddress)
90+
conn, err = connlib.Connect(*csiAddress, metricsManager)
8691

8792
m.Lock()
8893
defer m.Unlock()
@@ -109,8 +114,8 @@ func main() {
109114
klog.InitFlags(nil)
110115
flag.Set("logtostderr", "true")
111116
flag.Parse()
112-
113-
csiConn, err := acquireConnection(context.Background())
117+
metricsManager := metrics.NewCSIMetricsManager("")
118+
csiConn, err := acquireConnection(context.Background(), metricsManager)
114119
if err != nil {
115120
// connlib should retry forever so a returned error should mean
116121
// the grpc client is misconfigured rather than an error on the network
@@ -129,6 +134,9 @@ func main() {
129134
driverName: csiDriverName,
130135
}
131136

137+
metricsManager.SetDriverName(csiDriverName)
138+
metricsManager.StartMetricsEndpoint(*metricsAddress, *metricsPath)
139+
132140
addr := net.JoinHostPort("0.0.0.0", *healthzPort)
133141
http.HandleFunc("/healthz", hp.checkProbe)
134142
klog.Infof("Serving requests to /healthz on: %s", addr)

0 commit comments

Comments
 (0)