Skip to content

Commit

Permalink
feat: make healtcheck controller specific
Browse files Browse the repository at this point in the history
  • Loading branch information
nenkoru committed Dec 24, 2024
1 parent f8637fb commit 663a948
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
9 changes: 1 addition & 8 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand All @@ -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)
Expand Down Expand Up @@ -667,8 +662,6 @@ func getApiPath(binding string) string {
return "health-checks"
case "edge-oidc":
return "/oidc"
case "controller-health":
return "/controller/health"
}

return ""
Expand Down
55 changes: 38 additions & 17 deletions controller/webapis/controller-health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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"

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions controller/webapis/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 663a948

Please sign in to comment.