Forráskód Böngészése

add healthchecks to the provisioner service

Alexander Belanger 4 éve
szülő
commit
e77dd41b0e

+ 23 - 0
provisioner/server/handlers/healthcheck/livez.go

@@ -0,0 +1,23 @@
+package healthcheck
+
+import (
+	"net/http"
+
+	"github.com/porter-dev/porter/provisioner/server/config"
+)
+
+type LivezHandler struct {
+	Config *config.Config
+}
+
+func NewLivezHandler(
+	config *config.Config,
+) *LivezHandler {
+	return &LivezHandler{
+		Config: config,
+	}
+}
+
+func (c *LivezHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	writeHealthy(w)
+}

+ 42 - 0
provisioner/server/handlers/healthcheck/readyz.go

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

+ 4 - 0
provisioner/server/router/router.go

@@ -7,6 +7,7 @@ import (
 	"github.com/porter-dev/porter/provisioner/server/authz"
 	"github.com/porter-dev/porter/provisioner/server/config"
 	"github.com/porter-dev/porter/provisioner/server/handlers/credentials"
+	"github.com/porter-dev/porter/provisioner/server/handlers/healthcheck"
 	"github.com/porter-dev/porter/provisioner/server/handlers/provision"
 	"github.com/porter-dev/porter/provisioner/server/handlers/state"
 )
@@ -24,6 +25,9 @@ func NewAPIRouter(config *config.Config) *chi.Mux {
 		porterTokenAuth := authn.NewAuthNPorterTokenFactory(config)
 		workspaceAuth := authz.NewWorkspaceScopedFactory(config)
 
+		r.Method("GET", "/readyz", healthcheck.NewReadyzHandler(config))
+		r.Method("GET", "/livez", healthcheck.NewLivezHandler(config))
+
 		r.Group(func(r chi.Router) {
 			// This group is meant to be called via a provisioner pod
 			r.Group(func(r chi.Router) {