mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
61b202c64a
* init container to cleanup stale webhook configurations if any. * remove test code * use internal pkg for os signals * move webhook cleanup before http.server shutown. * update make file and remove init * update CI script
43 lines
1,010 B
Go
43 lines
1,010 B
Go
package signal
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
var onlyOneSignalHandler = make(chan struct{})
|
|
var shutdownHandler chan os.Signal
|
|
|
|
// 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
|
|
// is terminated with exit code 1.
|
|
func SetupSignalHandler() <-chan struct{} {
|
|
close(onlyOneSignalHandler) // panics when called twice
|
|
|
|
shutdownHandler = make(chan os.Signal, 2)
|
|
|
|
stop := make(chan struct{})
|
|
signal.Notify(shutdownHandler, shutdownSignals...)
|
|
go func() {
|
|
<-shutdownHandler
|
|
close(stop)
|
|
<-shutdownHandler
|
|
os.Exit(1) // second signal. Exit directly.
|
|
}()
|
|
|
|
return stop
|
|
}
|
|
|
|
// RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT)
|
|
// This returns whether a handler was notified
|
|
func RequestShutdown() bool {
|
|
if shutdownHandler != nil {
|
|
select {
|
|
case shutdownHandler <- shutdownSignals[0]:
|
|
return true
|
|
default:
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|