forked from kiali/kiali
/
config.go
430 lines (370 loc) · 16.5 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package config
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/kiali/kiali/config/security"
"github.com/kiali/kiali/log"
)
// Environment vars can define some default values.
// NOTE: If you add a new variable, don't forget to update README.adoc
const (
EnvIdentityCertFile = "IDENTITY_CERT_FILE"
EnvIdentityPrivateKeyFile = "IDENTITY_PRIVATE_KEY_FILE"
EnvPrometheusServiceURL = "PROMETHEUS_SERVICE_URL"
EnvPrometheusCustomMetricsURL = "PROMETHEUS_CUSTOM_METRICS_URL"
EnvInCluster = "IN_CLUSTER"
EnvIstioIdentityDomain = "ISTIO_IDENTITY_DOMAIN"
EnvIstioSidecarAnnotation = "ISTIO_SIDECAR_ANNOTATION"
EnvIstioUrlServiceVersion = "ISTIO_URL_SERVICE_VERSION"
EnvApiNamespacesExclude = "API_NAMESPACES_EXCLUDE"
EnvServerAddress = "SERVER_ADDRESS"
EnvServerPort = "SERVER_PORT"
EnvWebRoot = "SERVER_WEB_ROOT"
EnvServerStaticContentRootDirectory = "SERVER_STATIC_CONTENT_ROOT_DIRECTORY"
EnvServerCORSAllowAll = "SERVER_CORS_ALLOW_ALL"
EnvServerAuditLog = "SERVER_AUDIT_LOG"
EnvServerMetricsPort = "SERVER_METRICS_PORT"
EnvServerMetricsEnabled = "SERVER_METRICS_ENABLED"
EnvGrafanaDisplayLink = "GRAFANA_DISPLAY_LINK"
EnvGrafanaURL = "GRAFANA_URL"
EnvGrafanaServiceNamespace = "GRAFANA_SERVICE_NAMESPACE"
EnvGrafanaService = "GRAFANA_SERVICE"
EnvGrafanaWorkloadDashboardPattern = "GRAFANA_WORKLOAD_DASHBOARD_PATTERN"
EnvGrafanaServiceDashboardPattern = "GRAFANA_SERVICE_DASHBOARD_PATTERN"
EnvGrafanaVarNamespace = "GRAFANA_VAR_NAMESPACE"
EnvGrafanaVarService = "GRAFANA_VAR_SERVICE"
EnvGrafanaVarWorkload = "GRAFANA_VAR_WORKLOAD"
EnvGrafanaAPIKey = "GRAFANA_API_KEY"
EnvGrafanaUsername = "GRAFANA_USERNAME"
EnvGrafanaPassword = "GRAFANA_PASSWORD"
EnvJaegerURL = "JAEGER_URL"
EnvJaegerService = "JAEGER_SERVICE"
EnvLoginTokenSigningKey = "LOGIN_TOKEN_SIGNING_KEY"
EnvLoginTokenExpirationSeconds = "LOGIN_TOKEN_EXPIRATION_SECONDS"
EnvIstioNamespace = "ISTIO_NAMESPACE"
EnvIstioLabelNameApp = "ISTIO_LABEL_NAME_APP"
EnvIstioLabelNameVersion = "ISTIO_LABEL_NAME_VERSION"
EnvKubernetesBurst = "KUBERNETES_BURST"
EnvKubernetesQPS = "KUBERNETES_QPS"
EnvKubernetesCacheEnabled = "KUBERNETES_CACHE_ENABLED"
EnvKubernetesCacheDuration = "KUBERNETES_CACHE_DURATION"
EnvAuthStrategy = "AUTH_STRATEGY"
)
// The versions that Kiali requires
const (
IstioVersionSupported = ">= 1.0"
MaistraVersionSupported = ">= 0.1.0"
)
// The valid auth strategies and values for cookie handling
const (
AuthStrategyOpenshift = "openshift"
AuthStrategyLogin = "login"
AuthStrategyAnonymous = "anonymous"
TokenCookieName = "kiali-token"
AuthStrategyOpenshiftIssuer = "kiali-openshift"
AuthStrategyLoginIssuer = "kiali-login"
)
// the paths we expect the login secret to be located
const (
LoginSecretUsername = "/kiali-secret/username"
LoginSecretPassphrase = "/kiali-secret/passphrase"
)
// Global configuration for the application.
var configuration *Config
// Server configuration
type Server struct {
Address string `yaml:",omitempty"`
Port int `yaml:",omitempty"`
Credentials security.Credentials `yaml:",omitempty"`
WebRoot string `yaml:"web_root,omitempty"`
StaticContentRootDirectory string `yaml:"static_content_root_directory,omitempty"`
CORSAllowAll bool `yaml:"cors_allow_all,omitempty"`
AuditLog bool `yaml:"audit_log,omitempty"`
MetricsPort int `yaml:"metrics_port,omitempty"`
MetricsEnabled bool `yaml:"metrics_enabled,omitempty"`
}
// GrafanaConfig describes configuration used for Grafana links
type GrafanaConfig struct {
DisplayLink bool `yaml:"display_link"`
URL string `yaml:"url"`
ServiceNamespace string `yaml:"service_namespace"`
Service string `yaml:"service"`
WorkloadDashboardPattern string `yaml:"workload_dashboard_pattern"`
ServiceDashboardPattern string `yaml:"service_dashboard_pattern"`
VarNamespace string `yaml:"var_namespace"`
VarService string `yaml:"var_service"`
VarWorkload string `yaml:"var_workload"`
APIKey string `yaml:"api_key"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// JaegerConfig describes configuration used for jaeger links
type JaegerConfig struct {
URL string `yaml:"url"`
Service string `yaml:"service"`
}
// IstioConfig describes configuration used for istio links
type IstioConfig struct {
UrlServiceVersion string `yaml:"url_service_version"`
IstioIdentityDomain string `yaml:"istio_identity_domain,omitempty"`
IstioSidecarAnnotation string `yaml:"istio_sidecar_annotation,omitempty"`
}
// ExternalServices holds configurations for other systems that Kiali depends on
type ExternalServices struct {
Istio IstioConfig `yaml:"istio,omitempty"`
PrometheusServiceURL string `yaml:"prometheus_service_url,omitempty"`
PrometheusCustomMetricsURL string `yaml:"prometheus_custom_metrics_url,omitempty"`
Grafana GrafanaConfig `yaml:"grafana,omitempty"`
Jaeger JaegerConfig `yaml:"jaeger,omitempty"`
}
// LoginToken holds config used in token-based authentication
type LoginToken struct {
SigningKey []byte `yaml:"signing_key,omitempty"`
ExpirationSeconds int64 `yaml:"expiration_seconds,omitempty"`
}
// IstioLabels holds configuration about the labels required by Istio
type IstioLabels struct {
AppLabelName string `yaml:"app_label_name,omitempty" json:"appLabelName"`
VersionLabelName string `yaml:"version_label_name,omitempty" json:"versionLabelName"`
}
// Kubernetes client configuration
type KubernetesConfig struct {
Burst int `yaml:"burst,omitempty"`
QPS float32 `yaml:"qps,omitempty"`
CacheEnabled bool `yaml:"cache_enabled,omitempty"`
CacheDuration int64 `yaml:"cache_duration,omitempty"`
}
// Exclude Blacklist holds regex strings defining a blacklist
type ApiConfig struct {
Namespaces ApiNamespacesConfig
}
// Exclude Blacklist holds regex strings defining a blacklist
type ApiNamespacesConfig struct {
Exclude []string
}
// Authentication configuration
type AuthConfig struct {
Strategy string `yaml:"strategy,omitempty"`
}
// Config defines full YAML configuration.
type Config struct {
Identity security.Identity `yaml:",omitempty"`
Server Server `yaml:",omitempty"`
InCluster bool `yaml:"in_cluster,omitempty"`
ExternalServices ExternalServices `yaml:"external_services,omitempty"`
LoginToken LoginToken `yaml:"login_token,omitempty"`
IstioNamespace string `yaml:"istio_namespace,omitempty"`
IstioLabels IstioLabels `yaml:"istio_labels,omitempty"`
KubernetesConfig KubernetesConfig `yaml:"kubernetes_config,omitempty"`
API ApiConfig `yaml:"api,omitempty"`
Auth AuthConfig `yaml:"auth,omitempty"`
}
// NewConfig creates a default Config struct
func NewConfig() (c *Config) {
c = new(Config)
c.Identity.CertFile = getDefaultString(EnvIdentityCertFile, "")
c.Identity.PrivateKeyFile = getDefaultString(EnvIdentityPrivateKeyFile, "")
c.InCluster = getDefaultBool(EnvInCluster, true)
c.IstioNamespace = strings.TrimSpace(getDefaultString(EnvIstioNamespace, "istio-system"))
c.IstioLabels.AppLabelName = strings.TrimSpace(getDefaultString(EnvIstioLabelNameApp, "app"))
c.IstioLabels.VersionLabelName = strings.TrimSpace(getDefaultString(EnvIstioLabelNameVersion, "version"))
c.API.Namespaces.Exclude = getDefaultStringArray(EnvApiNamespacesExclude, "istio-operator,kube.*,openshift.*,ibm.*")
// Server configuration
c.Server.Address = strings.TrimSpace(getDefaultString(EnvServerAddress, ""))
c.Server.Port = getDefaultInt(EnvServerPort, 20000)
c.Server.Credentials = security.Credentials{
Username: getDefaultStringFromFile(LoginSecretUsername, ""),
Passphrase: getDefaultStringFromFile(LoginSecretPassphrase, ""),
}
c.Server.WebRoot = strings.TrimSpace(getDefaultString(EnvWebRoot, "/"))
c.Server.StaticContentRootDirectory = strings.TrimSpace(getDefaultString(EnvServerStaticContentRootDirectory, "/opt/kiali/console"))
c.Server.CORSAllowAll = getDefaultBool(EnvServerCORSAllowAll, false)
c.Server.AuditLog = getDefaultBool(EnvServerAuditLog, true)
c.Server.MetricsPort = getDefaultInt(EnvServerMetricsPort, 9090)
c.Server.MetricsEnabled = getDefaultBool(EnvServerMetricsEnabled, true)
// Prometheus configuration
c.ExternalServices.PrometheusServiceURL = strings.TrimSpace(getDefaultString(EnvPrometheusServiceURL, "http://prometheus.istio-system:9090"))
c.ExternalServices.PrometheusCustomMetricsURL = strings.TrimSpace(getDefaultString(EnvPrometheusCustomMetricsURL, c.ExternalServices.PrometheusServiceURL))
// Grafana Configuration
c.ExternalServices.Grafana.DisplayLink = getDefaultBool(EnvGrafanaDisplayLink, true)
c.ExternalServices.Grafana.URL = strings.TrimSpace(getDefaultString(EnvGrafanaURL, ""))
c.ExternalServices.Grafana.ServiceNamespace = strings.TrimSpace(getDefaultString(EnvGrafanaServiceNamespace, "istio-system"))
c.ExternalServices.Grafana.Service = strings.TrimSpace(getDefaultString(EnvGrafanaService, "grafana"))
c.ExternalServices.Grafana.WorkloadDashboardPattern = strings.TrimSpace(getDefaultString(EnvGrafanaWorkloadDashboardPattern, "Istio%20Workload%20Dashboard"))
c.ExternalServices.Grafana.ServiceDashboardPattern = strings.TrimSpace(getDefaultString(EnvGrafanaServiceDashboardPattern, "Istio%20Service%20Dashboard"))
c.ExternalServices.Grafana.VarNamespace = strings.TrimSpace(getDefaultString(EnvGrafanaVarNamespace, "var-namespace"))
c.ExternalServices.Grafana.VarService = strings.TrimSpace(getDefaultString(EnvGrafanaVarService, "var-service"))
c.ExternalServices.Grafana.VarWorkload = strings.TrimSpace(getDefaultString(EnvGrafanaVarWorkload, "var-workload"))
c.ExternalServices.Grafana.APIKey = strings.TrimSpace(getDefaultString(EnvGrafanaAPIKey, ""))
c.ExternalServices.Grafana.Username = strings.TrimSpace(getDefaultString(EnvGrafanaUsername, ""))
c.ExternalServices.Grafana.Password = strings.TrimSpace(getDefaultString(EnvGrafanaPassword, ""))
if c.ExternalServices.Grafana.Username != "" && c.ExternalServices.Grafana.Password == "" {
log.Error("Grafana username (\"GRAFANA_USERNAME\") requires that Grafana password (\"GRAFANA_PASSWORD\") is set.")
}
// Jaeger Configuration
c.ExternalServices.Jaeger.URL = strings.TrimSpace(getDefaultString(EnvJaegerURL, ""))
c.ExternalServices.Jaeger.Service = strings.TrimSpace(getDefaultString(EnvJaegerService, "jaeger-query"))
// Istio Configuration
c.ExternalServices.Istio.IstioIdentityDomain = strings.TrimSpace(getDefaultString(EnvIstioIdentityDomain, "svc.cluster.local"))
c.ExternalServices.Istio.IstioSidecarAnnotation = strings.TrimSpace(getDefaultString(EnvIstioSidecarAnnotation, "sidecar.istio.io/status"))
c.ExternalServices.Istio.UrlServiceVersion = strings.TrimSpace(getDefaultString(EnvIstioUrlServiceVersion, "http://istio-pilot:8080/version"))
// Token-based authentication Configuration
c.LoginToken.SigningKey = []byte(strings.TrimSpace(getDefaultString(EnvLoginTokenSigningKey, "kiali")))
c.LoginToken.ExpirationSeconds = getDefaultInt64(EnvLoginTokenExpirationSeconds, 24*3600)
// Kubernetes client Configuration
c.KubernetesConfig.Burst = getDefaultInt(EnvKubernetesBurst, 200)
c.KubernetesConfig.QPS = getDefaultFloat32(EnvKubernetesQPS, 175)
c.KubernetesConfig.CacheEnabled = getDefaultBool(EnvKubernetesCacheEnabled, false)
c.KubernetesConfig.CacheDuration = getDefaultInt64(EnvKubernetesCacheDuration, time.Duration(5*time.Minute).Nanoseconds())
trimmedExclusionPatterns := []string{}
for _, entry := range c.API.Namespaces.Exclude {
exclusionPattern := strings.TrimSpace(entry)
if _, err := regexp.Compile(exclusionPattern); err != nil {
log.Errorf("Invalid namespace exclude entry, [%s] is not a valid regex pattern: %v", exclusionPattern, err)
} else {
trimmedExclusionPatterns = append(trimmedExclusionPatterns, strings.TrimSpace(exclusionPattern))
}
}
c.API.Namespaces.Exclude = trimmedExclusionPatterns
c.Auth.Strategy = getDefaultString(EnvAuthStrategy, AuthStrategyLogin)
return
}
// Get the global Config
func Get() (conf *Config) {
return configuration
}
// Set the global Config
func Set(conf *Config) {
configuration = conf
}
func getDefaultStringFromFile(filename string, defaultValue string) (retVal string) {
if fileContents, err := ioutil.ReadFile(filename); err == nil {
retVal = string(fileContents)
} else {
retVal = defaultValue
}
return
}
func getDefaultString(envvar string, defaultValue string) (retVal string) {
retVal = os.Getenv(envvar)
if retVal == "" {
retVal = defaultValue
}
return
}
func getDefaultStringArray(envvar string, defaultValue string) (retVal []string) {
csv := os.Getenv(envvar)
if csv == "" {
csv = defaultValue
}
retVal = strings.Split(csv, ",")
return
}
func getDefaultInt(envvar string, defaultValue int) (retVal int) {
retValString := os.Getenv(envvar)
if retValString == "" {
retVal = defaultValue
} else {
if num, err := strconv.Atoi(retValString); err != nil {
log.Warningf("Invalid number for envvar [%v]. err=%v", envvar, err)
retVal = defaultValue
} else {
retVal = num
}
}
return
}
func getDefaultInt64(envvar string, defaultValue int64) (retVal int64) {
retValString := os.Getenv(envvar)
if retValString == "" {
retVal = defaultValue
} else {
if num, err := strconv.ParseInt(retValString, 10, 64); err != nil {
log.Warningf("Invalid number for envvar [%v]. err=%v", envvar, err)
retVal = defaultValue
} else {
retVal = num
}
}
return
}
func getDefaultBool(envvar string, defaultValue bool) (retVal bool) {
retValString := os.Getenv(envvar)
if retValString == "" {
retVal = defaultValue
} else {
if b, err := strconv.ParseBool(retValString); err != nil {
log.Warningf("Invalid boolean for envvar [%v]. err=%v", envvar, err)
retVal = defaultValue
} else {
retVal = b
}
}
return
}
func getDefaultFloat32(envvar string, defaultValue float32) (retVal float32) {
retValString := os.Getenv(envvar)
if retValString == "" {
retVal = defaultValue
} else {
if f, err := strconv.ParseFloat(retValString, 32); err != nil {
log.Warningf("Invalid float number for envvar [%v]. err=%v", envvar, err)
retVal = defaultValue
} else {
retVal = float32(f)
}
}
return
}
// String marshals the given Config into a YAML string
func (conf Config) String() (str string) {
str, err := Marshal(&conf)
if err != nil {
str = fmt.Sprintf("Failed to marshal config to string. err=%v", err)
log.Debugf(str)
}
return
}
// Unmarshal parses the given YAML string and returns its Config object representation.
func Unmarshal(yamlString string) (conf *Config, err error) {
conf = NewConfig()
err = yaml.Unmarshal([]byte(yamlString), &conf)
if err != nil {
return nil, fmt.Errorf("Failed to parse yaml data. error=%v", err)
}
return
}
// Marshal converts the Config object and returns its YAML string.
func Marshal(conf *Config) (yamlString string, err error) {
yamlBytes, err := yaml.Marshal(&conf)
if err != nil {
return "", fmt.Errorf("Failed to produce yaml. error=%v", err)
}
yamlString = string(yamlBytes)
return
}
// LoadFromFile reads the YAML from the given file, parses the content, and returns its Config object representation.
func LoadFromFile(filename string) (conf *Config, err error) {
log.Debugf("Reading YAML config from [%s]", filename)
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("Failed to load config file [%v]. error=%v", filename, err)
}
return Unmarshal(string(fileContent))
}
// SaveToFile converts the Config object and stores its YAML string into the given file, overwriting any data that is in the file.
func SaveToFile(filename string, conf *Config) (err error) {
fileContent, err := Marshal(conf)
if err != nil {
return fmt.Errorf("Failed to save config file [%v]. error=%v", filename, err)
}
log.Debugf("Writing YAML config to [%s]", filename)
err = ioutil.WriteFile(filename, []byte(fileContent), 0640)
return
}