@@ -21,7 +21,9 @@ import (
2121 "net"
2222 "net/url"
2323 "os"
24+ "os/signal"
2425 "strings"
26+ "syscall"
2527 "time"
2628
2729 "github.com/spf13/cobra"
@@ -134,7 +136,6 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
134136
135137 // This channel is used to ensure all spawned goroutines exit when we exit.
136138 stopCh := make (chan struct {})
137- defer close (stopCh )
138139
139140 // This channel is used to signal Run() something failed and to jump ship.
140141 // It's purely a chan<- in the Daemon struct for goroutines to write to, and
@@ -286,6 +287,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
286287 err = kClient .Get (context .Background (), types.NamespacedName {Namespace : vars .Namespace , Name : consts .DefaultConfigName }, defaultConfig )
287288 if err != nil {
288289 log .Log .Error (err , "Failed to get default SriovOperatorConfig object" )
290+ close (stopCh )
289291 return err
290292 }
291293 featureGates := featuregate .New ()
@@ -294,25 +296,47 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
294296 log .Log .Info ("Enabled featureGates" , "featureGates" , featureGates .String ())
295297
296298 setupLog .V (0 ).Info ("Starting SriovNetworkConfigDaemon" )
297- err = daemon .New (
298- kClient ,
299- snclient ,
300- kubeclient ,
301- hostHelpers ,
302- platformHelper ,
303- exitCh ,
304- stopCh ,
305- syncCh ,
306- refreshCh ,
307- eventRecorder ,
308- featureGates ,
309- startOpts .disabledPlugins ,
310- ).Run (stopCh , exitCh )
311- if err != nil {
312- setupLog .Error (err , "failed to run daemon" )
299+
300+ // create a signal channel to catch interrupts
301+ sigc := make (chan os.Signal , 1 )
302+ signal .Notify (sigc , os .Interrupt )
303+ signal .Notify (sigc , syscall .SIGTERM )
304+
305+ errChan := make (chan error )
306+ defer close (errChan )
307+ go func () {
308+ if err := daemon .New (
309+ kClient ,
310+ snclient ,
311+ kubeclient ,
312+ hostHelpers ,
313+ platformHelper ,
314+ exitCh ,
315+ stopCh ,
316+ syncCh ,
317+ refreshCh ,
318+ eventRecorder ,
319+ featureGates ,
320+ startOpts .disabledPlugins ,
321+ ).Run (stopCh , exitCh ); err != nil {
322+ errChan <- err
323+ }
324+ }()
325+
326+ select {
327+ case err := <- errChan :
328+ // daemon has exited, close the stop channel and return the error
329+ close (stopCh )
330+ return err
331+ case <- sigc :
332+ // signal received, close the stop channel and wait for the daemon to exit
333+ close (stopCh )
334+ if err := <- errChan ; err != nil {
335+ return err
336+ }
313337 }
314338 setupLog .V (0 ).Info ("Shutting down SriovNetworkConfigDaemon" )
315- return err
339+ return nil
316340}
317341
318342// updateDialer instruments a restconfig with a dial. the returned function allows forcefully closing all active connections.
0 commit comments