From 99e41928dd40b5ac964a96057e1068f77a509e2d Mon Sep 17 00:00:00 2001 From: Victor1319 Date: Thu, 21 Aug 2025 10:03:48 +0800 Subject: [PATCH] feat(cmd): add healthy and ready check url. #1000296298 Signed-off-by: Victor1319 (cherry picked from commit 2802e5e4866e7bac4197612d409472a1ccb87725) --- cmd/cmd.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/cmd.go b/cmd/cmd.go index eb5a299cb..cbeeb6685 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -15,6 +15,7 @@ package main import ( + "encoding/json" "flag" "fmt" syslog "log" @@ -404,6 +405,10 @@ func main() { os.Exit(1) } + http.HandleFunc("/healthyz", liveCheck) + http.HandleFunc("/livez", liveCheck) + http.HandleFunc("/readyz", readyCheck) + daemonize.SignalOutcome(nil) // Block main goroutine until server shutdown. @@ -438,3 +443,32 @@ func startDaemon() error { return nil } + +func writeJSONResponse(w http.ResponseWriter, status string, message string) { + resp := map[string]interface{}{ + "status": status, + "message": message, + "time": time.Now().Format(time.RFC3339), + } + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.LogErrorf("failed to encode response, err %s", err.Error()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } +} + +func liveCheck(w http.ResponseWriter, r *http.Request) { + log.LogInfof("livez check request received") + writeJSONResponse(w, "livez", "service is running") + log.LogInfo("livez check response sent successfully") +} + +func readyCheck(w http.ResponseWriter, r *http.Request) { + log.LogInfof("ready check request received") + writeJSONResponse(w, "ready", "service is ready") + log.LogInfo("ready check response sent successfully") +}