Skip to content

Commit

Permalink
Merge pull request #8441 from leseb/signal
Browse files Browse the repository at this point in the history
ceph: signal signals handling with context
  • Loading branch information
satoru-takeuchi committed Aug 24, 2021
2 parents 0e106f7 + 4e2879a commit 6582bac
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/operator/ceph/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6582bac

Please sign in to comment.