mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
WatchConfig and Kubernetes, fixes #284
This commit is contained in:
parent
25b30aa063
commit
9ed185bc8f
1 changed files with 22 additions and 3 deletions
25
viper.go
25
viper.go
|
@ -254,16 +254,35 @@ func (v *Viper) WatchConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile := filepath.Clean(filename)
|
configFile := filepath.Clean(filename)
|
||||||
configDir, _ := filepath.Split(configFile)
|
configDir := filepath.Dir(configFile)
|
||||||
|
realconfigFile, _ := filepath.EvalSymlinks(configFile)
|
||||||
|
realconfigDir := filepath.Dir(realconfigFile)
|
||||||
|
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-watcher.Events:
|
case event := <-watcher.Events:
|
||||||
// we only care about the config file
|
if filepath.Clean(event.Name) == configFile { // we care about the config file
|
||||||
if filepath.Clean(event.Name) == configFile {
|
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
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()
|
err := v.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
|
|
Loading…
Reference in a new issue