From 4ed81f1977eb9fd21e031e289f696b6ca940c170 Mon Sep 17 00:00:00 2001 From: Laurent Luce Date: Mon, 16 Sep 2024 16:37:09 -0400 Subject: [PATCH] feat: Validate DB connection as part of the api healthcheck (#26) * chore(main): release 0.1.16 (#162) :robot: I have created a release *beep* *boop* --- ## [0.1.16](https://github.com/kurtosis-tech/kardinal-kontrol-private/compare/0.1.15...0.1.16) (2024-08-16) ### Features * allow for templates to work ([#160](https://github.com/kurtosis-tech/kardinal-kontrol-private/issues/160)) ([3f70e0a](https://github.com/kurtosis-tech/kardinal-kontrol-private/commit/3f70e0a001cc4027fb7e17d54c4529152cb491a9)) * RDS cloud formation and DB secrets exposed to kontrol service pods ([#147](https://github.com/kurtosis-tech/kardinal-kontrol-private/issues/147)) ([0457a89](https://github.com/kurtosis-tech/kardinal-kontrol-private/commit/0457a893fb3274e6f4dbd11e4e0786a1c7567035)) ### Bug Fixes * allow for job to run on kontrol private repo ([#170](https://github.com/kurtosis-tech/kardinal-kontrol-private/issues/170)) ([786e7be](https://github.com/kurtosis-tech/kardinal-kontrol-private/commit/786e7be2fb02b89ce296aecd393da81af38c8825)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). * Check DB as part of the healthcheck * Check DB as part of the healthcheck * Update cli api dep --------- Co-authored-by: Edgar Gomes --- kontrol-service/api/server.go | 9 +++++++++ kontrol-service/database/db.go | 18 ++++++++++++++++++ kontrol-service/go.mod | 2 +- kontrol-service/go.sum | 6 ++++-- kontrol-service/gomod2nix.toml | 4 ++-- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/kontrol-service/api/server.go b/kontrol-service/api/server.go index 92772cd..5852cb7 100644 --- a/kontrol-service/api/server.go +++ b/kontrol-service/api/server.go @@ -53,6 +53,15 @@ func (sv *Server) RegisterExternalAndInternalApi(router api.EchoRouter) { } func (sv *Server) GetHealth(_ context.Context, _ api.GetHealthRequestObject) (api.GetHealthResponseObject, error) { + err := sv.db.Check() + if err != nil { + errMsg := "An error occurred checking the database connection" + errResp := api.ErrorJSONResponse{ + Error: err.Error(), + Msg: &errMsg, + } + return api.GetHealth500JSONResponse{ErrorJSONResponse: errResp}, nil + } resp := "ok" return api.GetHealth200JSONResponse(resp), nil } diff --git a/kontrol-service/database/db.go b/kontrol-service/database/db.go index 9676dee..7b2aea8 100644 --- a/kontrol-service/database/db.go +++ b/kontrol-service/database/db.go @@ -169,6 +169,24 @@ func (db *Db) Clear() error { return nil } +func (db *Db) Check() error { + rows, err := db.db.Raw("SELECT 1").Rows() + if err != nil { + return stacktrace.Propagate(err, "An error occurred while checking the DB connection") + } + + defer rows.Close() + rowsCount := 0 + for rows.Next() { + rowsCount += 1 + } + + if rowsCount != 1 { + return stacktrace.Propagate(err, "The SQL query SELECT 1 should have returned a single row and we got instead %d rows", rowsCount) + } + return nil +} + type DatabaseConnectionInfo struct { username string password string diff --git a/kontrol-service/go.mod b/kontrol-service/go.mod index 044f8dd..3b3facd 100644 --- a/kontrol-service/go.mod +++ b/kontrol-service/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.3 require ( github.com/DATA-DOG/go-sqlmock v1.5.2 github.com/dominikbraun/graph v0.23.0 - github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240913221752-e831db545217 + github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240916202910-d86eaa704e51 github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240913221752-e831db545217 github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 github.com/labstack/echo/v4 v4.12.0 diff --git a/kontrol-service/go.sum b/kontrol-service/go.sum index 5b2bd19..a4bace1 100644 --- a/kontrol-service/go.sum +++ b/kontrol-service/go.sum @@ -77,8 +77,10 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240913221752-e831db545217 h1:OLG5CtNffardgtFirHxOcM0LDOZ4qRh/fjTYmx1y7Bo= -github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240913221752-e831db545217/go.mod h1:yvON5b9BHp3yJ99+i+JUMGb985g3h6Eco+w9swS5rds= +github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240916193505-65de8a373686 h1:VFqLrskN+XGGUxudTerwFnQn+XnVXD1jLE29fypNRQw= +github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240916193505-65de8a373686/go.mod h1:yvON5b9BHp3yJ99+i+JUMGb985g3h6Eco+w9swS5rds= +github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240916202910-d86eaa704e51 h1:D2tqOkwf8+Tj/F4u/cKmCPMy4uD0NRQDN1fempg32nY= +github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240916202910-d86eaa704e51/go.mod h1:yvON5b9BHp3yJ99+i+JUMGb985g3h6Eco+w9swS5rds= github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240913221752-e831db545217 h1:cCM7nTd4E6Vh/dXlRgzmyU5I7GBr7D+ImU3iMVx+Dnk= github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240913221752-e831db545217/go.mod h1:lj6oLhpXgnc9ZaulV7jysgRaeZUDPK6XiM2TxpcGRqM= github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g= diff --git a/kontrol-service/gomod2nix.toml b/kontrol-service/gomod2nix.toml index 3b9871e..baa68ba 100644 --- a/kontrol-service/gomod2nix.toml +++ b/kontrol-service/gomod2nix.toml @@ -266,8 +266,8 @@ schema = 3 version = "v0.2.0" hash = "sha256-fadcWxZOORv44oak3jTxm6YcITcFxdGt4bpn869HxUE=" [mod."github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api"] - version = "v0.0.0-20240913221752-e831db545217" - hash = "sha256-Snene5X+e60ztYbCLluEcT8wMtd2w3MOJHGz/KhXr3w=" + version = "v0.0.0-20240916202910-d86eaa704e51" + hash = "sha256-NcQoWjkN4yuRpAnaWb4vgwkpIdzgh+G3ng1P0VH+ynU=" [mod."github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api"] version = "v0.0.0-20240913221752-e831db545217" hash = "sha256-Y1vl6Cqpvc+b9+JPnajRGPeAXqA/XQEgcGBCXsiuTek="