-
Notifications
You must be signed in to change notification settings - Fork 12
/
file.go
90 lines (76 loc) · 2.59 KB
/
file.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
package monitor
import (
"context"
"github.com/fsnotify/fsnotify"
gocommon "github.com/harvester/go-common"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctlv1 "github.com/harvester/node-manager/pkg/generated/controllers/node.harvesterhci.io/v1beta1"
"github.com/harvester/node-manager/pkg/utils"
)
var monitorTargets = []string{utils.SystemdConfigPath}
var timesyncdConfigPath = utils.SystemdConfigPath + utils.TimesyncdConfigName
type ConfigFileMonitor struct {
Context context.Context
MonitorName string
NodeName string
MonitorTargets []string
NodeConfigCtl ctlv1.NodeConfigController
}
func NewConfigFileMonitor(ctx context.Context, nodecfg ctlv1.NodeConfigController, nodeName, monitorName string) *ConfigFileMonitor {
return &ConfigFileMonitor{
Context: ctx,
MonitorName: monitorName,
NodeConfigCtl: nodecfg,
NodeName: nodeName,
MonitorTargets: []string{},
}
}
func (monitor *ConfigFileMonitor) startMonitor() {
go func() {
handler := gocommon.FSNotifyHandlerFunc(func(event fsnotify.Event) {
monitor.genericHandler(event.Name)
})
gocommon.WatchFileChange(monitor.Context, gocommon.AnyOf(gocommon.FSNotifyHandlerFunc(handler), fsnotify.Write), monitorTargets)
}()
}
func (monitor *ConfigFileMonitor) handleNTPConfigChange() {
wantedNTPServers := ""
nodeconfig, err := monitor.NodeConfigCtl.Get(HarvesterNS, monitor.NodeName, metav1.GetOptions{})
if err != nil {
logrus.Warnf("Get NodeConfig fail, err: %v", err)
} else {
wantedNTPServers = nodeconfig.Spec.NTPConfig.NTPServers
logrus.Debugf("Get the wanted NTP Servers: %s", wantedNTPServers)
}
currentNTPServers := getNTPServersOnNode()
logrus.Debugf("Current NTP Servers: %s, Config NTP Servers %s", currentNTPServers, wantedNTPServers)
if wantedNTPServers != "" && wantedNTPServers != currentNTPServers {
logrus.Infof("Enqueue to make controller to update NTP Servers")
monitor.NodeConfigCtl.Enqueue(nodeconfig.Namespace, nodeconfig.Name)
}
}
func (monitor *ConfigFileMonitor) genericHandler(eventName string) {
logrus.Debugf("Prepare to handle the event: %s", eventName)
eventType := parserEventType(eventName)
if eventType != "" {
monitor.doGenericHandler(eventType)
}
}
func (monitor *ConfigFileMonitor) doGenericHandler(eventType string) {
switch eventType {
case "NTP":
monitor.handleNTPConfigChange()
default:
logrus.Errorf("unknown event type: %s", eventType)
}
}
func parserEventType(path string) string {
switch path {
case timesyncdConfigPath:
return "NTP"
default:
logrus.Errorf("unknown supported path: %s", path)
}
return ""
}