-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
49 lines (38 loc) · 1004 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
lg = logrus.New()
)
func setupConfig() *viper.Viper {
cfg := viper.New()
cfg.AddConfigPath(".")
cfg.AddConfigPath("$HOME/.config")
cfg.AddConfigPath("/etc/ca-injector")
cfg.SetConfigName("ca-injector")
cfg.AutomaticEnv()
cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
cfg.SetDefault("tls.key", "/cert/tls.key")
cfg.SetDefault("tls.crt", "/cert/tls.crt")
cfg.SetDefault("tls.ca.key", "ca.crt")
cfg.SetDefault("shutdown.timeout", 10*time.Second)
if err := cfg.ReadInConfig(); err != nil {
lg.WithError(err).Error("could not read initial config")
}
cfg.OnConfigChange(func(_ fsnotify.Event) {
if err := cfg.ReadInConfig(); err != nil {
lg.WithError(err).Warn("could not reload config")
}
})
if os.Getenv("KUBERNETES_SERVICE_PORT") != "" {
lg.SetFormatter(&logrus.JSONFormatter{})
}
go cfg.WatchConfig()
return cfg
}