Skip to content

Commit

Permalink
feat(prometheus): Adding PodMonitor to autoconfigure prometheusOperator
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-jancik committed Oct 10, 2023
1 parent fab0794 commit 28f39fd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helm/postgres/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ name: postgrescluster
description: A Helm chart for Kubernetes
type: application
# This version has to be increased via any change of the helm chart (e.g., comment in default values or templates)
version: 5.4.2
version: 5.4.3
appVersion: 5.4.2
33 changes: 33 additions & 0 deletions helm/postgres/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "postgres.name" -}}
{{- default .Chart.Name .Values.name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "postgres.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "postgres.labels" -}}
helm.sh/chart: {{ include "postgres.chart" . }}
{{ include "postgres.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "postgres.selectorLabels" -}}
app.kubernetes.io/name: {{ include "postgres.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
73 changes: 73 additions & 0 deletions helm/postgres/templates/podmonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{{- if and .Values.monitoring .Values.podMonitor.enabled -}}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: {{ default .Release.Name .Values.name }}
{{- if .Values.podMonitor.namespace }}
namespace: {{ .Values.podMonitor.namespace | quote }}
{{- else }}
namespace: {{ .Release.Namespace }}
{{- end }}
labels:
{{- include "postgres.labels" . | nindent 4 }}
{{- if .Values.podMonitor.additionalLabels }}
{{- toYaml .Values.podMonitor.additionalLabels | nindent 4 }}
{{- end }}
spec:
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
podMetricsEndpoints:
- interval: {{ .Values.podMonitor.interval }}
scrapeTimeout: {{ .Values.podMonitor.scrapeTimeout }}
relabelings:
- sourceLabels: [__meta_kubernetes_namespace]
action: replace
targetLabel: kubernetes_namespace
- sourceLabels: [__meta_kubernetes_pod_name]
targetLabel: pod
# Because of the https://github.com/prometheus-operator/prometheus-operator/issues/5003 we cannot use the version with empty separator from monitoring chart.
#- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_cluster,__meta_kubernetes_pod_label_pg_cluster]
# targetLabel: cluster
# separator: ''
# replacement: '$1'
- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_cluster,__meta_kubernetes_pod_label_pg_cluster]
targetLabel: cluster
separator: ';'
regex: '(.*);(.*)'
action: replace
replacement: '$1$2'
- sourceLabels: [__meta_kubernetes_namespace,cluster]
targetLabel: pg_cluster
separator: ":"
replacement: '$1$2'
- sourceLabels: [__meta_kubernetes_pod_ip]
targetLabel: ip
replacement: '$1'
# Because of the https://github.com/prometheus-operator/prometheus-operator/issues/5003 we cannot use the version with empty separator from monitoring chart.
#- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_instance,__meta_kubernetes_pod_label_deployment_name]
# targetLabel: deployment
# replacement: '$1'
# separator: ''
- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_instance,__meta_kubernetes_pod_label_deployment_name]
targetLabel: deployment
separator: ';'
regex: '(.*);(.*)'
action: replace
replacement: '$1$2'
# Because of the https://github.com/prometheus-operator/prometheus-operator/issues/5003 we cannot use the version with empty separator from monitoring chart.
#- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_role,__meta_kubernetes_pod_label_role]
# targetLabel: role
# replacement: '$1'
# separator: ''
- sourceLabels: [__meta_kubernetes_pod_label_postgres_operator_crunchydata_com_role,__meta_kubernetes_pod_label_role]
targetLabel: role
separator: ';'
regex: '(.*);(.*)'
action: replace
replacement: '$1$2'
targetPort: 9187
selector:
matchLabels:
postgres-operator.crunchydata.com/cluster: {{ .Release.Name }}
{{- end -}}
14 changes: 14 additions & 0 deletions helm/postgres/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ postgresVersion: 15
# Can be overridden by "pgBackRestConfig", if set.
# backupsStorageClassName: "hostpath"

# Configuration for prometheus
podMonitor:
# Whether to create PodMonitor CR (to automatically configure Prometheus to scrape metrics from installed PG instance)
enabled: false

# The namespace where to create PodMonitor CR, defaults to the release namespace.
#namespace: "monitoring"

# Additional labels ot be applied to PodMonitor CR
additionalLabels: {}

interval: 15
scrapeTimeout: 15

############################
# User Interface, pgAdmin4 #
############################
Expand Down

0 comments on commit 28f39fd

Please sign in to comment.