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

feat: Validate DB connection as part of the api healthcheck #26

Merged
merged 17 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
ac69396
chore(main): release 0.1.16 (#162)
lostbean Aug 16, 2024
ce473fb
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 20, 2024
0342914
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 20, 2024
5656d78
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 20, 2024
8711eae
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 21, 2024
68b578a
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 22, 2024
1abf992
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 22, 2024
ca3a89f
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 24, 2024
3d0d2da
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 26, 2024
2619d30
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 27, 2024
53f5048
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 28, 2024
24cb56b
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Aug 30, 2024
7864c74
Check DB as part of the healthcheck
laurentluce Sep 16, 2024
bf8c93f
Check DB as part of the healthcheck
laurentluce Sep 16, 2024
0c6614c
Merge branch 'main' of github.com:kurtosis-tech/kardinal-kontrol
laurentluce Sep 16, 2024
483dff0
Merge branch 'main' into laurent/healthcheck
laurentluce Sep 16, 2024
14df6d4
Update cli api dep
laurentluce Sep 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading