Skip to content

Commit

Permalink
apiserver: add lifecycle signal for preshutdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashem committed May 17, 2022
1 parent c79b909 commit b1f7b60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (s *GenericAPIServer) PrepareRun() preparedGenericAPIServer {
// | |
// (ShutdownDelayDuration) (PreShutdownHooks)
// | |
// AfterShutdownDelayDuration (delayedStopCh) preShutdownHooksHasStoppedCh
// AfterShutdownDelayDuration (delayedStopCh) PreShutdownHooksStopped (preShutdownHooksHasStoppedCh)
// | |
// |---------------------------------- |
// | | |
Expand Down Expand Up @@ -504,12 +504,13 @@ func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error {
}

// pre-shutdown hooks need to finish before we stop the http server
preShutdownHooksHasStoppedCh, stopHttpServerCh := make(chan struct{}), make(chan struct{})
preShutdownHooksHasStoppedCh := s.lifecycleSignals.PreShutdownHooksStopped
stopHttpServerCh := make(chan struct{})
go func() {
defer close(stopHttpServerCh)

<-delayedStopOrDrainedCh
<-preShutdownHooksHasStoppedCh
<-preShutdownHooksHasStoppedCh.Signaled()
}()

stoppedCh, listenerStoppedCh, err := s.NonBlockingRun(stopHttpServerCh, shutdownTimeout)
Expand Down Expand Up @@ -540,7 +541,10 @@ func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error {
// run shutdown hooks directly. This includes deregistering from
// the kubernetes endpoint in case of kube-apiserver.
func() {
defer close(preShutdownHooksHasStoppedCh)
defer func() {
preShutdownHooksHasStoppedCh.Signal()
klog.V(1).InfoS("[graceful-termination] pre-shutdown hooks completed", "name", preShutdownHooksHasStoppedCh.Name())
}()
err = s.RunPreShutdownHooks()
}()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/server/lifecycle_signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ type lifecycleSignals struct {
// ShutdownDelayDuration allows the apiserver to delay shutdown for some time.
AfterShutdownDelayDuration lifecycleSignal

// PreShutdownHooksStopped event is signaled when all registered
// preshutdown hook(s) have finished running.
PreShutdownHooksStopped lifecycleSignal

// InFlightRequestsDrained event is signaled when the existing requests
// in flight have completed. This is used as signal to shut down the audit backends
InFlightRequestsDrained lifecycleSignal
Expand All @@ -143,6 +147,7 @@ func newLifecycleSignals() lifecycleSignals {
return lifecycleSignals{
ShutdownInitiated: newNamedChannelWrapper("ShutdownInitiated"),
AfterShutdownDelayDuration: newNamedChannelWrapper("AfterShutdownDelayDuration"),
PreShutdownHooksStopped: newNamedChannelWrapper("PreShutdownHooksStopped"),
InFlightRequestsDrained: newNamedChannelWrapper("InFlightRequestsDrained"),
HTTPServerStoppedListening: newNamedChannelWrapper("HTTPServerStoppedListening"),
HasBeenReady: newNamedChannelWrapper("HasBeenReady"),
Expand Down

0 comments on commit b1f7b60

Please sign in to comment.