forked from monzo/envoy-preflight
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
101 lines (83 loc) · 2.66 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
)
type Config struct {
LoggingEnabled bool
EnvoyAdminAPI string
StartWithoutEnvoy bool
IstioQuitAPI string
NeverKillIstio bool
IstioFallbackPkill bool
NeverKillIstioOnFailure bool
GenericQuitEndpoints []string
}
func getConfig() Config {
loggingEnabled := getBoolFromEnv("ENVOY_PREFLIGHT_LOGGING", true, false)
if getStringFromEnv("ENVOY_KILL_API", "", false) != "" {
log.Error("don't use ENVOY_KILL_API but ISTIO_QUIT_API instead")
}
defaultEnvoyAdminAPI := ""
defaultIstioQuitAPI := ""
if getBoolFromEnv("USE_ISTIO_ENDPOINT", false, loggingEnabled) {
defaultEnvoyAdminAPI = "http://127.0.0.1:15000"
defaultIstioQuitAPI = "http://127.0.0.1:15020"
}
config := Config{
// Logging enabled by default, disabled if "false"
LoggingEnabled: loggingEnabled,
EnvoyAdminAPI: getStringFromEnv("ENVOY_ADMIN_API", defaultEnvoyAdminAPI, loggingEnabled),
StartWithoutEnvoy: getBoolFromEnv("START_WITHOUT_ENVOY", false, loggingEnabled),
IstioQuitAPI: getStringFromEnv("ISTIO_QUIT_API", defaultIstioQuitAPI, loggingEnabled),
NeverKillIstio: getBoolFromEnv("NEVER_KILL_ISTIO", false, loggingEnabled),
IstioFallbackPkill: getBoolFromEnv("ISTIO_FALLBACK_PKILL", false, loggingEnabled),
NeverKillIstioOnFailure: getBoolFromEnv("NEVER_KILL_ISTIO_ON_FAILURE", false, loggingEnabled),
GenericQuitEndpoints: getStringArrayFromEnv("GENERIC_QUIT_ENDPOINTS", make([]string, 0), loggingEnabled),
}
return config
}
func getStringArrayFromEnv(name string, defaultVal []string, logEnabled bool) []string {
userValCsv := strings.Trim(os.Getenv(name), " ")
if userValCsv == "" {
return defaultVal
}
if logEnabled {
log.Infof("%s: %s", name, userValCsv)
}
userValArray := strings.Split(userValCsv, ",")
if len(userValArray) == 0 {
return defaultVal
}
return userValArray
}
func getStringFromEnv(name string, defaultVal string, logEnabled bool) string {
userVal := os.Getenv(name)
if logEnabled {
log.Infof("%s: %s", name, userVal)
}
if userVal != "" {
return userVal
}
return defaultVal
}
func getBoolFromEnv(name string, defaultVal bool, logEnabled bool) bool {
userVal := os.Getenv(name)
// User did not set anything return default
if userVal == "" {
return defaultVal
}
// User set something, check it is valid
if userVal != "true" && userVal != "false" {
if logEnabled {
log.Infof("%s: %s (Invalid value will be ignored)", name, userVal)
}
return defaultVal
}
// User gave valid option
if logEnabled {
log.Infof("%s: %s", name, userVal)
}
return (userVal == "true")
}