Skip to content

Commit

Permalink
Add (optional) dedicated metrics service (#727)
Browse files Browse the repository at this point in the history
Signed-off-by: Freddy Grieshaber <[email protected]>
Co-authored-by: Michel Loiseleur <[email protected]>
  • Loading branch information
grieshaber and mloiseleur authored Nov 21, 2022
1 parent f4a7bec commit 498a645
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 5 deletions.
31 changes: 30 additions & 1 deletion traefik/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
# Change Log

## 20.4.0 ![AppVersion: v2.9.4](https://img.shields.io/static/v1?label=AppVersion&message=v2.9.4&color=success&logo=) ![Helm: v3](https://img.shields.io/static/v1?label=Helm&message=v3&color=informational&logo=helm)

**Release date:** 2022-11-18

* Add (optional) dedicated metrics service

### Default value changes

```diff
diff --git a/traefik/values.yaml b/traefik/values.yaml
index ca15f6a..46690aa 100644
--- a/traefik/values.yaml
+++ b/traefik/values.yaml
@@ -266,7 +266,13 @@ metrics:
# address: localhost:8125
##
## enable optional CRDs for Prometheus Operator
##
+ ## Create a dedicated metrics service for use with ServiceMonitor
+ ## When hub.enabled is set to true, it's not needed: it will use hub service.
+ # service:
+ # enabled: false
+ # labels: {}
+ # annotations: {}
# serviceMonitor:
# metricRelabelings: []
# - sourceLabels: [__name__]
```

## 20.3.1 ![AppVersion: v2.9.4](https://img.shields.io/static/v1?label=AppVersion&message=v2.9.4&color=success&logo=) ![Helm: v3](https://img.shields.io/static/v1?label=Helm&message=v3&color=informational&logo=helm)

**Release date:** 2022-11-21

* 🐛 Fix namespace override which was missing on `ServiceAccount`
* 🐛 Fix namespace override which was missing on `ServiceAccount` (#731)


## 20.3.0 ![AppVersion: v2.9.4](https://img.shields.io/static/v1?label=AppVersion&message=v2.9.4&color=success&logo=) ![Helm: v3](https://img.shields.io/static/v1?label=Helm&message=v3&color=informational&logo=helm)
Expand Down
5 changes: 2 additions & 3 deletions traefik/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: traefik
description: A Traefik based Kubernetes ingress controller
type: application
version: 20.3.1
version: 20.4.0
appVersion: v2.9.4
keywords:
- traefik
Expand All @@ -24,6 +24,5 @@ maintainers:
email: [email protected]
icon: https://raw.githubusercontent.com/traefik/traefik/v2.3/docs/content/assets/img/traefik.logo.png
annotations:
artifacthub.io/changes: "- Adds support for namespace overrides in subchart use\n- Document recent changes in the README (#717)\n"
artifacthub.io/changes: |
- 🐛 Fix namespace override which was missing on `ServiceAccount`
- Add (optional) dedicated metrics service
22 changes: 22 additions & 0 deletions traefik/templates/_service-metrics.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{- define "traefik.metrics-service-metadata" }}
labels:
{{- include "traefik.metricsservicelabels" . | nindent 4 -}}
{{- with .Values.metrics.prometheus.service.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}

{{/* Labels used for metrics-relevant selector*/}}
{{/* This is an immutable field: this should not change between upgrade */}}
{{- define "traefik.metricslabelselector" -}}
{{- include "traefik.labelselector" . }}
app.kubernetes.io/component: metrics
{{- end }}

{{/* Shared labels used in metadata of metrics-service and servicemonitor */}}
{{- define "traefik.metricsservicelabels" -}}
{{ include "traefik.metricslabelselector" . }}
helm.sh/chart: {{ template "traefik.chart" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

30 changes: 30 additions & 0 deletions traefik/templates/service-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{- $fullname := include "traefik.fullname" . }}
{{- if ge (len $fullname) 50 }}
{{- fail "ERROR: Cannot create a metrics service when name contains more than 50 characters" }}
{{- end }}

{{- if (and (.Values.metrics.prometheus.service).enabled (not .Values.hub.enabled)) -}}
apiVersion: v1
kind: Service
metadata:
name: {{ $fullname }}-metrics
namespace: {{ template "traefik.namespace" . }}
{{- template "traefik.metrics-service-metadata" . }}
annotations:
{{- with .Values.metrics.prometheus.service.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
type: ClusterIP
selector:
{{- include "traefik.labelselector" . | nindent 4 }}
ports:
- port: {{ .Values.ports.metrics.port }}
name: "metrics"
targetPort: metrics
protocol: TCP
{{- if .Values.ports.metrics.nodePort }}
nodePort: {{ .Values.ports.metrics.nodePort }}
{{- end }}
{{- end -}}

8 changes: 8 additions & 0 deletions traefik/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ metadata:
namespace: {{ . }}
{{- end }}
labels:
{{- if (and (.Values.metrics.prometheus.service).enabled (not .Values.hub.enabled)) }}
{{- include "traefik.metricsservicelabels" . | nindent 4 }}
{{- else }}
{{- include "traefik.labels" . | nindent 4 }}
{{- end }}
{{- with .Values.metrics.prometheus.serviceMonitor.additionalLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
Expand Down Expand Up @@ -56,6 +60,10 @@ spec:
{{- end }}
selector:
matchLabels:
{{- if (and (.Values.metrics.prometheus.service).enabled (not .Values.hub.enabled)) }}
{{- include "traefik.metricslabelselector" . | nindent 6 }}
{{- else }}
{{- include "traefik.labelselector" . | nindent 6 }}
{{- end }}
{{- end }}
{{- end }}
53 changes: 53 additions & 0 deletions traefik/tests/service-metrics-config_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
suite: Metrics Service configuration
templates:
- service-metrics.yaml
tests:
- it: should not provide a dedicated metrics service by default
asserts:
- hasDocuments:
count: 0
- it: should not provide a dedicated metrics service when hub is enabled
set:
hub:
enabled: true
metrics:
prometheus:
service:
enabled: true
asserts:
- hasDocuments:
count: 0
- it: should provide a dedicated metrics service when enabled set to true
set:
metrics:
prometheus:
service:
enabled: true
labels:
custom-label: label-value
annotations:
custom-annotation: annotation-value
asserts:
- hasDocuments:
count: 1
- equal:
path: spec.type
value: ClusterIP
- equal:
path: metadata.labels.custom-label
value: label-value
- equal:
path: metadata.annotations.custom-annotation
value: annotation-value
- equal:
path: spec.selector
value:
app.kubernetes.io/name: traefik
app.kubernetes.io/instance: RELEASE-NAME-NAMESPACE
- contains:
path: spec.ports
content:
port: 9100
name: "metrics"
targetPort: metrics
protocol: TCP
39 changes: 39 additions & 0 deletions traefik/tests/servicemonitor-config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,42 @@ tests:
- equal:
path: spec.selector.matchLabels.[app.kubernetes.io/name]
value: traefik
- it: should provide ServiceMonitor pointing to metrics service, when hub is disabled and prometheus.service.enabled is true
values:
- ./values/servicemonitor.yaml
capabilities:
apiVersions:
- monitoring.coreos.com/v1
set:
hub:
enabled: false
metrics:
prometheus:
service:
enabled: true
asserts:
- hasDocuments:
count: 1
- equal:
path: spec.selector.matchLabels.[app.kubernetes.io/component]
value: metrics
- it: should provide ServiceMonitor pointing to hub service, when hub is enabled
values:
- ./values/servicemonitor.yaml
capabilities:
apiVersions:
- monitoring.coreos.com/v1
set:
hub:
enabled: true
metrics:
prometheus:
service:
enabled: true
asserts:
- hasDocuments:
count: 1
- isNotSubset:
path: spec.selector.matchLabels
content:
app.kubernetes.io/component: metrics
1 change: 0 additions & 1 deletion traefik/tests/values/servicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
metrics:
prometheus:
serviceMonitor:
enabled: true
additionalLabels:
release: traefik-release
namespace: another-namespace
Expand Down
6 changes: 6 additions & 0 deletions traefik/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ metrics:
##
## enable optional CRDs for Prometheus Operator
##
## Create a dedicated metrics service for use with ServiceMonitor
## When hub.enabled is set to true, it's not needed: it will use hub service.
# service:
# enabled: false
# labels: {}
# annotations: {}
# serviceMonitor:
# metricRelabelings: []
# - sourceLabels: [__name__]
Expand Down

0 comments on commit 498a645

Please sign in to comment.