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="