From f59dd2d95e6e48e6d1fee86205267279ebb19ae0 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Sat, 20 May 2023 16:38:21 +0000 Subject: [PATCH] Add /readyz for kube-scheduler /readyz contains `sched-handler-sync`, `leaderElection` (when election is enabled) and `shutdown` checks Kubernetes-commit: 44c08fdbd592b7c167ad0c3b0b4a3b52b99c116f --- pkg/server/healthz.go | 21 +-------------------- pkg/server/healthz/healthz.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/server/healthz.go b/pkg/server/healthz.go index f4b564c48..96da308e8 100644 --- a/pkg/server/healthz.go +++ b/pkg/server/healthz.go @@ -118,7 +118,7 @@ func (s *GenericAPIServer) AddLivezChecks(delay time.Duration, checks ...healthz // that we can register that the api-server is no longer ready while we attempt to gracefully // shutdown. func (s *GenericAPIServer) addReadyzShutdownCheck(stopCh <-chan struct{}) error { - return s.AddReadyzChecks(shutdownCheck{stopCh}) + return s.AddReadyzChecks(healthz.NewShutdownHealthz(stopCh)) } // installHealthz creates the healthz endpoint for this server @@ -139,25 +139,6 @@ func (s *GenericAPIServer) installLivez() { s.livezRegistry.installHandler(s.Handler.NonGoRestfulMux) } -// shutdownCheck fails if the embedded channel is closed. This is intended to allow for graceful shutdown sequences -// for the apiserver. -type shutdownCheck struct { - StopCh <-chan struct{} -} - -func (shutdownCheck) Name() string { - return "shutdown" -} - -func (c shutdownCheck) Check(req *http.Request) error { - select { - case <-c.StopCh: - return fmt.Errorf("process is shutting down") - default: - } - return nil -} - // delayedHealthCheck wraps a health check which will not fail until the explicitly defined delay has elapsed. This // is intended for use primarily for livez health checks. func delayedHealthCheck(check healthz.HealthChecker, clock clock.Clock, delay time.Duration) healthz.HealthChecker { diff --git a/pkg/server/healthz/healthz.go b/pkg/server/healthz/healthz.go index 4613fd6d1..76f5745b3 100644 --- a/pkg/server/healthz/healthz.go +++ b/pkg/server/healthz/healthz.go @@ -105,6 +105,29 @@ func (i *informerSync) Name() string { return "informer-sync" } +type shutdown struct { + stopCh <-chan struct{} +} + +// NewShutdownHealthz returns a new HealthChecker that will fail if the embedded channel is closed. +// This is intended to allow for graceful shutdown sequences. +func NewShutdownHealthz(stopCh <-chan struct{}) HealthChecker { + return &shutdown{stopCh} +} + +func (s *shutdown) Name() string { + return "shutdown" +} + +func (s *shutdown) Check(req *http.Request) error { + select { + case <-s.stopCh: + return fmt.Errorf("process is shutting down") + default: + } + return nil +} + func (i *informerSync) Check(_ *http.Request) error { stopCh := make(chan struct{}) // Close stopCh to force checking if informers are synced now.