-
Notifications
You must be signed in to change notification settings - Fork 120
/
config.go
142 lines (128 loc) · 3.99 KB
/
config.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
package reconciler
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/go-co-op/gocron/v2"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/config"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/types"
)
type ConfigWatcher struct {
configDir string
configPath string
currentSchedule string
job gocron.Job
scheduler gocron.Scheduler
handlerFunc func()
jobFactoryFunc func(string) gocron.JobDefinition
watcher *fsnotify.Watcher
}
func NewConfigWatcher(configPath string, scheduler gocron.Scheduler, configWatcher *fsnotify.Watcher, handlerFunc func()) (*ConfigWatcher, error) {
return newConfigWatcher(
configPath,
scheduler,
configWatcher,
func(schedule string) gocron.JobDefinition {
return gocron.CronJob(schedule, false)
},
handlerFunc,
)
}
func newConfigWatcher(
configPath string,
scheduler gocron.Scheduler,
configWatcher *fsnotify.Watcher,
cronJobFactoryFunc func(string) gocron.JobDefinition,
handlerFunc func(),
) (*ConfigWatcher, error) {
schedule, err := determineCronExpression(configPath)
if err != nil {
return nil, err
}
job, err := scheduler.NewJob(
cronJobFactoryFunc(schedule),
gocron.NewTask(handlerFunc),
)
if err != nil {
return nil, fmt.Errorf("error creating job: %v", err)
}
return &ConfigWatcher{
configDir: filepath.Dir(configPath),
configPath: configPath,
currentSchedule: schedule,
job: job,
scheduler: scheduler,
watcher: configWatcher,
handlerFunc: handlerFunc,
jobFactoryFunc: cronJobFactoryFunc,
}, nil
}
func determineCronExpression(configPath string) (string, error) {
// We read the expression from a file if present, otherwise we use ReconcilerCronExpression
fileContents, err := os.ReadFile(configPath)
if err != nil {
flatipam, _, err := config.GetFlatIPAM(true, &types.IPAMConfig{}, "")
if err != nil {
return "", logging.Errorf("could not get flatipam config: %v", err)
}
_ = logging.Errorf("could not read file: %v, using expression from flatfile: %v", err, flatipam.IPAM.ReconcilerCronExpression)
return flatipam.IPAM.ReconcilerCronExpression, nil
}
logging.Verbosef("using expression: %v", strings.TrimSpace(string(fileContents))) // do i need to trim spaces? idk i think the file would JUST be the expression?
return strings.TrimSpace(string(fileContents)), nil
}
func (c *ConfigWatcher) SyncConfiguration(relevantEventPredicate func(event fsnotify.Event) bool) {
go c.syncConfig(relevantEventPredicate)
if err := c.watcher.Add(c.configDir); err != nil {
_ = logging.Errorf("error adding watcher to config %q: %v", c.configPath, err)
}
}
func (c *ConfigWatcher) syncConfig(relevantEventPredicate func(event fsnotify.Event) bool) {
for {
select {
case event, ok := <-c.watcher.Events:
if !ok {
return
}
if !relevantEventPredicate(event) {
logging.Debugf("event not relevant: %v", event)
continue
}
updatedSchedule, err := determineCronExpression(c.configPath)
if err != nil {
_ = logging.Errorf("error determining cron expression from %q: %v", c.configPath, err)
}
logging.Verbosef(
"configuration updated to file %q. New cron expression: %s",
event.Name,
updatedSchedule,
)
if updatedSchedule == c.currentSchedule {
logging.Debugf("no changes in schedule, nothing to do.")
continue
}
updatedJob, err := c.scheduler.Update(
c.job.ID(),
c.jobFactoryFunc(updatedSchedule),
gocron.NewTask(c.handlerFunc),
)
if err != nil {
_ = logging.Errorf("error updating job %q configuration: %v", c.job.ID().String(), err)
}
c.currentSchedule = updatedSchedule
logging.Verbosef(
"successfully updated CRON configuration id %q - new cron expression: %s",
updatedJob.ID().String(),
updatedSchedule,
)
case err, ok := <-c.watcher.Errors:
_ = logging.Errorf("error when listening to config changes: %v", err)
if !ok {
return
}
}
}
}