Migrate sample-controller to contextual logging

Kubernetes-commit: e346475822604fc41bd38e24a331fc7a8314876a
This commit is contained in:
Prasad Chandrasekaran
2022-11-14 09:16:04 +05:30
committed by Kubernetes Publisher
parent 32a619188a
commit ab7425d650
4 changed files with 76 additions and 54 deletions
+5 -4
View File
@@ -17,6 +17,7 @@ limitations under the License.
package signals
import (
"context"
"os"
"os/signal"
)
@@ -26,18 +27,18 @@ var onlyOneSignalHandler = make(chan struct{})
// 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() (stopCh <-chan struct{}) {
func SetupSignalHandler() context.Context {
close(onlyOneSignalHandler) // panics when called twice
stop := make(chan struct{})
c := make(chan os.Signal, 2)
ctx, cancel := context.WithCancel(context.Background())
signal.Notify(c, shutdownSignals...)
go func() {
<-c
close(stop)
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
return stop
return ctx
}