Skip to content

Commit a5185ec

Browse files
authored
Don't run Prometheus and Alertmanager as root. (#244)
* Don't run Prometheus and Alertmanager as root. Signed-off-by: Tom Wilkie <[email protected]> * Actually don't run alertmanager as root. Signed-off-by: Tom Wilkie <[email protected]>
1 parent 10de0f4 commit a5185ec

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

prometheus-ksonnet/lib/alertmanager.libsonnet

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,24 @@
121121
], self.alertmanager_pvc) +
122122
statefulset.mixin.spec.withServiceName('alertmanager') +
123123
statefulset.mixin.spec.template.metadata.withAnnotations({ 'prometheus.io.path': '%smetrics' % $._config.alertmanager_path }) +
124-
statefulset.mixin.spec.template.spec.securityContext.withRunAsUser(0) +
125-
statefulset.mixin.spec.template.spec.securityContext.withFsGroup(0) +
126124
$.util.configVolumeMount('alertmanager-config', '/etc/alertmanager/config') +
127125
$.util.podPriority('critical')
128126
else {},
129127

128+
local service = $.core.v1.service,
129+
local servicePort = service.mixin.spec.portsType,
130+
130131
// Do not create service in clusters without any alertmanagers.
131-
alertmanager_service: if replicas > 0 then
132-
$.util.serviceFor($.alertmanager_statefulset)
133-
else {},
132+
alertmanager_service:
133+
if replicas == 0
134+
then {}
135+
else
136+
$.util.serviceFor($.alertmanager_statefulset) +
137+
service.mixin.spec.withPortsMixin([
138+
servicePort.newNamed(
139+
name='http',
140+
port=80,
141+
targetPort=$._config.alertmanager_port,
142+
),
143+
]),
134144
}

prometheus-ksonnet/lib/config.libsonnet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
prometheus_insecure_skip_verify: self.insecureSkipVerify,
3737
prometheus_external_hostname: 'http://prometheus.%(namespace)s.svc.%(cluster_dns_suffix)s' % self,
3838
prometheus_path: '/prometheus/',
39-
prometheus_port: 80,
39+
prometheus_port: 9090,
4040
prometheus_web_route_prefix: $._config.prometheus_path,
4141

4242
// Alertmanager config options.
4343
alertmanager_external_hostname: 'http://alertmanager.%(namespace)s.svc.%(cluster_dns_suffix)s' % self,
4444
alertmanager_path: '/alertmanager/',
45-
alertmanager_port: 80,
45+
alertmanager_port: 9093,
4646
alertmanager_gossip_port: 9094,
4747
// Description of how many alertmanager replicas to run where. All
4848
// clusters with `'global': true` are participating in one global

prometheus-ksonnet/lib/prometheus.libsonnet

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
local _config = self._config;
3535

3636
container.new('prometheus', $._images.prometheus) +
37-
container.withPorts($.core.v1.containerPort.new('http-metrics', 80)) +
37+
container.withPorts($.core.v1.containerPort.new('http-metrics', _config.prometheus_port)) +
3838
container.withArgs([
3939
'--config.file=/etc/prometheus/prometheus.yml',
4040
'--web.listen-address=:%s' % _config.prometheus_port,
@@ -91,12 +91,26 @@
9191
statefulset.mixin.spec.template.metadata.withAnnotations({
9292
'prometheus.io.path': '%smetrics' % _config.prometheus_web_route_prefix,
9393
}) +
94-
statefulset.mixin.spec.template.spec.securityContext.withRunAsUser(0) +
9594
statefulset.mixin.spec.template.spec.withServiceAccount(self.name) +
95+
statefulset.mixin.spec.template.spec.securityContext.withFsGroup(2000) +
96+
statefulset.mixin.spec.template.spec.securityContext.withRunAsUser(1000) +
97+
statefulset.mixin.spec.template.spec.securityContext.withRunAsNonRoot(true) +
9698
$.util.podPriority('critical'),
9799

100+
local service = $.core.v1.service,
101+
local servicePort = service.mixin.spec.portsType,
102+
98103
prometheus_service:
99-
$.util.serviceFor(self.prometheus_statefulset),
104+
local _config = self._config;
105+
106+
$.util.serviceFor(self.prometheus_statefulset) +
107+
service.mixin.spec.withPortsMixin([
108+
servicePort.newNamed(
109+
name='http',
110+
port=80,
111+
targetPort=_config.prometheus_port,
112+
),
113+
]),
100114
},
101115

102116
main_prometheus: $.prometheus { name: 'prometheus' },

0 commit comments

Comments
 (0)