-
Notifications
You must be signed in to change notification settings - Fork 356
/
prometheus.jsonnet
319 lines (292 loc) · 10.8 KB
/
prometheus.jsonnet
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
local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
local serviceAccount = k.core.v1.serviceAccount;
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local secret = k.core.v1.secret;
local clusterRole = k.rbac.v1.clusterRole;
local policyRule = clusterRole.rulesType;
local selector = k.apps.v1beta2.deployment.mixin.spec.selectorType;
local authenticationRole = policyRule.new() +
policyRule.withApiGroups(['authentication.k8s.io']) +
policyRule.withResources([
'tokenreviews',
]) +
policyRule.withVerbs(['create']);
local authorizationRole = policyRule.new() +
policyRule.withApiGroups(['authorization.k8s.io']) +
policyRule.withResources([
'subjectaccessreviews',
]) +
policyRule.withVerbs(['create']);
local namespacesRole = policyRule.new() +
policyRule.withApiGroups(['']) +
policyRule.withResources([
'namespaces',
]) +
policyRule.withVerbs(['get']);
{
prometheus+:: {
// OpenShift route to access the Prometheus UI.
route: {
apiVersion: 'v1',
kind: 'Route',
metadata: {
name: 'prometheus-k8s',
namespace: $._config.namespace,
},
spec: {
to: {
kind: 'Service',
name: 'prometheus-k8s',
},
port: {
targetPort: 'web',
},
tls: {
termination: 'Reencrypt',
},
},
},
// The ServiceAccount needs this annotation, to signify the identity
// provider, that when a users it doing the oauth flow through the
// oauth proxy, that it should redirect to the prometheus-k8s route on
// successful authentication.
serviceAccount+:
serviceAccount.mixin.metadata.withAnnotations({
'serviceaccounts.openshift.io/oauth-redirectreference.prometheus-k8s': '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"prometheus-k8s"}}',
}),
// Adding the serving certs annotation causes the serving certs controller
// to generate a valid and signed serving certificate and put it in the
// specified secret.
//
// The ClusterIP is explicitly set, as it signifies the
// cluster-monitoring-operator, that when reconciling this service the
// cluster IP needs to be retained.
//
// The ports are overridden, as due to the port binding of the oauth proxy
// the serving port is 9091 instead of the 9090 default.
service+:
service.mixin.metadata.withAnnotations({
'service.alpha.openshift.io/serving-cert-secret-name': 'prometheus-k8s-tls',
}) +
service.mixin.spec.withType('ClusterIP') +
service.mixin.spec.withPorts(servicePort.newNamed('web', 9091, 'web')),
// As Prometheus is protected by the oauth proxy it requires the
// ability to create TokenReview and SubjectAccessReview requests.
// Additionally in order to authenticate with the Alertmanager it
// requires `get` method on all `namespaces`, which is the
// SubjectAccessReview required by the Alertmanager instances.
clusterRole+:
clusterRole.withRulesMixin([authenticationRole, authorizationRole, namespacesRole]),
// OpenShift currently has the kube-controller-manager and
// kube-scheduler combined in one component called the
// kube-controllers. This Service and ServiceMonitor enable scraping
// its metrics.
kubeControllersService:
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local kubeControllersPort = servicePort.newNamed('http-metrics', 8444, 8444);
service.new('kube-controllers', {
'openshift.io/component': 'controllers',
'openshift.io/control-plane': 'true',
}, kubeControllersPort) +
service.mixin.metadata.withNamespace('kube-system') +
service.mixin.metadata.withLabels({ 'k8s-app': 'kube-controllers' }) +
service.mixin.spec.withClusterIp('None'),
serviceMonitorKubeControllers:
{
apiVersion: 'monitoring.coreos.com/v1',
kind: 'ServiceMonitor',
metadata: {
labels: {
'k8s-app': 'kube-controllers',
},
name: 'kube-controllers',
},
spec: {
endpoints: [
{
bearerTokenFile: '/var/run/secrets/kubernetes.io/serviceaccount/token',
interval: '30s',
port: 'http-metrics',
scheme: 'https',
tlsConfig: {
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
},
},
],
jobLabel: 'k8s-app',
namespaceSelector: {
matchNames: ['kube-system'],
},
selector: {
matchLabels: {
'k8s-app': 'kube-controllers',
},
},
},
},
// The proxy secret is there to encrypt session created by the oauth proxy.
proxySecret:
secret.new('prometheus-k8s-proxy', {}) +
secret.mixin.metadata.withNamespace($._config.namespace) +
secret.mixin.metadata.withLabels({ 'k8s-app': 'prometheus-k8s' }),
htpasswdSecret:
secret.new('prometheus-k8s-htpasswd', {}) +
secret.mixin.metadata.withNamespace($._config.namespace) +
secret.mixin.metadata.withLabels({ 'k8s-app': 'prometheus-k8s' }),
// This changes the kubelet's certificates to be validated when
// scraping.
serviceMonitorKubelet:
{
apiVersion: 'monitoring.coreos.com/v1',
kind: 'ServiceMonitor',
metadata: {
labels: {
'k8s-app': 'kubelet',
},
name: 'kubelet',
},
spec: {
endpoints: [
{
bearerTokenFile: '/var/run/secrets/kubernetes.io/serviceaccount/token',
interval: '30s',
port: 'https-metrics',
scheme: 'https',
tlsConfig: {
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
},
},
{
bearerTokenFile: '/var/run/secrets/kubernetes.io/serviceaccount/token',
honorLabels: true,
interval: '30s',
path: '/metrics/cadvisor',
port: 'https-metrics',
scheme: 'https',
tlsConfig: {
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
},
},
],
jobLabel: 'k8s-app',
namespaceSelector: {
matchNames: ['kube-system'],
},
selector: {
matchLabels: {
'k8s-app': 'kubelet',
},
},
},
},
serviceMonitorEtcd+:
{
metadata+: {
namespace: $._config.namespace,
},
spec+: {
namespaceSelector: {
matchNames: ['kube-system'],
},
},
},
// This changes the Prometheuses to be scraped with TLS, authN and
// authZ, which are not present in kube-prometheus.
serviceMonitor+:
{
spec+: {
endpoints: [
{
port: 'web',
interval: '30s',
scheme: 'https',
tlsConfig: {
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt',
serverName: 'prometheus-k8s',
},
bearerTokenFile: '/var/run/secrets/kubernetes.io/serviceaccount/token',
},
],
},
},
// These patches inject the oauth proxy as a sidecar and configures it with
// TLS. Additionally as the Alertmanager is protected with TLS, authN and
// authZ it requires some additonal configuration.
prometheus+:
{
spec+: {
alerting+: {
alertmanagers:
std.map(
function(a) a {
scheme: 'https',
tlsConfig: {
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt',
serverName: 'alertmanager-main',
},
bearerTokenFile: '/var/run/secrets/kubernetes.io/serviceaccount/token',
},
super.alertmanagers,
),
},
securityContext: {},
resources: {},
secrets+: [
'prometheus-k8s-tls',
'prometheus-k8s-proxy',
'prometheus-k8s-htpasswd',
],
serviceMonitorSelector: selector.withMatchExpressions({ key: 'k8s-app', operator: 'Exists' }),
serviceMonitorNamespaceSelector: selector.withMatchExpressions({ key: 'openshift.io/cluster-monitoring', operator: 'Exists' }),
listenLocal: true,
containers: [
{
name: 'prometheus-proxy',
image: $._config.imageRepos.openshiftOauthProxy + ':' + $._config.versions.openshiftOauthProxy,
resources: {},
ports: [
{
containerPort: 9091,
name: 'web',
},
],
args: [
'-provider=openshift',
'-https-address=:9091',
'-http-address=',
'-email-domain=*',
'-upstream=http://localhost:9090',
'-htpasswd-file=/etc/proxy/htpasswd/auth',
'-openshift-service-account=prometheus-k8s',
'-openshift-sar={"resource": "namespaces", "verb": "get"}',
'-openshift-delegate-urls={"/": {"resource": "namespaces", "verb": "get"}}',
'-tls-cert=/etc/tls/private/tls.crt',
'-tls-key=/etc/tls/private/tls.key',
'-client-secret-file=/var/run/secrets/kubernetes.io/serviceaccount/token',
'-cookie-secret-file=/etc/proxy/secrets/session_secret',
'-openshift-ca=/etc/pki/tls/cert.pem',
'-openshift-ca=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
'-skip-auth-regex=^/metrics',
],
volumeMounts: [
{
mountPath: '/etc/tls/private',
name: 'secret-prometheus-k8s-tls',
},
{
mountPath: '/etc/proxy/secrets',
name: 'secret-prometheus-k8s-proxy',
},
{
mountPath: '/etc/proxy/htpasswd',
name: 'secret-prometheus-k8s-htpasswd',
},
],
},
],
},
},
},
}