Skip to content

Commit f13f0ab

Browse files
committed
Clean Systemd files on exit
If implemented all systemD related files will be cleaned up on exit.
1 parent a85ab70 commit f13f0ab

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

cmd/sriov-network-config-daemon/start.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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,45 @@ 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 and gracefully shutdown the daemon
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+
errChan <- 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)
322+
}()
323+
324+
select {
325+
case err := <-errChan:
326+
// daemon has exited, close the stop channel and return the error
327+
close(stopCh)
328+
return err
329+
case <-sigc:
330+
// signal received, close the stop channel and wait for the daemon to exit
331+
close(stopCh)
332+
if err := <-errChan; err != nil {
333+
return err
334+
}
313335
}
314336
setupLog.V(0).Info("Shutting down SriovNetworkConfigDaemon")
315-
return err
337+
return nil
316338
}
317339

318340
// updateDialer instruments a restconfig with a dial. the returned function allows forcefully closing all active connections.

pkg/daemon/daemon.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error {
207207
for {
208208
select {
209209
case <-stopCh:
210+
// clean files from host if we are running in systemd mode
211+
if vars.UsingSystemdMode {
212+
err := systemd.CleanSriovFilesFromHost(vars.ClusterType == consts.ClusterTypeOpenshift)
213+
if err != nil {
214+
log.Log.Error(err, "failed to remove all the systemd sriov files")
215+
return err
216+
}
217+
}
210218
log.Log.V(0).Info("Run(): stop daemon")
211219
return nil
212220
case err, more := <-exitCh:

0 commit comments

Comments
 (0)