-
Notifications
You must be signed in to change notification settings - Fork 5
/
monitor.go
147 lines (119 loc) · 3.24 KB
/
monitor.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/golang/glog"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
type PluginState int
const (
PluginIdle PluginState = 0
PluginRunning PluginState = 1
PluginRestarting PluginState = 2
pluginStartRetryTimeout = 3 * time.Second
)
type IPluginState interface {
state() PluginState
setState(PluginState)
}
type NitroEnclavesPluginMonitor struct {
pluginState PluginState
devicePlugin IBasicDevicePlugin
fsWatcher *fsnotify.Watcher
sigWatcher chan os.Signal
devicePluginPath string
kubeletSocketName string
IPluginState
}
func (ps PluginState) String() string {
switch ps {
case PluginIdle:
return "Idle"
case PluginRestarting:
return "Restarting"
case PluginRunning:
return "Running"
default:
return "Unknown"
}
}
func (nepm *NitroEnclavesPluginMonitor) state() PluginState {
return nepm.pluginState
}
func (nepm *NitroEnclavesPluginMonitor) setState(newState PluginState) {
nepm.pluginState = newState
}
func (nepm *NitroEnclavesPluginMonitor) Init() error {
glog.V(0).Info("Creating plugin monitor...")
nepm.setState(PluginIdle)
var err error
if nepm.fsWatcher, err = fsnotify.NewWatcher(); err != nil {
glog.Error("Error while creating file system watcher!")
return err
}
if err = nepm.fsWatcher.Add(nepm.devicePluginPath); err != nil {
glog.Errorf("Error while accessing: %s", pluginapi.DevicePluginPath)
defer nepm.fsWatcher.Close()
return err
}
nepm.sigWatcher = make(chan os.Signal, 1)
signal.Notify(nepm.sigWatcher, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
glog.V(0).Info("Plugin monitor has been successfully created.")
return nil
}
func run(nepm *NitroEnclavesPluginMonitor) bool {
cont := true
if nepm.state() != PluginRunning {
if err := nepm.devicePlugin.Start(); err != nil {
// Sleep and try again as long as the monitor is running.
time.Sleep(pluginStartRetryTimeout)
return cont
}
}
nepm.setState(PluginRunning)
glog.V(0).Infof("New plugin state is: %v.", nepm.state())
L:
select {
case fsEvent := <-nepm.fsWatcher.Events:
//glog.V(0).Info("FS EVENT: ", fsEvent)
if fsEvent.Name == nepm.kubeletSocketName {
if fsEvent.Op&fsnotify.Create == fsnotify.Create {
glog.V(0).Infof("Kubelet sock has been re/created. The plugin needs a restart.")
nepm.setState(PluginRestarting)
break L
}
}
case sig := <-nepm.sigWatcher:
switch sig {
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
glog.V(0).Infof("Terminating plugin monitor... (Reason: \"%v\")", sig)
nepm.devicePlugin.Stop()
cont = false
break L
}
}
return cont
}
func (nepm *NitroEnclavesPluginMonitor) Run() {
defer nepm.fsWatcher.Close()
for ever := true; ever; {
ever = run(nepm)
}
}
// Create a new plugin monitor.
func NewNitroEnclavesMonitor(nedp *NitroEnclavesDevicePlugin) *NitroEnclavesPluginMonitor {
nepm := &NitroEnclavesPluginMonitor{
devicePlugin: nedp,
devicePluginPath: pluginapi.DevicePluginPath,
kubeletSocketName: pluginapi.KubeletSocket,
}
if nepm.Init() != nil {
nepm = nil
}
return nepm
}