72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package checkgrp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"go.uber.org/zap"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
type Handlers struct {
|
|
Build string
|
|
Log *zap.SugaredLogger
|
|
}
|
|
|
|
func (h Handlers) Readiness(w http.ResponseWriter, r *http.Request) {
|
|
data := struct {
|
|
Status string `json:"status"`
|
|
}{
|
|
Status: "ok",
|
|
}
|
|
statusCode := http.StatusOK
|
|
if err := response(w, statusCode, data); err != nil {
|
|
h.Log.Errorw("readiness", "ERROR", err)
|
|
}
|
|
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
|
|
r.URL.Path, "remoteAddr", r.RemoteAddr)
|
|
}
|
|
|
|
func (h Handlers) Liveliness(w http.ResponseWriter, r *http.Request) {
|
|
host, err := os.Hostname()
|
|
if err != nil {
|
|
host = "unavailable"
|
|
}
|
|
data := struct {
|
|
Status string `json:"status"`
|
|
Build string `json:"build"`
|
|
Host string `json:"host"`
|
|
Pod string `json:"pod"`
|
|
PodIP string `json:"pod_ip"`
|
|
Node string `json:"node"`
|
|
Namespace string `json:"namespace"`
|
|
}{
|
|
Status: "up",
|
|
Build: h.Build,
|
|
Host: host,
|
|
Pod: os.Getenv("KUBERNETES_PODNAME"),
|
|
PodIP: os.Getenv("KUBERNETES_NAMESPACE_POD_IP"),
|
|
Node: os.Getenv("KUBERNETES_NODENAME"),
|
|
Namespace: os.Getenv("KUBERNETES_NAMESPACE"),
|
|
}
|
|
statusCode := http.StatusOK
|
|
if err := response(w, statusCode, data); err != nil {
|
|
h.Log.Errorw("liveness", "ERROR", err)
|
|
}
|
|
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
|
|
r.URL.Path, "remoteAddr", r.RemoteAddr)
|
|
}
|
|
|
|
func response(w http.ResponseWriter, statusCode int, data interface{}) error {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w.Header().Set("Content-type", "application/json")
|
|
w.WriteHeader(statusCode)
|
|
|
|
if _, err := w.Write(jsonData); err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|