-
Notifications
You must be signed in to change notification settings - Fork 417
/
util.go
123 lines (113 loc) · 3.63 KB
/
util.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
package fileutil
import (
"fmt"
"os"
"strconv"
"time"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
)
type FileChangeCallBack interface {
CallBackForFileLoad(dynamicContent []byte) error
CallBackForFileDeletion() error
}
func waitUntilFileAvailable(filename string, stopCh <-chan struct{}) {
if _, err := os.Stat(filename); err == nil {
return
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-stopCh:
logrus.Infof("startLoadDynamicFile: waitUntilFileAvailable exit because get stopCh, filename is %s", filename)
return
case <-ticker.C:
if _, err := os.Stat(filename); err == nil {
return
}
}
}
}
func loadDynamicFile(filename string, stopCh <-chan struct{}) ([]byte, error) {
waitUntilFileAvailable(filename, stopCh)
if content, err := os.ReadFile(filename); err == nil {
logrus.Infof("LoadDynamicFile: %v is available. content is %s", filename, string(content))
return content, nil
} else {
return nil, err
}
}
func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <-chan struct{}) {
go wait.Until(func() {
// start to watch the file change
watcher, err := fsnotify.NewWatcher()
if err != nil {
logrus.Errorf("startLoadDynamicFile: failed when call fsnotify.NewWatcher, %+v", err)
metrics.Get().DynamicFileFailures.Inc()
return
}
defer watcher.Close()
content, err := loadDynamicFile(filename, stopCh)
if err != nil {
return
}
err = watcher.Add(filename)
if err != nil {
logrus.Errorf("startLoadDynamicFile: could not add file to watcher %v", err)
metrics.Get().DynamicFileFailures.Inc()
return
}
if err := callBack.CallBackForFileLoad(content); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileLoad, %v", err)
}
for {
select {
case <-stopCh:
logrus.Infof("startLoadDynamicFile: watching exit because stopCh closed, filename is %s", filename)
return
case event := <-watcher.Events:
switch {
case event.Op&fsnotify.Write == fsnotify.Write, event.Op&fsnotify.Create == fsnotify.Create:
// reload the access entry file
logrus.Info("startLoadDynamicFile: got WRITE/CREATE event reload it the memory")
content, err := loadDynamicFile(filename, stopCh)
if err != nil {
logrus.Errorf("StartLoadDynamicFile: error in loadDynamicFile, %v", err)
return
}
if err := callBack.CallBackForFileLoad(content); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileLoad, %v", err)
}
case event.Op&fsnotify.Rename == fsnotify.Rename, event.Op&fsnotify.Remove == fsnotify.Remove:
logrus.Info("startLoadDynamicFile: got RENAME/REMOVE event")
// test if the "REMOVE" is triggered by vi or cp cmd
_, err := os.Stat(filename)
if os.IsNotExist(err) {
if err := callBack.CallBackForFileDeletion(); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileDeletion, %v", err)
}
}
return
}
case err := <-watcher.Errors:
logrus.Errorf("startLoadDynamicFile: watcher.Errors for dynamic file %v", err)
metrics.Get().DynamicFileFailures.Inc()
return
}
}
}, time.Second, stopCh)
}
func CalculateTimeDeltaFromUnixInSeconds(from string) (int64, error) {
startTime, err := strconv.ParseInt(from, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse 'startTime' string: %v", err)
}
endTime := time.Now().Unix()
if startTime > endTime {
return 0, fmt.Errorf("start timestamp is after end timestamp")
}
return endTime - startTime, nil
}