Skip to content

Commit

Permalink
Merge pull request #3508 from owncloud/health-reference
Browse files Browse the repository at this point in the history
Add reference implementation for health & readiness endpoint in IDP
  • Loading branch information
dragonchaser authored Apr 11, 2022
2 parents 4c7eed8 + 42a4e01 commit 45ae440
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
33 changes: 22 additions & 11 deletions idp/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package debug
import (
"io"
"net/http"
"net/url"

"github.com/owncloud/ocis/idp/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/service/debug"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/owncloud/ocis/ocis-pkg/version"
)

Expand All @@ -21,23 +24,30 @@ func Server(opts ...Option) (*http.Server, error) {
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(health(options.Config)),
debug.Health(health(options.Config, options.Logger)),
debug.Ready(ready(options.Config)),
), nil
}

// health implements the health check.
func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
func health(cfg *config.Config, l log.Logger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
targetHost, err := url.Parse(cfg.Ldap.URI)
if err != nil {
l.Fatal().Err(err).Str("uri", cfg.Ldap.URI).Msg("invalid LDAP URI")
}
err = shared.RunChecklist(shared.TCPConnect(targetHost.Host))
retVal := http.StatusOK
if err != nil {
l.Error().Err(err).Msg("Healtcheck failed")
retVal = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)

// TODO: check if services are up and running
w.WriteHeader(retVal)

_, err := io.WriteString(w, http.StatusText(http.StatusOK))
// io.WriteString should not fail but if it does we want to know.
_, err = io.WriteString(w, http.StatusText(retVal))
if err != nil {
panic(err)
l.Fatal().Err(err).Msg("Could not write health check body")
}
}
}
Expand All @@ -47,9 +57,10 @@ func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)

// TODO: check if services are up and running

// if we can call this function, a http(200) is a valid response as
// there is nothing we can check at this point for IDP
// if there is a mishap when initializing, there is a minimal (talking ms or ns window)
// timeframe where this code is callable
_, err := io.WriteString(w, http.StatusText(http.StatusOK))
// io.WriteString should not fail but if it does we want to know.
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions ocis-pkg/shared/healthchecklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package shared

import "net"

// Check is a single health-check
type Check func() error

// RunChecklist runs all the given checks
func RunChecklist(checks ...Check) error {
for _, c := range checks {
err := c()
if err != nil {
return err
}
}
return nil
}

// TCPConnect connects to a given tcp endpoint
func TCPConnect(host string) Check {
return func() error {
conn, err := net.Dial("tcp", host)
if err != nil {
return err
}
defer conn.Close()
return nil
}
}

0 comments on commit 45ae440

Please sign in to comment.