Feature: adding support for k8s health endpoints (#191)

* adding support for k8s health endpoints
* fix: update readyz if mqtt is lost
This commit is contained in:
Tobias Lindberg
2024-07-24 22:31:44 +02:00
committed by GitHub
parent 7220454ec0
commit fa4736e509
3 changed files with 40 additions and 0 deletions
+8
View File
@@ -190,6 +190,9 @@ func startMQTT() (*statusCache, error) {
s.topicScan = fmt.Sprintf("teslamate%s/cars/%%d/%%s", getMQTTNameSpace())
// setting readyz endpoint to true (when using MQTT)
isReady.Store(true)
// Thats all - newMessage will be called when something new arrives
return &s, nil
}
@@ -215,12 +218,17 @@ func (s *statusCache) connectedHandler(c mqtt.Client) {
}
log.Println("[info] subscribed to: " + topic)
// setting readyz endpoint to true (when using MQTT)
isReady.Store(true)
}
// connectionLost - called by mqtt package when the connection get lost
func (s *statusCache) connectionLost(c mqtt.Client, err error) {
log.Println("[error] MQTT connection lost: " + err.Error())
s.mqttConnected = false
// setting readyz endpoint to false (when using MQTT)
isReady.Store(false)
}
// newMessage - called by mqtt package when new message received
+30
View File
@@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"strconv"
"sync/atomic"
"time"
"github.com/gin-contrib/gzip"
@@ -17,6 +18,9 @@ import (
)
var (
// application readyz endpoint value for k8s
isReady *atomic.Value
// setting TeslaMateApi version number
apiVersion = "unspecified"
@@ -32,6 +36,9 @@ var (
// main function
func main() {
// setup of readyness endpoint code
isReady := &atomic.Value{}
isReady.Store(false)
// setting log parameters
log.SetFlags(log.Ldate | log.Lmicroseconds)
@@ -141,6 +148,10 @@ func main() {
// /api/ping endpoint
api.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) })
// health endpoints for kubernetes
api.GET("/healthz", healthz)
api.GET("/readyz", readyz)
}
// TeslaMateApi endpoints (before versioning)
@@ -161,6 +172,11 @@ func main() {
Handler: r,
}
// setting readyz endpoint to true (if not using MQTT)
if getEnvAsBool("DISABLE_MQTT", false) {
isReady.Store(true)
}
// graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
@@ -428,3 +444,17 @@ func checkArrayContainsString(s []string, e string) bool {
}
return false
}
// healthz is a liveness probe.
func healthz(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK)})
}
// readyz is a readiness probe.
func readyz(c *gin.Context) {
if isReady == nil || !isReady.Load().(bool) {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": http.StatusText(http.StatusServiceUnavailable)})
return
}
TeslaMateAPIHandleSuccessResponse(c, "webserver", gin.H{"status": http.StatusText(http.StatusOK)})
}