Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to watch directory instead of cfg file only #12

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ The haproxy-reload-wrapper watches the HAProxy configuration file using an inoti
stats socket /var/run/haproxy.sock mode 600 level admin expose-fd listeners
```
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`.
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.
4. Modify the configuration file and let the magic happen.✨
3. Optionally set the `WATCH_PATH` environment variable to watch a directory instead of the haproxy.cfg file only
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.
5. Modify the configuration file and let the magic happen.✨
42 changes: 17 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ func main() {
}
log.Notice(fmt.Sprintf("process %d started", cmd.Process.Pid))

// create a fsnotify.Watcher for config file changes
cfgFile := utils.LookupHAProxyConfigFile()
watchPath := utils.LookupWatchPath()
if watchPath == "" {
watchPath = utils.LookupHAProxyConfigFile()
}

// create a fsnotify.Watcher for config changes
fswatch, err := fsnotify.NewWatcher()
if err != nil {
log.Notice(fmt.Sprintf("fsnotify watcher create failed : %v", err))
os.Exit(1)
}
if err := fswatch.Add(cfgFile); err != nil {
log.Notice(fmt.Sprintf("watch file failed : %v", err))
if err := fswatch.Add(watchPath); err != nil {
log.Notice(fmt.Sprintf("watch failed : %v", err))
os.Exit(1)
}
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
log.Notice(fmt.Sprintf("watch : %s", watchPath))

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

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

// re-add watch if file was removed - config maps are updated by removing/adding a symlink
if isRemove(event) {
if err := fswatch.Add(cfgFile); err != nil {
log.Alert(fmt.Sprintf("watch file failed : %v", err))
// re-add watch if path was removed - config maps are updated by removing/adding a symlink
if event.Has(fsnotify.Remove) {
if err := fswatch.Add(watchPath); err != nil {
log.Alert(fmt.Sprintf("watch failed : %v", err))
} else {
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
log.Notice(fmt.Sprintf("watch : %s", watchPath))
}
}

Expand Down Expand Up @@ -131,15 +135,3 @@ func main() {
}
}
}

func isWrite(event fsnotify.Event) bool {
return event.Op&fsnotify.Write == fsnotify.Write
}

func isCreate(event fsnotify.Event) bool {
return event.Op&fsnotify.Create == fsnotify.Create
}

func isRemove(event fsnotify.Event) bool {
return event.Op&fsnotify.Remove == fsnotify.Remove
}
5 changes: 5 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func LookupExecutablePathAbs(executable string) (string, error) {
return filepath.Abs(file)
}

// LookupWatchPath return WATCH_PATH if defined
func LookupWatchPath() string {
return os.Getenv("WATCH_PATH")
}

// LookupHAProxyConfigFile lookup the program arguments to find the config file path (default: "/etc/haproxy/haproxy.cfg")
func LookupHAProxyConfigFile() string {
file := "/etc/haproxy/haproxy.cfg"
Expand Down
Loading