From 4e2879a15476d12bace38ce27a95b1bd35ff9282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Fri, 30 Jul 2021 11:08:07 +0200 Subject: [PATCH] ceph: signal signals handling with context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As of Golang 1.16, we can use `signal.NotifyContext()` from the signal package. Essentially it combines signal handling for termination as well as canceling the context to stop any ongoing operations. Signed-off-by: Sébastien Han --- pkg/operator/ceph/operator.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/operator/ceph/operator.go b/pkg/operator/ceph/operator.go index 4bfd87740..334afcdf2 100644 --- a/pkg/operator/ceph/operator.go +++ b/pkg/operator/ceph/operator.go @@ -60,6 +60,10 @@ var ( // ImmediateRetryResult Return this for a immediate retry of the reconciliation loop with the same request object. ImmediateRetryResult = reconcile.Result{Requeue: true} + + // Signals to watch for to terminate the operator gracefully + // Using os.Interrupt is more portable across platforms instead of os.SIGINT + shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM} ) // Operator type for managing storage @@ -127,8 +131,9 @@ func (o *Operator) Run() error { } opcontroller.SetCephCommandsTimeout(o.context) - // creating a context - stopContext, stopFunc := context.WithCancel(context.Background()) + + // Initialize signal handler and context + stopContext, stopFunc := signal.NotifyContext(context.Background(), shutdownSignals...) defer stopFunc() rookDiscover := discover.New(o.context.Clientset) @@ -152,10 +157,8 @@ func (o *Operator) Run() error { return errors.Wrap(err, "failed to get server version") } - // Initialize signal handler - signalChan := make(chan os.Signal, 1) + // Initialize stop channel for watchers stopChan := make(chan struct{}) - signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) // For Flex Driver, run volume provisioner for each of the supported configurations if opcontroller.FlexDriverEnabled(o.context) { @@ -191,8 +194,8 @@ func (o *Operator) Run() error { // Signal handler to stop the operator for { select { - case <-signalChan: - logger.Info("shutdown signal received, exiting...") + case <-stopContext.Done(): + logger.Infof("shutdown signal received, exiting... %v", stopContext.Err()) o.cleanup(stopChan) return nil case err := <-mgrErrorChan: