From 1c01e6f2c06d116f6e8aa20a511109e235450349 Mon Sep 17 00:00:00 2001 From: Martino Dell'Ambrogio Date: Mon, 23 Nov 2026 08:58:43 +0100 Subject: [PATCH] Serve the Rspamd web interface and metrics on a ClusterIP Service The main Service carried every port, including the Rspamd web interface (11334) and the metrics exporter. With service.type LoadBalancer or NodePort, both were then reachable from outside the cluster, and the Rspamd controller could be reached directly, bypassing any authentication the rspamd ingress puts in front of it. Both ports now live only on a new ClusterIP Service, -internal, and the main Service no longer has them. The internal Service also carries the prometheus.io/* annotations (it is the one with the metrics port), and takes service.labels, service.ipFamilyPolicy and service.ipFamilies like the main Service. The rspamd ingress routes to it, and the ServiceMonitor selects it by app.kubernetes.io/component=internal, so a scrape never hits both Services. The README documents the change under Ports. Tests: tests/service_test.yaml covers the main Service without the ports and annotations, the internal Service's ports, annotations, labels and IP families, the empty case, the ingress backend and the ServiceMonitor selector. helm lint and kube-score pass. --- charts/docker-mailserver/README.md | 7 + .../templates/ingress-rspamd.yaml | 2 +- .../templates/service-internal.yaml | 48 +++++++ .../docker-mailserver/templates/service.yaml | 22 +--- .../templates/servicemonitor.yaml | 1 + .../docker-mailserver/tests/service_test.yaml | 124 ++++++++++++++++++ charts/docker-mailserver/values.yaml | 3 +- 7 files changed, 184 insertions(+), 23 deletions(-) create mode 100644 charts/docker-mailserver/templates/service-internal.yaml create mode 100644 charts/docker-mailserver/tests/service_test.yaml diff --git a/charts/docker-mailserver/README.md b/charts/docker-mailserver/README.md index 98ce6f54..f754c616 100644 --- a/charts/docker-mailserver/README.md +++ b/charts/docker-mailserver/README.md @@ -14,6 +14,7 @@ - [Minimal Configuration](#minimal-configuration) - [Certificate](#certificate) - [Ports](#ports) + - [Rspamd web interface and metrics](#rspamd-web-interface-and-metrics) - [Persistence](#persistence) - [Backing Storage](#backing-storage) - [Generic / All](#generic--all) @@ -248,6 +249,12 @@ If you do not enable the PROXY protocol and your mail server is not exposed usin [dms-docs::k8s::proxy-protocol]: https://docker-mailserver.github.io/docker-mailserver/latest/config/advanced/kubernetes/#proxy-port-to-service-via-proxy-protocol [k3s-klipperlb-pp]: https://github.com/docker-mailserver/docker-mailserver-helm/issues/176#issuecomment-3097915161 +### Rspamd web interface and metrics + +The Rspamd web interface (port 11334) and the metrics exporter (`monitoring.service.port`, 9102 by default) are not on the main Service. They are on a separate ClusterIP Service named `-internal`, so a main Service of type `LoadBalancer` or `NodePort` never publishes them outside the cluster. The Rspamd ingress (`rspamd.ingress`) and the ServiceMonitor (`metrics.serviceMonitor`) use the internal Service, and it carries the `prometheus.io/*` annotations. `service.labels`, `service.ipFamilyPolicy` and `service.ipFamilies` apply to both Services. + +If you reached the Rspamd web interface or scraped the metrics through the main Service before (a port-forward, an annotation-based scrape, or an external address), use `-internal` instead. + ## Persistence Docker-mailserver assumes there are [four](https://docker-mailserver.github.io/docker-mailserver/latest/config/advanced/optional-config/#volumes) mounted volumes: diff --git a/charts/docker-mailserver/templates/ingress-rspamd.yaml b/charts/docker-mailserver/templates/ingress-rspamd.yaml index af5a38d7..27326422 100644 --- a/charts/docker-mailserver/templates/ingress-rspamd.yaml +++ b/charts/docker-mailserver/templates/ingress-rspamd.yaml @@ -22,7 +22,7 @@ spec: path: {{ .Values.rspamd.ingress.path }} backend: service: - name: {{ template "dockermailserver.fullname" . }} + name: {{ template "dockermailserver.fullname" . }}-internal port: name: rspamd diff --git a/charts/docker-mailserver/templates/service-internal.yaml b/charts/docker-mailserver/templates/service-internal.yaml new file mode 100644 index 00000000..f772ca17 --- /dev/null +++ b/charts/docker-mailserver/templates/service-internal.yaml @@ -0,0 +1,48 @@ +{{- if or .Values.deployment.env.ENABLE_RSPAMD .Values.metrics.enabled }} +--- +## The Rspamd web interface and the metrics exporter live on this ClusterIP +## Service, never on the main Service, so a LoadBalancer or NodePort main +## Service does not publish them outside the cluster. The rspamd ingress and the +## ServiceMonitor use this Service. +kind: "Service" +apiVersion: "v1" +metadata: + annotations: + ## These annotations mark the service as monitorable by Prometheus, both directly as a service level metric and + ## via the blackbox exporter. For more information, see values.yaml + prometheus.io/scrape: {{ .Values.monitoring.service.scrape | quote }} + prometheus.io/probe: {{ .Values.monitoring.service.probe | quote }} + prometheus.io/path: {{ .Values.monitoring.service.path | quote }} + prometheus.io/port: {{ .Values.monitoring.service.port | quote }} + labels: + app.kubernetes.io/name: {{ template "dockermailserver.fullname" . }} + app.kubernetes.io/component: internal + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + heritage: "{{ .Release.Service }}" + release: "{{ .Release.Name }}" +{{- if .Values.service.labels }} +{{ toYaml .Values.service.labels | indent 4 }} +{{- end }} + name: {{ template "dockermailserver.fullname" . }}-internal +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: {{ template "dockermailserver.fullname" . }} + release: "{{ .Release.Name }}" + ports: + {{- if .Values.deployment.env.ENABLE_RSPAMD }} + - name: rspamd + targetPort: rspamd + port: 11334 + {{- end }} + {{- if .Values.metrics.enabled }} + - name: metrics + targetPort: metrics + port: {{ .Values.monitoring.service.port }} + {{- end }} + ipFamilyPolicy: {{ default "SingleStack" .Values.service.ipFamilyPolicy }} + {{- if .Values.service.ipFamilies }} + ipFamilies: + {{- toYaml .Values.service.ipFamilies | nindent 2 }} + {{- end }} +{{- end }} diff --git a/charts/docker-mailserver/templates/service.yaml b/charts/docker-mailserver/templates/service.yaml index 1d62aa1d..df9dddd0 100644 --- a/charts/docker-mailserver/templates/service.yaml +++ b/charts/docker-mailserver/templates/service.yaml @@ -2,16 +2,8 @@ kind: "Service" apiVersion: "v1" metadata: - annotations: - ## These annontations mark the service as monitorable by Prometheus, both directly as a service level metric and - ## via the blackbox exporter. For more information, see - ## values.yaml - prometheus.io/scrape: {{ .Values.monitoring.service.scrape | quote }} - prometheus.io/probe: {{ .Values.monitoring.service.probe | quote }} - prometheus.io/path: {{ .Values.monitoring.service.path | quote }} - prometheus.io/port: {{ .Values.monitoring.service.port | quote }} - ## {{- if .Values.service.annotations }} + annotations: {{ toYaml .Values.service.annotations | indent 4 }} {{ end }} labels: @@ -118,12 +110,6 @@ spec: {{- end }} {{- end }} - {{- if .Values.deployment.env.ENABLE_RSPAMD }} - - name: rspamd - targetPort: rspamd - port: 11334 - {{- end }} - {{- if and (.Values.deployment.env.ENABLE_MANAGESIEVE) (not .Values.deployment.env.SMTP_ONLY) }} - name: managesieve targetPort: managesieve @@ -135,12 +121,6 @@ spec: {{- end }} {{- end }} - {{- if .Values.metrics.enabled }} - - name: metrics - port: {{ .Values.monitoring.service.port }} - targetPort: metrics - {{- end }} - type: {{ default "ClusterIP" .Values.service.type }} {{- if .Values.service.clusterIp }} clusterIP: {{ .Values.service.clusterIp }} diff --git a/charts/docker-mailserver/templates/servicemonitor.yaml b/charts/docker-mailserver/templates/servicemonitor.yaml index d362b3d1..6d997619 100644 --- a/charts/docker-mailserver/templates/servicemonitor.yaml +++ b/charts/docker-mailserver/templates/servicemonitor.yaml @@ -23,6 +23,7 @@ spec: selector: matchLabels: app.kubernetes.io/name: {{ template "dockermailserver.fullname" . }} + app.kubernetes.io/component: internal release: "{{ .Release.Name }}" {{- end }} diff --git a/charts/docker-mailserver/tests/service_test.yaml b/charts/docker-mailserver/tests/service_test.yaml new file mode 100644 index 00000000..fbde65d5 --- /dev/null +++ b/charts/docker-mailserver/tests/service_test.yaml @@ -0,0 +1,124 @@ +suite: service tests +templates: + - service.yaml + - service-internal.yaml + - ingress-rspamd.yaml + - servicemonitor.yaml +chart: + version: 0.1.0 + appVersion: 0.1.0 +values: + - ../values.yaml +set: + service.type: LoadBalancer + metrics.enabled: true + metrics.serviceMonitor.enabled: true + rspamd.ingress.enabled: true +tests: + - it: keeps the rspamd and metrics ports off the main service + template: service.yaml + asserts: + - notContains: + path: spec.ports + any: true + content: + name: rspamd + - notContains: + path: spec.ports + any: true + content: + name: metrics + - contains: + path: spec.ports + any: true + content: + name: smtp + + - it: leaves the prometheus annotations off the main service + template: service.yaml + asserts: + - notExists: + path: metadata.annotations["prometheus.io/scrape"] + - notExists: + path: metadata.annotations["prometheus.io/port"] + + - it: serves the rspamd and metrics ports on a ClusterIP service + template: service-internal.yaml + asserts: + - isKind: + of: Service + - matchRegex: + path: metadata.name + pattern: -docker-mailserver-internal$ + - equal: + path: spec.type + value: ClusterIP + - equal: + path: metadata.labels["app.kubernetes.io/component"] + value: internal + - equal: + path: spec.ports + value: + - name: rspamd + targetPort: rspamd + port: 11334 + - name: metrics + targetPort: metrics + port: 9102 + + - it: carries the prometheus annotations on the internal service + template: service-internal.yaml + asserts: + - equal: + path: metadata.annotations["prometheus.io/scrape"] + value: "true" + - equal: + path: metadata.annotations["prometheus.io/port"] + value: "9102" + - equal: + path: metadata.annotations["prometheus.io/path"] + value: /metrics + + - it: applies service.labels and the IP family settings to the internal service + template: service-internal.yaml + set: + service.labels: + team: mail + service.ipFamilyPolicy: PreferDualStack + service.ipFamilies: [IPv4, IPv6] + asserts: + - equal: + path: metadata.labels.team + value: mail + - equal: + path: spec.ipFamilyPolicy + value: PreferDualStack + - equal: + path: spec.ipFamilies + value: [IPv4, IPv6] + + - it: renders no internal service when rspamd and metrics are both off + template: service-internal.yaml + set: + deployment.env.ENABLE_RSPAMD: 0 + metrics.enabled: false + asserts: + - hasDocuments: + count: 0 + + - it: routes the rspamd ingress to the internal service + template: ingress-rspamd.yaml + asserts: + - matchRegex: + path: spec.rules[0].http.paths[0].backend.service.name + pattern: -docker-mailserver-internal$ + - equal: + path: spec.rules[0].http.paths[0].backend.service.port.name + value: rspamd + + - it: scrapes only the internal service, so metrics are not collected twice + template: servicemonitor.yaml + asserts: + - equal: + path: spec.selector.matchLabels["app.kubernetes.io/component"] + value: internal diff --git a/charts/docker-mailserver/values.yaml b/charts/docker-mailserver/values.yaml index a0bf5534..83313407 100644 --- a/charts/docker-mailserver/values.yaml +++ b/charts/docker-mailserver/values.yaml @@ -423,7 +423,8 @@ persistence: ## See https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus-kubernetes.yml monitoring: ## Whether to scrape this service with the monitoring toolkit. Mostly useful for blackbox probing of a given service - ## to ensure it's "up" + ## to ensure it's "up". These prometheus.io/* annotations go on the -internal Service, which carries the + ## metrics port (the main Service does not). service: ## monitoring should be configured to only scrape services that have a value of "true" scrape: "true"