diff --git a/controller/controller.go b/controller/controller.go index 0e665e05c..d3463a767 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -35,7 +35,6 @@ import ( "github.com/openziti/xweb/v2" "github.com/openziti/ziti/common/capabilities" "github.com/openziti/ziti/common/concurrency" - "github.com/openziti/ziti/common/health" fabricMetrics "github.com/openziti/ziti/common/metrics" "github.com/openziti/ziti/common/pb/ctrl_pb" "github.com/openziti/ziti/common/profiler" @@ -296,7 +295,7 @@ func (c *Controller) initWeb() { logrus.WithError(err).Fatalf("failed to create health checker") } - if err = c.xweb.GetRegistry().Add(health.NewHealthCheckApiFactory(healthChecker)); err != nil { + if err = c.xweb.GetRegistry().Add(webapis.NewControllerHealthCheckApiFactory(c.env, healthChecker)); err != nil { logrus.WithError(err).Fatalf("failed to create health checks api factory") } @@ -313,10 +312,6 @@ func (c *Controller) initWeb() { logrus.WithError(err).Fatalf("failed to create single page application factory") } - if err = c.xweb.GetRegistry().Add(webapis.NewControllerHealthCheckApiFactory(c.env, healthChecker)); err != nil { - logrus.WithError(err).Fatalf("failed to create controller-is-leader api factory") - } - if c.IsEdgeEnabled() { managementApiFactory := webapis.NewManagementApiFactory(c.env) clientApiFactory := webapis.NewClientApiFactory(c.env) @@ -667,8 +662,6 @@ func getApiPath(binding string) string { return "health-checks" case "edge-oidc": return "/oidc" - case "controller-health": - return "/controller/health" } return "" diff --git a/controller/webapis/controller-health.go b/controller/webapis/controller-health.go index 7897f3f4e..dbfaf6531 100644 --- a/controller/webapis/controller-health.go +++ b/controller/webapis/controller-health.go @@ -51,19 +51,37 @@ func (factory ControllerHealthCheckApiFactory) Binding() string { } func (factory ControllerHealthCheckApiFactory) New(_ *xweb.ServerConfig, options map[interface{}]interface{}) (xweb.ApiHandler, error) { - return &ControllerHealthCheckApiHandler{ - healthChecker: factory.healthChecker, - appEnv: factory.appEnv, + healthCheckApiHandler, err := NewControllerHealthCheckApiHandler(factory.healthChecker, factory.appEnv, options) + + if err != nil { + return nil, err + } + + return healthCheckApiHandler, nil + +} + +func NewControllerHealthCheckApiHandler(healthChecker gosundheit.Health, appEnv *env.AppEnv, options map[interface{}]interface{}) (*ControllerHealthCheckApiHandler, error) { + healthCheckApi := &ControllerHealthCheckApiHandler{ + healthChecker: healthChecker, + appEnv: appEnv, options: options, - }, nil + } + if value, found := options["enableRaftControllerCheck"]; found { + if f, ok := value.(bool); ok { + healthCheckApi.enableRaftControllerCheck = f + } + } + return healthCheckApi, nil } type ControllerHealthCheckApiHandler struct { - handler http.Handler - options map[interface{}]interface{} - appEnv *env.AppEnv - healthChecker gosundheit.Health + handler http.Handler + options map[interface{}]interface{} + appEnv *env.AppEnv + enableRaftControllerCheck bool + healthChecker gosundheit.Health } func (self ControllerHealthCheckApiHandler) Binding() string { @@ -75,7 +93,7 @@ func (self ControllerHealthCheckApiHandler) Options() map[interface{}]interface{ } func (self ControllerHealthCheckApiHandler) RootPath() string { - return "/controller/health" + return "/health-checks" } func (self ControllerHealthCheckApiHandler) IsHandler(r *http.Request) bool { @@ -94,13 +112,7 @@ func (self *ControllerHealthCheckApiHandler) ServeHTTP(w http.ResponseWriter, re encoder.SetIndent("", " ") results, healthy := self.healthChecker.Results() - isLeader := self.appEnv.GetHostController().IsRaftLeader() - isRaftEnabled := self.appEnv.GetHostController().IsRaftEnabled() - raftData := map[string]interface{}{} data["healthy"] = healthy - raftData["isLeader"] = isLeader - raftData["isRaftEnabled"] = isRaftEnabled - output["raft"] = raftData var checks []map[string]interface{} shortFormat := request.URL.Query().Get("type") == "short" @@ -127,9 +139,18 @@ func (self *ControllerHealthCheckApiHandler) ServeHTTP(w http.ResponseWriter, re } } data["checks"] = checks + if self.enableRaftControllerCheck { + isRaftEnabled := self.appEnv.GetHostController().IsRaftEnabled() + isLeader := self.appEnv.GetHostController().IsRaftLeader() + + if !isLeader && isRaftEnabled { + w.WriteHeader(429) + } - if !isLeader && isRaftEnabled { - w.WriteHeader(429) + raftData := map[string]interface{}{} + raftData["isRaftEnabled"] = isRaftEnabled + raftData["isLeader"] = isLeader + output["raft"] = raftData } if err := encoder.Encode(output); err != nil { diff --git a/controller/webapis/versions.go b/controller/webapis/versions.go index 9bbc3c7b6..50557853e 100644 --- a/controller/webapis/versions.go +++ b/controller/webapis/versions.go @@ -24,7 +24,7 @@ const ( RestApiRootPath = "/edge" ClientRestApiBase = "/edge/client" ManagementRestApiBase = "/edge/management" - ControllerHealthCheck = "/controller/health" + ControllerHealthCheck = "/health-checks" LegacyClientRestApiBaseUrlV1 = RestApiRootPath + RestApiV1 ClientRestApiBaseUrlV1 = ClientRestApiBase + RestApiV1 @@ -41,7 +41,7 @@ const ( ClientApiBinding = "edge-client" ManagementApiBinding = "edge-management" OidcApiBinding = "edge-oidc" - ControllerHealthCheckApiBinding = "controller-health" + ControllerHealthCheckApiBinding = "health-checks" ) // AllApiBindingVersions is a map of: API Binding -> Api Version -> API Path