diff --git a/charts/docker-mailserver/README.md b/charts/docker-mailserver/README.md index 98ce6f5..f754c61 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 af5a38d..2732642 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 0000000..f772ca1 --- /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 1d62aa1..df9dddd 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 d362b3d..6d99761 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 0000000..fbde65d --- /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 a0bf553..8331340 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"