-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
83 lines (71 loc) · 1.8 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/spf13/viper"
)
type Config struct {
APIKey string
KubeHost string
KubePort string
ConsulHost string
ConsulPort string
ConsulDomain string
Protocol string
SkipTlsVerify bool
UserConfigs []UserConfig
}
const HTTP = "http"
const HTTPS = "https"
var appConfig *Config
func loadConfig() {
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.ReadInConfig()
appConfig = &Config{
APIKey: getKubernetesAPIToken(),
KubeHost: viper.GetString("KUBERNETES_SERVICE_HOST"),
KubePort: viper.GetString("KUBERNETES_SERVICE_PORT"),
ConsulHost: viper.GetString("CONSUL_HOST"),
ConsulPort: viper.GetString("CONSUL_PORT"),
ConsulDomain: viper.GetString("CONSUL_DOMAIN"),
Protocol: getProtocol(),
SkipTlsVerify: viper.GetBool("SKIP_TLS_VERIFY"),
UserConfigs: getIngressConfigs(),
}
}
func getProtocol() string {
if port := viper.GetInt("KUBERNETES_SERVICE_PORT"); port == 443 {
return HTTPS
} else if isHttps := viper.GetBool("USE_HTTPS"); isHttps {
return HTTPS
} else {
return HTTP
}
}
func getKubernetesAPIToken() string {
tokenFile := "/var/run/secrets/kubernetes.io/serviceaccount/token"
data, err := ioutil.ReadFile(tokenFile)
if err != nil {
return viper.GetString("KUBERNETES_API_TOKEN")
}
return string(data)
}
func getIngressConfigs() []UserConfig {
var configs []UserConfig
configAsString := viper.GetString("USER_CONFIGS")
if err := json.Unmarshal([]byte(configAsString), &configs); err != nil {
log.Fatal(err)
}
return configs
}
func getConsulDomain() string {
consulDomain := viper.GetString("CONSUL_DOMAIN")
if consulDomain == "" {
return "service.consul"
}
return consulDomain
}