forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmap.go
398 lines (339 loc) · 11.9 KB
/
configmap.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package prometheus
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"gopkg.in/yaml.v2"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
"github.com/kubermatic/kubermatic/api/pkg/resources/reconciling"
corev1 "k8s.io/api/core/v1"
)
type tlsConfig struct {
CAFile string `yaml:"ca_file"`
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
}
// customizationData is the data available to custom scraping configs and rules,
// containing everything required to scrape resources.
type customizationData struct {
Cluster *kubermaticv1.Cluster
APIServerHost string
EtcdTLS tlsConfig
ApiserverTLS tlsConfig
ScrapingAnnotationPrefix string
}
type configTemplateData struct {
TemplateData interface{}
APIServerHost string
EtcdTLSConfig string
ApiserverTLSConfig string
CustomScrapingConfigs string
}
// ConfigMapCreator returns a ConfigMapCreator containing the prometheus config for the supplied data
func ConfigMapCreator(data *resources.TemplateData) reconciling.NamedConfigMapCreatorGetter {
return func() (string, reconciling.ConfigMapCreator) {
return resources.PrometheusConfigConfigMapName, func(cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
cluster := data.Cluster()
// prepare TLS config
etcdTLS := tlsConfig{
CAFile: "/etc/etcd/pki/client/ca.crt",
CertFile: "/etc/etcd/pki/client/apiserver-etcd-client.crt",
KeyFile: "/etc/etcd/pki/client/apiserver-etcd-client.key",
}
apiserverTLS := tlsConfig{
CAFile: "/etc/kubernetes/ca.crt",
CertFile: "/etc/kubernetes/prometheus-client.crt",
KeyFile: "/etc/kubernetes/prometheus-client.key",
}
// get custom scraping configs and rules
customData := &customizationData{
Cluster: cluster,
APIServerHost: fmt.Sprintf("%s:%d", cluster.Address.InternalName, cluster.Address.Port),
EtcdTLS: etcdTLS,
ApiserverTLS: apiserverTLS,
ScrapingAnnotationPrefix: data.MonitoringScrapeAnnotationPrefix(),
}
customScrapingFile := data.InClusterPrometheusScrapingConfigsFile()
customScrapingConfigs, err := loadTemplatedFile(customScrapingFile, customData)
if err != nil {
return nil, fmt.Errorf("failed to load custom scraping configs file %s: %v", customScrapingFile, err)
}
customRulesFile := data.InClusterPrometheusRulesFile()
customRules, err := loadTemplatedFile(customRulesFile, customData)
if err != nil {
return nil, fmt.Errorf("failed to load custom rules file %s: %v", customRulesFile, err)
}
// prepare tls_config stanza
etcdTLSYaml, err := yaml.Marshal(etcdTLS)
if err != nil {
return nil, fmt.Errorf("failed to encode etcd TLS config as YAML: %v", err)
}
apiserverTLSYaml, err := yaml.Marshal(apiserverTLS)
if err != nil {
return nil, fmt.Errorf("failed to encode apiserver TLS config as YAML: %v", err)
}
// prepare config template
configData := &configTemplateData{
TemplateData: data,
APIServerHost: customData.APIServerHost,
CustomScrapingConfigs: customScrapingConfigs,
EtcdTLSConfig: strings.TrimSpace(string(etcdTLSYaml)),
ApiserverTLSConfig: strings.TrimSpace(string(apiserverTLSYaml)),
}
config, err := renderTemplate(prometheusConfig, configData)
if err != nil {
return nil, fmt.Errorf("failed to render Prometheus config: %v", err)
}
// update ConfigMap
cm.Labels = resources.BaseAppLabels(name, nil)
if cm.Data == nil {
cm.Data = map[string]string{}
}
cm.Data["prometheus.yaml"] = config
if data.InClusterPrometheusDisableDefaultRules() {
delete(cm.Data, "rules.yaml")
} else {
cm.Data["rules.yaml"] = prometheusRules
}
if customRules == "" {
delete(cm.Data, "rules-custom.yaml")
} else {
cm.Data["rules-custom.yaml"] = customRules
}
// make sure all files end with exactly one empty line to prevent needless pod restarts
for k, v := range cm.Data {
cm.Data[k] = strings.TrimSpace(v) + "\n"
}
return cm, nil
}
}
}
func loadTemplatedFile(file string, data *customizationData) (string, error) {
if file == "" {
return "", nil
}
content, err := ioutil.ReadFile(file)
if err != nil {
return "", fmt.Errorf("couldn't read file: %v", err)
}
return renderTemplate(string(content), data)
}
func renderTemplate(tpl string, data interface{}) (string, error) {
t, err := template.New("base").Funcs(sprig.TxtFuncMap()).Parse(tpl)
if err != nil {
return "", fmt.Errorf("failed to parse as Go template: %v", err)
}
output := bytes.Buffer{}
if err := t.Execute(&output, data); err != nil {
return "", fmt.Errorf("failed to render template: %v", err)
}
return strings.TrimSpace(output.String()), nil
}
const prometheusConfig = `
global:
evaluation_interval: 30s
scrape_interval: 30s
external_labels:
cluster: "{{ .TemplateData.Cluster.Name }}"
seed_cluster: "{{ .TemplateData.Seed.Name }}"
rule_files:
- "/etc/prometheus/config/rules*.yaml"
alerting:
alertmanagers:
- dns_sd_configs:
# configure the Seed's alertmanager for the user cluster
- names:
- 'alertmanager.monitoring.svc.cluster.local'
type: A
port: 9093
scrape_configs:
{{- if not .TemplateData.InClusterPrometheusDisableDefaultScrapingConfigs }}
#######################################################################
# These rules will scrape pods running inside the seed cluster.
# scrape the etcd pods
- job_name: etcd
scheme: https
tls_config:
{{ .EtcdTLSConfig | indent 4 }}
static_configs:
- targets:
- 'etcd-0.etcd.{{ .TemplateData.Cluster.Status.NamespaceName }}.svc.cluster.local:2379'
- 'etcd-1.etcd.{{ .TemplateData.Cluster.Status.NamespaceName }}.svc.cluster.local:2379'
- 'etcd-2.etcd.{{ .TemplateData.Cluster.Status.NamespaceName }}.svc.cluster.local:2379'
relabel_configs:
- source_labels: [__address__]
regex: (etcd-\d+).+
action: replace
replacement: $1
target_label: instance
# scrape the cluster's control plane (apiserver, controller-manager, scheduler)
- job_name: kubernetes-control-plane
scheme: https
tls_config:
{{ .ApiserverTLSConfig | indent 4 }}
# insecure_skip_verify is needed because the apiservers certificate
# does not contain a common name for the pod's ip address
insecure_skip_verify: true
kubernetes_sd_configs:
- role: pod
namespaces:
names:
- "{{ .TemplateData.Cluster.Status.NamespaceName }}"
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape_with_kube_cert]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
- source_labels: [__meta_kubernetes_pod_label_app]
action: replace
target_label: job
# drop very expensive apiserver metrics
metric_relabel_configs:
- source_labels: [__name__]
regex: 'apiserver_request_(duration|latencies)_.*'
action: drop
- source_labels: [__name__]
regex: 'apiserver_response_sizes_.*'
action: drop
# scrape other cluster control plane components, like kube-state-metrics, DNS resolver,
# machine-controller etcd.
- job_name: control-plane-pods
kubernetes_sd_configs:
- role: pod
namespaces:
names:
- "{{ $.TemplateData.Cluster.Status.NamespaceName }}"
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app, __meta_kubernetes_pod_container_init]
regex: "kube-state-metrics;true"
action: drop
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- source_labels: [__meta_kubernetes_pod_label_role, __meta_kubernetes_pod_label_app]
action: replace
target_label: job
separator: ''
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
#######################################################################
# These rules will scrape pods running inside the user cluster itself.
# scrape node metrics
- job_name: nodes
scheme: https
tls_config:
{{ .ApiserverTLSConfig | indent 4 }}
kubernetes_sd_configs:
- role: node
api_server: 'https://{{ .APIServerHost }}'
tls_config:
{{ .ApiserverTLSConfig | indent 6 }}
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: '{{ .APIServerHost }}'
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics
# scrape node cadvisor
- job_name: cadvisor
scheme: https
tls_config:
{{ .ApiserverTLSConfig | indent 4 }}
kubernetes_sd_configs:
- role: node
api_server: 'https://{{ .APIServerHost }}'
tls_config:
{{ .ApiserverTLSConfig | indent 6 }}
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: '{{ .APIServerHost }}'
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
# scrape pods inside the user cluster with a special annotation
- job_name: 'user-cluster-pods'
scheme: https
tls_config:
{{ .ApiserverTLSConfig | indent 4 }}
kubernetes_sd_configs:
- role: pod
api_server: 'https://{{ .APIServerHost }}'
tls_config:
{{ .ApiserverTLSConfig | indent 6 }}
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_{{ .TemplateData.MonitoringScrapeAnnotationPrefix }}_port]
action: keep
regex: \d+
- source_labels: [__meta_kubernetes_pod_annotation_{{ .TemplateData.MonitoringScrapeAnnotationPrefix }}_path]
regex: (.+)
action: replace
target_label: __metrics_path__
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_pod_name, __meta_kubernetes_pod_annotation_{{ .TemplateData.MonitoringScrapeAnnotationPrefix }}_port, __metrics_path__]
action: replace
regex: (.*);(.*);(.*);(.*)
target_label: __metrics_path__
replacement: /api/v1/namespaces/${1}/pods/${2}:${3}/proxy${4}
- target_label: __address__
replacement: '{{ .APIServerHost }}'
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
{{- end }}
{{- with .CustomScrapingConfigs }}
#######################################################################
# custom scraping configurations
{{ . }}
{{- end }}
`