add healthcheck service
This commit is contained in:
63
cmd/utility/main.go
Normal file
63
cmd/utility/main.go
Normal file
@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
monitorAddress := os.Getenv("DEVSIM_MONITOR_ADDR")
|
||||
if monitorAddress == "" {
|
||||
return
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(monitorAddress)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "provided address is invalid: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
|
||||
if len(os.Args) == 1 {
|
||||
fmt.Fprintf(os.Stderr, "no command provided")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
destinationAddress := net.JoinHostPort(host, port)
|
||||
|
||||
cmd := os.Args[1]
|
||||
var resp *http.Response
|
||||
switch cmd {
|
||||
case "health":
|
||||
resp, err = http.Get("http://" + destinationAddress + "/health")
|
||||
case "ready":
|
||||
resp, err = http.Get("http://" + destinationAddress + "/ready")
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unable to check %s", cmd)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to proceed request %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
code := resp.StatusCode
|
||||
status := resp.Header.Get("X-Readiness-Probe")
|
||||
|
||||
if code != http.StatusOK {
|
||||
println("code not ok")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if status != "" && status != "ok" {
|
||||
println("status not ok")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
println("ok")
|
||||
}
|
||||
Reference in New Issue
Block a user