-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
94 lines (83 loc) · 2.48 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package deviceplugin
import (
"os"
"syscall"
"github.com/everpeace/k8s-hostpath-device-plugin/pkg/config"
"github.com/everpeace/k8s-hostpath-device-plugin/pkg/watcher"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
type Runner struct {
cfg config.HostPathDevicePluginConfig
fsWatcher *fsnotify.Watcher
sigCh chan os.Signal
}
func MustNewRunner(
cfg config.HostPathDevicePluginConfig,
) *Runner {
log.Info().Str("Path", pluginapi.DevicePluginPath).Msg("Starting filesystem watcher.")
fsWatcher, err := watcher.NewFSWatcher(pluginapi.DevicePluginPath)
if err != nil {
log.Fatal().Str("Path", pluginapi.DevicePluginPath).Err(err).Msg("Failed to create filesystem watcher")
}
log.Info().Msg("Starting signal watcher.")
sigCh := watcher.NewSignalWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
return &Runner{
cfg: cfg,
fsWatcher: fsWatcher,
sigCh: sigCh,
}
}
func (r *Runner) Run() {
var devicePlugin *HostPathDevicePlugin
restart := true
for {
if restart {
if devicePlugin != nil {
if err := devicePlugin.Stop(); err != nil {
log.Fatal().Err(err).Msg("Failed to stop HostPath device plugin")
}
}
var err error
devicePlugin, err = NewHostPathDevicePlugin(r.cfg)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize HostPath device plugin")
}
if err := devicePlugin.Serve(); err != nil {
log.Error().Err(err).Msg("Failed to start HostPath device plugin")
} else {
restart = false
}
}
select {
case event, ok := <-r.fsWatcher.Events:
if ok {
if event.Name == pluginapi.KubeletSocket && event.Op&fsnotify.Create == fsnotify.Create {
log.Info().
Str("KubeletSocket", pluginapi.KubeletSocket).
Msg("inotify: detected KubeletSocket created. Restarting K8s HostPath Device Plugin")
restart = true
}
}
case err, ok := <-r.fsWatcher.Errors:
if ok {
log.Error().Err(err).Msg("inotify: got error")
}
case s := <-r.sigCh:
switch s {
case syscall.SIGHUP:
log.Info().Str("Signal", s.String()).Msg("Received Signal. Restarting K8s HostPath Device Plugin")
restart = true
default:
log.Info().Str("Signal", s.String()).Msg("Received signal, shutting down")
if err := devicePlugin.Stop(); err != nil {
log.Fatal().Err(err).Msg("Failed to shutdown")
}
r.fsWatcher.Close()
log.Info().Msg("Shutdown successfully")
os.Exit(0)
}
}
}
}