forked from redboxllc/scuttle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scuttle_config.go
124 lines (105 loc) · 3.42 KB
/
scuttle_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"os"
"strings"
"time"
)
// ScuttleConfig ... represents Scuttle's configuration based on environment variables or defaults.
type ScuttleConfig struct {
LoggingEnabled bool
EnvoyAdminAPI string
StartWithoutEnvoy bool
WaitForEnvoyTimeout time.Duration
IstioQuitAPI string
NeverKillIstio bool
IstioFallbackPkill bool
NeverKillIstioOnFailure bool
GenericQuitEndpoints []string
QuitWithoutEnvoyTimeout time.Duration
}
func log(message string) {
if config.LoggingEnabled {
fmt.Printf("%s scuttle: %s\n", time.Now().UTC().Format("2006-01-02T15:04:05Z"), message)
}
}
func getConfig() ScuttleConfig {
loggingEnabled := getBoolFromEnv("SCUTTLE_LOGGING", true, false)
config := ScuttleConfig{
// Logging enabled by default, disabled if "false"
LoggingEnabled: loggingEnabled,
EnvoyAdminAPI: getStringFromEnv("ENVOY_ADMIN_API", "", loggingEnabled),
StartWithoutEnvoy: getBoolFromEnv("START_WITHOUT_ENVOY", false, loggingEnabled),
WaitForEnvoyTimeout: getDurationFromEnv("WAIT_FOR_ENVOY_TIMEOUT", time.Duration(0), loggingEnabled),
IstioQuitAPI: getStringFromEnv("ISTIO_QUIT_API", "", 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),
QuitWithoutEnvoyTimeout: getDurationFromEnv("QUIT_WITHOUT_ENVOY_TIMEOUT", time.Duration(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(fmt.Sprintf("%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(fmt.Sprintf("%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(fmt.Sprintf("%s: %s (Invalid value will be ignored)", name, userVal))
}
return defaultVal
}
// User gave valid option
if logEnabled {
log(fmt.Sprintf("%s: %s", name, userVal))
}
return userVal == "true"
}
func getDurationFromEnv(name string, defaultVal time.Duration, logEnabled bool) time.Duration {
userVal := os.Getenv(name)
// User did not set anything, return default.
if userVal == "" {
return defaultVal
}
// User has set something, check it is valid.
if userVal != "" {
if duration, err := time.ParseDuration(userVal); err == nil {
// User gave valid option.
if logEnabled {
log(fmt.Sprintf("%s: %s", name, userVal))
}
return duration
} else if logEnabled {
log(fmt.Sprintf("%s: %s (Invalid value will be ignored)", name, userVal))
}
}
return defaultVal
}