From c3ce077f7854d4f36e53accb2d04fe050c34eea3 Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Mon, 5 Aug 2024 20:01:11 +0300 Subject: [PATCH] New probe handlers (#100) Signed-off-by: Igor Shishkin --- cmd/exporter/main.go | 30 +++++++++++++++++++ cmd/manager/main.go | 30 +++++++++++++++++++ cmd/publisher/main.go | 30 +++++++++++++++++++ .../deploy/k8s/archived-exporter.yaml | 12 ++++++-- .../examples/deploy/k8s/archived-manager.yaml | 12 ++++++-- .../deploy/k8s/archived-publisher.yaml | 12 ++++++-- 6 files changed, 117 insertions(+), 9 deletions(-) diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index 88a957f..2bf6a37 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -64,6 +64,36 @@ func main() { g.Go(func() error { http.Handle("/metrics", promhttp.Handler()) + + http.HandleFunc("/healthz/startup", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + }) + + http.HandleFunc("/healthz/readiness", func(w http.ResponseWriter, r *http.Request) { + log.Warnf("db.Ping() error on readiness probe: %s", err) + + if err := db.Ping(); err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + + http.HandleFunc("/healthz/liveness", func(w http.ResponseWriter, r *http.Request) { + if err := db.Ping(); err != nil { + log.Warnf("db.Ping() error on liveness probe: %s", err) + + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + return http.ListenAndServe(cfg.Addr, nil) }) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index f55a535..f383dea 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -128,6 +128,36 @@ func main() { g.Go(func() error { http.Handle("/metrics", promhttp.Handler()) + + http.HandleFunc("/healthz/startup", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + }) + + http.HandleFunc("/healthz/readiness", func(w http.ResponseWriter, r *http.Request) { + log.Warnf("db.Ping() error on readiness probe: %s", err) + + if err := db.Ping(); err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + + http.HandleFunc("/healthz/liveness", func(w http.ResponseWriter, r *http.Request) { + if err := db.Ping(); err != nil { + log.Warnf("db.Ping() error on liveness probe: %s", err) + + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + return http.ListenAndServe(cfg.MetricsAddr, nil) }) diff --git a/cmd/publisher/main.go b/cmd/publisher/main.go index aaacf6a..eda2a23 100644 --- a/cmd/publisher/main.go +++ b/cmd/publisher/main.go @@ -112,6 +112,36 @@ func main() { g.Go(func() error { http.Handle("/metrics", promhttp.Handler()) + + http.HandleFunc("/healthz/startup", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + }) + + http.HandleFunc("/healthz/readiness", func(w http.ResponseWriter, r *http.Request) { + log.Warnf("db.Ping() error on readiness probe: %s", err) + + if err := db.Ping(); err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + + http.HandleFunc("/healthz/liveness", func(w http.ResponseWriter, r *http.Request) { + if err := db.Ping(); err != nil { + log.Warnf("db.Ping() error on liveness probe: %s", err) + + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("failed\n")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok\n")) + } + }) + return http.ListenAndServe(cfg.MetricsAddr, nil) }) diff --git a/docs/examples/deploy/k8s/archived-exporter.yaml b/docs/examples/deploy/k8s/archived-exporter.yaml index 6332b4f..17ac541 100644 --- a/docs/examples/deploy/k8s/archived-exporter.yaml +++ b/docs/examples/deploy/k8s/archived-exporter.yaml @@ -55,15 +55,21 @@ spec: memory: 128Mi limits: memory: 1Gi - readinessProbe: + startupProbe: httpGet: - path: /metrics + path: /healthz/startup port: metrics timeoutSeconds: 1 + readinessProbe: + httpGet: + path: /healthz/readiness + port: metrics + timeoutSeconds: 5 livenessProbe: httpGet: - path: /metrics + path: /healthz/liveness port: metrics + timeoutSeconds: 5 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true diff --git a/docs/examples/deploy/k8s/archived-manager.yaml b/docs/examples/deploy/k8s/archived-manager.yaml index e8cf7a9..bec690c 100644 --- a/docs/examples/deploy/k8s/archived-manager.yaml +++ b/docs/examples/deploy/k8s/archived-manager.yaml @@ -63,15 +63,21 @@ spec: memory: 1Gi limits: memory: 1Gi - readinessProbe: + startupProbe: httpGet: - path: /metrics + path: /healthz/startup port: metrics timeoutSeconds: 1 + readinessProbe: + httpGet: + path: /healthz/readiness + port: metrics + timeoutSeconds: 5 livenessProbe: httpGet: - path: /metrics + path: /healthz/liveness port: metrics + timeoutSeconds: 5 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true diff --git a/docs/examples/deploy/k8s/archived-publisher.yaml b/docs/examples/deploy/k8s/archived-publisher.yaml index 0653367..ed9f2de 100644 --- a/docs/examples/deploy/k8s/archived-publisher.yaml +++ b/docs/examples/deploy/k8s/archived-publisher.yaml @@ -63,15 +63,21 @@ spec: memory: 128Mi limits: memory: 1Gi - readinessProbe: + startupProbe: httpGet: - path: /metrics + path: /healthz/startup port: metrics timeoutSeconds: 1 + readinessProbe: + httpGet: + path: /healthz/readiness + port: metrics + timeoutSeconds: 5 livenessProbe: httpGet: - path: /metrics + path: /healthz/liveness port: metrics + timeoutSeconds: 5 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true