Viper: WatchConfig and Kubernetes

Created on 6 Dec 2016  路  10Comments  路  Source: spf13/viper

I've mounted a Kubernetes ConfigMap to the directory /configuration/sub/ which contains the file "config.yaml"

I've pointed by config directory to /configuration/sub/. When I delete the ConfigMap and create a new one following events are emitted:

`EVENT
"/configuration/sub/..129812_06_12_09_48_04.149851493": CREATE
/configuration/sub/..129812_06_12_09_48_04.149851493
/configuration/sub/config.yaml

EVENT
"/configuration/sub/..129812_06_12_09_48_04.149851493": CHMOD
/configuration/sub/..129812_06_12_09_48_04.149851493
/configuration/sub/config.yaml

EVENT
"/configuration/sub/..data_tmp": RENAME
/configuration/sub/..data_tmp
/configuration/sub/config.yaml

EVENT
"/configuration/sub/..data": CREATE
/configuration/sub/..data
/configuration/sub/config.yaml

EVENT
"/configuration/sub/..129812_06_12_09_46_53.419174284": REMOVE
/configuration/sub/..129812_06_12_09_46_53.419174284
/configuration/sub/config.yaml
`

Is there a way to tell viper to watch for directory modifications and simply reload the config file?

Config is simply a simplink to /..data/
root@go-config-template-425445534-wyuxo:/configuration/sub# ls -lah
total 12K
drwxrwxrwx 3 root root 4.0K Dec 6 10:14 .
drwxr-xr-x 3 root root 4.0K Dec 6 09:46 ..
drwxr-xr-x 2 root root 4.0K Dec 6 10:14 ..129812_06_12_10_14_35.092642688
lrwxrwxrwx 1 root root 33 Dec 6 10:14 ..data -> ..129812_06_12_10_14_35.092642688
lrwxrwxrwx 1 root root 18 Dec 6 09:46 config.yaml -> ..data/config.yaml
`

Most helpful comment

Should be merged now!

All 10 comments

If I watch for the parent dir of the symlink to change, i.e.:

`

    configFile := filepath.Clean(filename)
    configDir, _ := filepath.Split(configFile)
    symlinkConfigFile, _ := filepath.EvalSymlinks(configFile)
    symlinkConfigDir, _ := filepath.Split(symlinkConfigFile)

`

and

`

                 if filepath.Clean(event.Name) == filepath.Clean(symlinkConfigDir) {
                    if event.Op&fsnotify.Remove == fsnotify.Remove {
                        err := v.ReadInConfig()
                        if err != nil {
                            log.Println("error:", err)
                        }
                        v.onConfigChange(event)
                    }
                }

`

but this sounds a bit hacky, any ideas?

Isn't there a fancy way to check if any of the symlinks of config.yaml change?

@vasu1124 So the main difference in how viper is currently using fsnotify and that article is using it to watch in Kubernetes is that they are watching the Remove event and Viper is watching Create/Write.

Any easy solution for viper would likely be to add support for fsnotify.Remove at least as an optional watch config.

@anopheles could you review against your fix/proposal?

Would be cool to have this working

Indeed. I even closed my PR in favour of the one from @xcoulon, so there should be no confusion. Seems that this issue is not deemed as relevant ...

thanks @dimm0 and @vasu1124 for your comments.

@bep (I'm taking the liberty to ping you since you're the most recent active committer on the project), could you please give a look at this PR?

Could somebody look at it plz? It will help a lot having this stuff working

What are others doing to resolve this issue?

Should be merged now!

Was this page helpful?
0 / 5 - 0 ratings