Skip to content

Commit 240543a

Browse files
snorwinm-terra
andauthored
option to watch directory instead of cfg file only (#12) (#13)
* watch config directory instead of cfg file only Co-authored-by: Andy Baer <andy.baer@gmail.com>
1 parent a0ec75c commit 240543a

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ The haproxy-reload-wrapper watches the HAProxy configuration file using an inoti
2323
stats socket /var/run/haproxy.sock mode 600 level admin expose-fd listeners
2424
```
2525
2. Set the `HAPROXY_SOCKET` environment variable to the path of the socket if it is different from the default path: `/var/run/haproxy.sock`.
26-
3. Replace the `docker.io/haproxy` image with the `ghcr.io/snorwin/haproxy` image on container platforms or compile the source code and run `./haproxy-reload-wrapper` on a Linux system. As an example, check out the [Helm chart](test/helm) used for the tests.
27-
4. Modify the configuration file and let the magic happen.✨
26+
3. Optionally set the `WATCH_PATH` environment variable to watch a directory instead of the haproxy.cfg file only
27+
4. Replace the `docker.io/haproxy` image with the `ghcr.io/snorwin/haproxy` image on container platforms or compile the source code and run `./haproxy-reload-wrapper` on a Linux system. As an example, check out the [Helm chart](test/helm) used for the tests.
28+
5. Modify the configuration file and let the magic happen.✨

main.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ func main() {
3232
}
3333
log.Notice(fmt.Sprintf("process %d started", cmd.Process.Pid))
3434

35-
// create a fsnotify.Watcher for config file changes
36-
cfgFile := utils.LookupHAProxyConfigFile()
35+
watchPath := utils.LookupWatchPath()
36+
if watchPath == "" {
37+
watchPath = utils.LookupHAProxyConfigFile()
38+
}
39+
40+
// create a fsnotify.Watcher for config changes
3741
fswatch, err := fsnotify.NewWatcher()
3842
if err != nil {
3943
log.Notice(fmt.Sprintf("fsnotify watcher create failed : %v", err))
4044
os.Exit(1)
4145
}
42-
if err := fswatch.Add(cfgFile); err != nil {
43-
log.Notice(fmt.Sprintf("watch file failed : %v", err))
46+
if err := fswatch.Add(watchPath); err != nil {
47+
log.Notice(fmt.Sprintf("watch failed : %v", err))
4448
os.Exit(1)
4549
}
46-
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
50+
log.Notice(fmt.Sprintf("watch : %s", watchPath))
4751

4852
// flag used for termination handling
4953
var terminated bool
@@ -56,19 +60,19 @@ func main() {
5660
for {
5761
select {
5862
case event := <-fswatch.Events:
59-
// only care about events which may modify the contents of the file
60-
if !(isWrite(event) || isRemove(event) || isCreate(event)) {
63+
// only care about events which may modify the contents of the directory
64+
if !(event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Create)) {
6165
continue
6266
}
6367

64-
log.Notice(fmt.Sprintf("fs event for file %s : %v", cfgFile, event.Op))
68+
log.Notice(fmt.Sprintf("fs event for %s : %v", watchPath, event.Op))
6569

66-
// re-add watch if file was removed - config maps are updated by removing/adding a symlink
67-
if isRemove(event) {
68-
if err := fswatch.Add(cfgFile); err != nil {
69-
log.Alert(fmt.Sprintf("watch file failed : %v", err))
70+
// re-add watch if path was removed - config maps are updated by removing/adding a symlink
71+
if event.Has(fsnotify.Remove) {
72+
if err := fswatch.Add(watchPath); err != nil {
73+
log.Alert(fmt.Sprintf("watch failed : %v", err))
7074
} else {
71-
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
75+
log.Notice(fmt.Sprintf("watch : %s", watchPath))
7276
}
7377
}
7478

@@ -131,15 +135,3 @@ func main() {
131135
}
132136
}
133137
}
134-
135-
func isWrite(event fsnotify.Event) bool {
136-
return event.Op&fsnotify.Write == fsnotify.Write
137-
}
138-
139-
func isCreate(event fsnotify.Event) bool {
140-
return event.Op&fsnotify.Create == fsnotify.Create
141-
}
142-
143-
func isRemove(event fsnotify.Event) bool {
144-
return event.Op&fsnotify.Remove == fsnotify.Remove
145-
}

pkg/utils/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func LookupExecutablePathAbs(executable string) (string, error) {
1717
return filepath.Abs(file)
1818
}
1919

20+
// LookupWatchPath return WATCH_PATH if defined
21+
func LookupWatchPath() string {
22+
return os.Getenv("WATCH_PATH")
23+
}
24+
2025
// LookupHAProxyConfigFile lookup the program arguments to find the config file path (default: "/etc/haproxy/haproxy.cfg")
2126
func LookupHAProxyConfigFile() string {
2227
file := "/etc/haproxy/haproxy.cfg"

0 commit comments

Comments
 (0)