generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 19
/
watchconfig.go
137 lines (119 loc) · 3.51 KB
/
watchconfig.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
// Copyright 2023 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package watchconfig watches for config file changes
package watchconfig
import (
"bytes"
"os"
"path/filepath"
"time"
commonlog "sigs.k8s.io/usage-metrics-collector/pkg/log"
"github.com/fsnotify/fsnotify"
)
var (
log = commonlog.Log.WithName("watch-config")
)
type ConfigFile struct {
lastRead []byte
modTime time.Time
ConfigFilename string
}
// WatchConfig will watch the config file and call onChange if the file contents change
func (cf ConfigFile) WatchConfig(onChange func(*fsnotify.Watcher, chan interface{})) error {
watcher, err := fsnotify.NewWatcher()
stop := make(chan interface{})
if err != nil {
return err
}
// read the initial config
if err := cf.read(); err != nil {
return err
}
// watch the directory instead of the file itself
// because we won't get events for the file when the
// configmap is updated as it is a symlink.
if err := watcher.Add(filepath.Dir(cf.ConfigFilename)); err != nil {
return err
}
go func() {
for {
select {
case _, ok := <-watcher.Events:
if !ok {
log.Info("error reading config event", "filename", cf.ConfigFilename)
continue
}
changed, err := cf.changed()
if err != nil {
log.Error(err, "error watching config file", "filename", cf.ConfigFilename)
continue
}
if changed {
// configFile has changed
onChange(watcher, stop)
}
case err, ok := <-watcher.Errors:
if !ok {
log.Info("error reading config event", "filename", cf.ConfigFilename)
continue
}
log.Error(err, "error watching config file", "filename", cf.ConfigFilename)
case <-stop:
log.V(1).Info("stopping watch config file", "filename", cf.ConfigFilename)
return
}
}
}()
return nil
}
// read caches the config file mod time and contents to check for changes in the future
func (cf *ConfigFile) read() error {
f, err := os.Stat(cf.ConfigFilename)
if err != nil {
log.Error(err, "error read config file", "filename", cf.ConfigFilename)
return err
}
cf.modTime = f.ModTime()
b, err := os.ReadFile(cf.ConfigFilename)
if err != nil {
log.Error(err, "error stat config file", "filename", cf.ConfigFilename)
return err
}
cf.lastRead = b
return nil
}
// changed returns true if the file has changed since the last time read or changed was called
func (cf *ConfigFile) changed() (bool, error) {
f, err := os.Stat(cf.ConfigFilename)
if err != nil {
log.Error(err, "error state config file", "filename", cf.ConfigFilename)
return false, err
}
// file hasn't changed since we last read it
if !f.ModTime().After(cf.modTime) {
return false, nil
}
b, err := os.ReadFile(cf.ConfigFilename)
if err != nil {
log.Error(err, "error stat config file", "filename", cf.ConfigFilename)
return false, err
}
// check if the config file has changed
if bytes.Equal(cf.lastRead, b) {
return false, nil
}
log.Info("config file changed", "filename", cf.ConfigFilename)
cf.lastRead = b
return true, nil
}