Alexander Belanger 4 лет назад
Родитель
Сommit
fc01dcc4d9

+ 26 - 0
api/server/handlers/healthcheck/livez.go

@@ -0,0 +1,26 @@
+package healthcheck
+
+import (
+	"net/http"
+
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/config"
+)
+
+type LivezHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewLivezHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *LivezHandler {
+	return &LivezHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (v *LivezHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	writeHealthy(w)
+}

+ 45 - 0
api/server/handlers/healthcheck/readyz.go

@@ -0,0 +1,45 @@
+package healthcheck
+
+import (
+	"net/http"
+
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/apierrors"
+	"github.com/porter-dev/porter/api/server/shared/config"
+)
+
+type ReadyzHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewReadyzHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ReadyzHandler {
+	return &ReadyzHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (v *ReadyzHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	db, err := v.Config().DB.DB()
+
+	if err != nil {
+		v.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	if err := db.Ping(); err != nil {
+		v.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	writeHealthy(w)
+}
+
+func writeHealthy(w http.ResponseWriter) {
+	w.Header().Set("Content-Type", "text/plain")
+	w.WriteHeader(http.StatusOK)
+	w.Write([]byte("."))
+}

+ 47 - 0
api/server/router/base.go

@@ -3,6 +3,7 @@ package router
 import (
 	"github.com/go-chi/chi"
 	"github.com/porter-dev/porter/api/server/handlers/gitinstallation"
+	"github.com/porter-dev/porter/api/server/handlers/healthcheck"
 	"github.com/porter-dev/porter/api/server/handlers/metadata"
 	"github.com/porter-dev/porter/api/server/handlers/release"
 	"github.com/porter-dev/porter/api/server/handlers/user"
@@ -27,6 +28,52 @@ func GetBaseRoutes(
 ) []*Route {
 	routes := make([]*Route, 0)
 
+	// GET /api/readyz -> healthcheck.NewReadyzHandler
+	getReadyzEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/readyz",
+			},
+		},
+	)
+
+	getReadyzHandler := healthcheck.NewReadyzHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: getReadyzEndpoint,
+		Handler:  getReadyzHandler,
+		Router:   r,
+	})
+
+	// GET /api/livez -> healthcheck.NewLivezHandler
+	getLivezEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/livez",
+			},
+		},
+	)
+
+	getLivezHandler := healthcheck.NewLivezHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: getLivezEndpoint,
+		Handler:  getLivezHandler,
+		Router:   r,
+	})
+
 	// GET /api/capabilities -> user.NewUserCreateHandler
 	getMetadataEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{

+ 4 - 0
api/server/shared/config/config.go

@@ -13,6 +13,7 @@ import (
 	"github.com/porter-dev/porter/internal/oauth"
 	"github.com/porter-dev/porter/internal/repository"
 	"golang.org/x/oauth2"
+	"gorm.io/gorm"
 )
 
 type Config struct {
@@ -77,6 +78,9 @@ type Config struct {
 	// IngressAgent is the kubernetes client responsible for creating new ingress
 	// resources
 	IngressAgent *kubernetes.Agent
+
+	// DB is the gorm DB instance
+	DB *gorm.DB
 }
 
 type ConfigLoader interface {

+ 2 - 0
api/server/shared/config/loader/loader.go

@@ -53,6 +53,8 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 		return nil, err
 	}
 
+	res.DB = db
+
 	err = gorm.AutoMigrate(db)
 
 	if err != nil {

+ 5 - 5
docs/developing/backend-refactor-status.md

@@ -15,7 +15,7 @@
 | <li>- [X] `GET /api/integrations/helm`                                                                                      | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/registry`                                                                                  | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/repo`                                                                                      | N/A         |                 |             |                  |
-| <li>- [ ] `GET /api/livez`                                                                                                  |             |                 |             |                  |
+| <li>- [X] `GET /api/livez`                                                                                                  | AB          |                 |             |                  |
 | <li>- [x] `POST /api/login`                                                                                                 | AB          |                 |             | yes              |
 | <li>- [x] `POST /api/logout`                                                                                                | AB          |                 |             | yes              |
 | <li>- [X] `GET /api/oauth/digitalocean/callback`                                                                            | AB          |                 |             |                  |
@@ -57,9 +57,9 @@
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/contents`         | AB          |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/procfile`         | AB          |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/gitrepos/{installation_id}/repos/{kind}/{owner}/{name}/{branch}/tarball_url`      | AB          |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/helmrepos`                                                                        |             |                 |             |                  |
-| <li>- [ ] `POST /api/projects/{project_id}/helmrepos`                                                                       |             |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/helmrepos/{helm_id}/charts`                                                       |             |                 |             |                  |
+| <li>- [X] `GET /api/projects/{project_id}/helmrepos`                                                                        | N/A         |                 |             |                  |
+| <li>- [X] `POST /api/projects/{project_id}/helmrepos`                                                                       | N/A         |                 |             |                  |
+| <li>- [X] `GET /api/projects/{project_id}/helmrepos/{helm_id}/charts`                                                       | N/A         |                 |             |                  |
 | <li>- [x] `GET /api/projects/{project_id}/infra`                                                                            | AS          |                 |             | yes              |
 | <li>- [x] `POST /api/projects/{project_id}/integrations/aws`                                                                | AS          |                 | yes         | yes              |
 | <li>- [x] `POST /api/projects/{project_id}/integrations/aws/{aws_integration_id}/overwrite`                                 | AS          | yes             |             | yes              |
@@ -142,7 +142,7 @@
 | <li>- [x] `GET /api/projects/{project_id}/slack_integrations`                                                               | AS          |                 |             | yes              |
 | <li>- [x] `GET /api/projects/{project_id}/slack_integrations/exists`                                                        | AS          |                 |             | yes              |
 | <li>- [x] `DELETE /api/projects/{project_id}/slack_integrations/{slack_integration_id}`                                     | AS          |                 |             | yes              |
-| <li>- [ ] `GET /api/readyz`                                                                                                 |             |                 |             |                  |
+| <li>- [X] `GET /api/readyz`                                                                                                 | AB          |                 |             |                  |
 | <li>- [X] `GET /api/templates`                                                                                              | AB          |                 |             |                  |
 | <li>- [X] `GET /api/templates/upgrade_notes/{name}/{version}`                                                               | AB          | yes             |             |                  |
 | <li>- [X] `GET /api/templates/{name}/{version}`                                                                             | AB          |                 |             |                  |