Skip to content

Commit eb53a4e

Browse files
committed
Add health check; pings the DB
1 parent e19de36 commit eb53a4e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Diff for: cmd/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717

1818
import (
1919
"flag"
20+
"net/http"
2021
"os"
2122

2223
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -125,7 +126,9 @@ func main() {
125126
}
126127
//+kubebuilder:scaffold:builder
127128

128-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
129+
if err := mgr.AddHealthzCheck("healthz", func(req *http.Request) error {
130+
return pg.Ping(req.Context())
131+
}); err != nil {
129132
setupLog.Error(err, "unable to set up health check")
130133
os.Exit(1)
131134
}

Diff for: internal/postgres/mock/postgres.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/postgres/postgres.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postgres
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"log"
@@ -25,6 +26,7 @@ type PG interface {
2526
GetUser() string
2627
GetHost() string
2728
GetDefaultDatabase() string
29+
Ping(ctx context.Context) error
2830
}
2931

3032
type pg struct {
@@ -81,6 +83,10 @@ func (c *pg) GetDefaultDatabase() string {
8183
return c.default_database
8284
}
8385

86+
func (c *pg) Ping(ctx context.Context) error {
87+
return c.db.PingContext(ctx)
88+
}
89+
8490
func GetConnection(user, password, host, database, uri_args string) (*sql.DB, error) {
8591
db, err := sql.Open("postgres", fmt.Sprintf("postgresql://%s:%s@%s/%s?%s", user, password, host, database, uri_args))
8692
if err != nil {

0 commit comments

Comments
 (0)