make Kubelet bootstrap certificate signal aware

Kubernetes-commit: db7194177538d5efc84af621c4400174a2836b3b
This commit is contained in:
chenjun.cj 2020-07-04 12:08:04 +08:00 committed by Kubernetes Publisher
parent b7c5b5ca79
commit 0cdc30602c
1 changed files with 13 additions and 3 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package server package server
import ( import (
"context"
"os" "os"
"os/signal" "os/signal"
) )
@ -27,21 +28,30 @@ var shutdownHandler chan os.Signal
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned // SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
// which is closed on one of these signals. If a second signal is caught, the program // which is closed on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1. // is terminated with exit code 1.
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
// be called once.
func SetupSignalHandler() <-chan struct{} { func SetupSignalHandler() <-chan struct{} {
return SetupSignalContext().Done()
}
// SetupSignalContext is same as SetupSignalHandler, but a context.Context is returned.
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
// be called once.
func SetupSignalContext() context.Context {
close(onlyOneSignalHandler) // panics when called twice close(onlyOneSignalHandler) // panics when called twice
shutdownHandler = make(chan os.Signal, 2) shutdownHandler = make(chan os.Signal, 2)
stop := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background())
signal.Notify(shutdownHandler, shutdownSignals...) signal.Notify(shutdownHandler, shutdownSignals...)
go func() { go func() {
<-shutdownHandler <-shutdownHandler
close(stop) cancel()
<-shutdownHandler <-shutdownHandler
os.Exit(1) // second signal. Exit directly. os.Exit(1) // second signal. Exit directly.
}() }()
return stop return ctx
} }
// RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT) // RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT)