-
Notifications
You must be signed in to change notification settings - Fork 354
/
monitoring-plugin.libsonnet
270 lines (238 loc) · 7.66 KB
/
monitoring-plugin.libsonnet
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
function(params)
local cfg = params;
local pluginName = 'monitoring-plugin';
local pluginLabels = {
'app.kubernetes.io/name': pluginName,
'app.kubernetes.io/component': pluginName,
'app.kubernetes.io/managed-by': 'cluster-monitoring-operator',
} + cfg.commonLabels;
local nginxPort = 9443;
local nginxPortName = 'https';
local tlsSecret = 'monitoring-plugin-cert';
local tlsVolumeName = 'monitoring-plugin-cert';
local tlsMountPath = '/var/cert';
local nginxConf = |||
error_log /dev/stdout info;
events {}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen LISTEN_ADDRESS_PORT_REPLACED_AT_RUNTIME ssl;
ssl_certificate %(tlsPath)s/tls.crt;
ssl_certificate_key %(tlsPath)s/tls.key;
root /usr/share/nginx/html;
# Prevent caching for plugin-manifest.json
location = /plugin-manifest.json {
add_header Cache-Control 'no-cache, no-store, must-revalidate, proxy-revalidate, max-age=0';
add_header Pragma 'no-cache';
add_header Expires '0';
}
# Prevent caching for plugin-entry.js
location = /plugin-entry.js {
add_header Cache-Control 'no-cache, no-store, must-revalidate, proxy-revalidate, max-age=0';
add_header Pragma 'no-cache';
add_header Expires '0';
}
}
}
||| % { tlsPath: tlsMountPath, nginxPort: nginxPort };
local nginxConfigMap = pluginName;
local nginxCMVolName = 'nginx-conf';
local nginxConfMountPath = '/etc/nginx/nginx.conf';
{
_config+:: {
name: pluginName,
namespace: 'openshift-monitoring',
// NOTE: using pluginLabels ensures that both common and plugin labels
// are merged and allows all resources to just use $._config.labels
labels+:: pluginLabels,
image: 'IMAGE NOT SPECIFIED',
} + cfg,
// utility functions
metadata(noName=false, noNamespace=false)::
local name = if noName then {} else { name: $._config.name };
local ns = if noNamespace then {} else { namespace: $._config.namespace };
{ labels: $._config.labels } + name + ns,
configmapVolume(volName, cmName):: {
name: volName,
configMap: {
name: cmName,
defaultMode: 420,
},
},
volumeMount(name, path, subPath=''):: {
name: name,
mountPath: path,
readOnly: true,
} + if subPath == '' then {}
else { subPath: subPath }, // add sub-path if specified
configMapVolume(name, cmName):: {
name: name,
configMap: {
defaultMode: 420,
name: cmName,
},
},
secretVolume(name, secretName):: {
name: name,
secret: {
defaultMode: 420,
secretName: secretName,
},
},
// resources
configMap: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: $.metadata(),
data: {
'nginx.conf': nginxConf,
},
},
consolePlugin: {
apiVersion: 'console.openshift.io/v1',
kind: 'ConsolePlugin',
metadata: $.metadata(noNamespace=true),
spec: {
displayName: $._config.name,
backend: {
type: 'Service',
service: {
basePath: '/',
name: $._config.name,
namespace: $._config.namespace,
port: nginxPort,
},
},
},
},
podDisruptionBudget: {
apiVersion: 'policy/v1',
kind: 'PodDisruptionBudget',
metadata: $.metadata(),
spec: {
minAvailable: 1,
selector: {
matchLabels: $._config.labels,
},
},
},
serviceAccount: {
apiVersion: 'v1',
kind: 'ServiceAccount',
metadata: $.metadata(),
},
servicePort(name, port, targetPort):: {
name: name,
port: port,
targetPort: targetPort,
},
service: {
apiVersion: 'v1',
kind: 'Service',
metadata: $.metadata() + {
annotations: {
'service.beta.openshift.io/serving-cert-secret-name': tlsSecret,
},
},
spec: {
ports: [$.servicePort('https', 9443, 'https')],
selector: $._config.labels,
sessionAffinity: 'None',
},
},
deployment: {
apiVersion: 'apps/v1',
kind: 'Deployment',
metadata: $.metadata(),
spec: {
replicas: 2,
selector: {
matchLabels: $._config.labels,
},
strategy: {
rollingUpdate: { maxUnavailable: 1 },
type: 'RollingUpdate',
},
template: {
metadata: $.metadata(noName=true, noNamespace=true) + {
annotations: {
'target.workload.openshift.io/management': '{"effect": "PreferredDuringScheduling"}',
},
},
spec: {
affinity: {
podAntiAffinity: {
requiredDuringSchedulingIgnoredDuringExecution: [{
labelSelector: { matchLabels: $._config.labels },
namespaces: [$._config.namespace],
topologyKey: 'kubernetes.io/hostname',
}], // requiredDuringSchedulingIgnoredDuringExecution
}, // podAntiAffinity
}, // affinity
automountServiceAccountToken: false,
containers: [
{ // monitoring-plugin container
name: $._config.name,
image: $._config.image,
imagePullPolicy: 'IfNotPresent',
ports: [
{ containerPort: nginxPort, name: nginxPortName },
],
resources: {
requests: { cpu: '10m', memory: '50Mi' },
},
securityContext: {
allowPrivilegeEscalation: false,
capabilities: {
drop: ['ALL'],
},
},
volumeMounts: [
$.volumeMount(tlsVolumeName, tlsMountPath),
$.volumeMount(nginxCMVolName, nginxConfMountPath, 'nginx.conf'),
],
env: [
{
name: 'POD_IP',
valueFrom: {
fieldRef: {
fieldPath: 'status.podIP',
},
},
},
],
command: [
'/bin/sh',
'-c',
|||
if echo "$POD_IP" | grep -qE '^([0-9]{1,3}\.){3}[0-9]{1,3}$'; then
LISTEN_ADDRESS_PORT_REPLACED_AT_RUNTIME="9443"
else
LISTEN_ADDRESS_PORT_REPLACED_AT_RUNTIME="[::]:9443"
fi
sed "s/LISTEN_ADDRESS_PORT_REPLACED_AT_RUNTIME/$LISTEN_ADDRESS_PORT_REPLACED_AT_RUNTIME/g" /etc/nginx/nginx.conf > /tmp/nginx.conf
exec nginx -c /tmp/nginx.conf -g 'daemon off;'
|||,
],
}, // monitoring-plugin container
], // containers
dnsPolicy: 'ClusterFirst',
nodeSelector: { 'kubernetes.io/os': 'linux' },
priorityClassName: 'system-cluster-critical',
restartPolicy: 'Always',
securityContext: {
runAsNonRoot: true,
seccompProfile: { type: 'RuntimeDefault' },
},
volumes: [
$.configMapVolume(nginxCMVolName, nginxConfigMap),
$.secretVolume(tlsVolumeName, tlsSecret),
],
}, // spec
}, // template
}, // spec
}, // deployment
}