From 9ed185bc8feb35443faf94cb7032b11cbdbf5b49 Mon Sep 17 00:00:00 2001 From: vasu1124 Date: Mon, 18 Sep 2017 12:27:25 +0200 Subject: [PATCH] WatchConfig and Kubernetes, fixes #284 --- viper.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 2a221e5..c1f74c6 100644 --- a/viper.go +++ b/viper.go @@ -254,16 +254,35 @@ func (v *Viper) WatchConfig() { } configFile := filepath.Clean(filename) - configDir, _ := filepath.Split(configFile) + configDir := filepath.Dir(configFile) + realconfigFile, _ := filepath.EvalSymlinks(configFile) + realconfigDir := filepath.Dir(realconfigFile) done := make(chan bool) go func() { for { select { case event := <-watcher.Events: - // we only care about the config file - if filepath.Clean(event.Name) == configFile { + if filepath.Clean(event.Name) == configFile { // we care about the config file if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { + err := v.ReadInConfig() + if err != nil { + log.Println("error:", err) + } + v.onConfigChange(event) + } + } else if filepath.Clean(event.Name) == realconfigDir { // we also care about the REMOVE event on the realconfigDir + // within a kubernetes pod, the file changes are registered as + // "/some/configDir/..9989_18_09_07_40_15.009361056": CREATE + // "/some/configDir/..9989_18_09_07_40_15.009361056": CHMOD + // "/some/configDir/..data_tmp": RENAME + // "/some/configDir/..data": CREATE + // "/some/configDir/..9989_18_09_07_38_48.160272044": REMOVE + if event.Op&fsnotify.Remove == fsnotify.Remove { + //re-eval symlink + realconfigFile, _ = filepath.EvalSymlinks(configFile) + realconfigDir = filepath.Dir(realconfigFile) + err := v.ReadInConfig() if err != nil { log.Println("error:", err)