Skip to content

Commit

Permalink
Add /_/health endpoint to watchdog
Browse files Browse the repository at this point in the history
Introduce new endpoint `/_/health` to watchdog for health status of
functions  which check for `/tmp/.lock` file

Fixes first part of openfaas#547 issue.

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
  • Loading branch information
viveksyngh committed Mar 18, 2018
1 parent 30cf8de commit 29dd04a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
43 changes: 43 additions & 0 deletions watchdog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,48 @@ func getAdditionalEnvs(config *WatchdogConfig, r *http.Request, method string) [
return envs
}

func lockFilePresent() bool {
path := filepath.Join(os.TempDir(), ".lock")
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}

func createLockFile() error {
path := filepath.Join(os.TempDir(), ".lock")
log.Printf("Writing lock-file to: %s\n", path)
writeErr := ioutil.WriteFile(path, []byte{}, 0660)
return writeErr
}

func removeLockFile() error {
path := filepath.Join(os.TempDir(), ".lock")
log.Printf("Removing lock-file : %s\n", path)
removeErr := os.Remove(path)
return removeErr
}

func makeHealthHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
if lockFilePresent() == false {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)

}

}
}

func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
Expand Down Expand Up @@ -290,6 +332,7 @@ func main() {
MaxHeaderBytes: 1 << 20, // Max header of 1MB
}

http.HandleFunc("/_/health", makeHealthHandler())
http.HandleFunc("/", makeRequestHandler(&config))

if config.suppressLock == false {
Expand Down
66 changes: 66 additions & 0 deletions watchdog/requesthandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,69 @@ func TestHandler_StatusOKForGETAndNoBody(t *testing.T) {
status, required)
}
}

func TestHealthHandler_SatusOK_LockFilePresent(t *testing.T) {
rr := httptest.NewRecorder()

if lockFilePresent() == false {
if err := createLockFile(); err != nil {
t.Fatal(err)
}
}

req, err := http.NewRequest("GET", "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)

required := http.StatusOK
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code: got %v, but wanted %v", status, required)
}

}

func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T) {
rr := httptest.NewRecorder()

if lockFilePresent() == true {
if err := removeLockFile(); err != nil {
t.Fatal(err)
}
}

req, err := http.NewRequest("GET", "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)

required := http.StatusInternalServerError
if status := rr.Code; status != required {
t.Errorf("handler retruned wrong status code: got %v, but wanted %v", status, required)
}
}

func TestHealthHandler_SatusMethoNotAllowed_ForWriteableVerbs(t *testing.T) {
rr := httptest.NewRecorder()

verbs := []string{"POST", "PUT", "UPDATE", "DELETE"}

for _, verb := range verbs {
req, err := http.NewRequest(verb, "/_/health", nil)
if err != nil {
t.Fatal(err)
}

handler := makeHealthHandler()
handler(rr, req)

required := http.StatusMethodNotAllowed
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code: got %v, but wanted %v", status, required)
}
}
}

0 comments on commit 29dd04a

Please sign in to comment.