Add liveness probe

This commit is contained in:
Alex Vest
2022-10-04 16:41:34 +01:00
parent b7e83b74d8
commit d34c99baf4
2 changed files with 20 additions and 4 deletions
+18 -3
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
@@ -20,6 +21,11 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
// Used for liveness probe
healthy bool = true
)
// NewReloaderCommand starts the reloader controller
func NewReloaderCommand() *cobra.Command {
cmd := &cobra.Command{
@@ -164,11 +170,11 @@ func startReloader(cmd *cobra.Command, args []string) {
lock := leadership.GetNewLock(clientset, constants.LockName, podName, podNamespace)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
leadership.RunLeaderElection(lock, ctx, cancel, podName, controllers)
return
leadership.RunLeaderElection(lock, ctx, cancel, podName, controllers, healthy)
}
select {}
http.HandleFunc("/live", healthz)
logrus.Fatal(http.ListenAndServe(":8080", nil))
}
func getIgnoredNamespacesList(cmd *cobra.Command) (util.List, error) {
@@ -203,3 +209,12 @@ func getIgnoredResourcesList(cmd *cobra.Command) (util.List, error) {
return ignoredResourcesList, nil
}
func healthz(w http.ResponseWriter, req *http.Request) {
if healthy {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusInternalServerError)
}
+2 -1
View File
@@ -26,7 +26,7 @@ func GetNewLock(clientset *kubernetes.Clientset, lockName, podname, namespace st
}
// runLeaderElection runs leadership election. If an instance of the controller is the leader and stops leading it will shutdown.
func RunLeaderElection(lock *resourcelock.LeaseLock, ctx context.Context, cancel context.CancelFunc, id string, controllers []*controller.Controller) {
func RunLeaderElection(lock *resourcelock.LeaseLock, ctx context.Context, cancel context.CancelFunc, id string, controllers []*controller.Controller, health bool) {
// Construct channels for the controllers to use
var stopChannels []chan struct{}
for i := 0; i < len(controllers); i++ {
@@ -49,6 +49,7 @@ func RunLeaderElection(lock *resourcelock.LeaseLock, ctx context.Context, cancel
logrus.Info("no longer leader, shutting down")
stopControllers(stopChannels)
cancel()
health = false
},
OnNewLeader: func(current_id string) {
if current_id == id {