diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index 09f893e7..18f0999d 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -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) +} diff --git a/internal/pkg/leadership/leadership.go b/internal/pkg/leadership/leadership.go index e0f3db55..cdc37663 100644 --- a/internal/pkg/leadership/leadership.go +++ b/internal/pkg/leadership/leadership.go @@ -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 {