Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat: Validate DB connection as part of the api healthcheck (#26)
Browse files Browse the repository at this point in the history
* chore(main): release 0.1.16 (#162)

:robot: I have created a release *beep* *boop*
---


##
[0.1.16](kurtosis-tech/kardinal-kontrol-private@0.1.15...0.1.16)
(2024-08-16)


### Features

* allow for templates to work
([#160](kurtosis-tech/kardinal-kontrol-private#160))
([3f70e0a](kurtosis-tech/kardinal-kontrol-private@3f70e0a))
* RDS cloud formation and DB secrets exposed to kontrol service pods
([#147](kurtosis-tech/kardinal-kontrol-private#147))
([0457a89](kurtosis-tech/kardinal-kontrol-private@0457a89))


### Bug Fixes

* allow for job to run on kontrol private repo
([#170](kurtosis-tech/kardinal-kontrol-private#170))
([786e7be](kurtosis-tech/kardinal-kontrol-private@786e7be))

---
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 <[email protected]>
  • Loading branch information
laurentluce and lostbean authored Sep 16, 2024
1 parent 45f1826 commit 4ed81f1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
9 changes: 9 additions & 0 deletions kontrol-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions kontrol-service/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kontrol-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions kontrol-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions kontrol-service/gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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="
Expand Down

0 comments on commit 4ed81f1

Please sign in to comment.