From 2e7959f07852405060bae846fb885c5bafe3bed3 Mon Sep 17 00:00:00 2001 From: Denis Policastro Date: Thu, 23 Jan 2025 12:17:11 -0300 Subject: [PATCH] feat: add prometheus-rules Chart --- charts/prometheus-rules/.helmignore | 23 +++++++ charts/prometheus-rules/Chart.yaml | 7 ++ charts/prometheus-rules/README.md | 20 ++++++ .../prometheus-rules/templates/_helpers.tpl | 66 +++++++++++++++++++ .../templates/prometheus-rules.yaml | 33 ++++++++++ charts/prometheus-rules/values.yaml | 11 ++++ 6 files changed, 160 insertions(+) create mode 100644 charts/prometheus-rules/.helmignore create mode 100644 charts/prometheus-rules/Chart.yaml create mode 100644 charts/prometheus-rules/README.md create mode 100644 charts/prometheus-rules/templates/_helpers.tpl create mode 100644 charts/prometheus-rules/templates/prometheus-rules.yaml create mode 100644 charts/prometheus-rules/values.yaml diff --git a/charts/prometheus-rules/.helmignore b/charts/prometheus-rules/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/charts/prometheus-rules/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/prometheus-rules/Chart.yaml b/charts/prometheus-rules/Chart.yaml new file mode 100644 index 00000000..0bafd19f --- /dev/null +++ b/charts/prometheus-rules/Chart.yaml @@ -0,0 +1,7 @@ +apiVersion: v2 +name: prometheus-rules +description: A Helm chart for Prometheus Operator PrometheusRule custom resources +type: application +version: 0.0.1 +maintainers: + - name: 0xDones diff --git a/charts/prometheus-rules/README.md b/charts/prometheus-rules/README.md new file mode 100644 index 00000000..6933840f --- /dev/null +++ b/charts/prometheus-rules/README.md @@ -0,0 +1,20 @@ + +# prometheus-rules + +![Version: 0.0.1](https://img.shields.io/badge/Version-0.0.1-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) + +A Helm chart for Prometheus Operator PrometheusRule custom resources + +## Values + +| Key | Type | Default | Description | +|-----|------|---------|-------------| +| defaultAlertLabels | object | `{}` | | +| spec.groups[0].name | string | `"./example.rules"` | | +| spec.groups[0].rules[0].alert | string | `"ExampleAlert"` | | +| spec.groups[0].rules[0].expr | string | `"Vector(1)"` | | +| spec.groups[0].rules[0].for | string | `"5m"` | | +| spec.groups[0].rules[0].labels.severity | string | `"warning"` | | + +---------------------------------------------- +Autogenerated from chart metadata using [helm-docs v1.14.2](https://github.com/norwoodj/helm-docs/releases/v1.14.2) diff --git a/charts/prometheus-rules/templates/_helpers.tpl b/charts/prometheus-rules/templates/_helpers.tpl new file mode 100644 index 00000000..44c404c6 --- /dev/null +++ b/charts/prometheus-rules/templates/_helpers.tpl @@ -0,0 +1,66 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "prometheus-rules.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "prometheus-rules.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "prometheus-rules.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "prometheus-rules.labels" -}} +helm.sh/chart: {{ include "prometheus-rules.chart" . }} +{{ include "prometheus-rules.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "prometheus-rules.selectorLabels" -}} +app.kubernetes.io/name: {{ include "prometheus-rules.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "prometheus-rules.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "prometheus-rules.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} + +{{- define "prometheus-rules.defaultAlertLabels" -}} +{{ .Values.defaultAlertLabels | toYaml }} +{{- end }} diff --git a/charts/prometheus-rules/templates/prometheus-rules.yaml b/charts/prometheus-rules/templates/prometheus-rules.yaml new file mode 100644 index 00000000..08e1e1a9 --- /dev/null +++ b/charts/prometheus-rules/templates/prometheus-rules.yaml @@ -0,0 +1,33 @@ +--- +apiVersion: monitoring.coreos.com/v1 +kind: PrometheusRule +metadata: + name: {{ include "prometheus-rules.fullname" . }} + labels: + {{- include "prometheus-rules.labels" . | nindent 4 }} +spec: + groups: + {{- range .Values.spec.groups }} + - name: {{ .name }} + rules: + {{- range .rules }} + - expr: {{ .expr }} + {{- with .for }} + for: {{ . }} + {{- end }} + {{- with .alert }} + alert: {{ . }} + {{- end }} + {{- with .record }} + record: {{ . }} + {{- end }} + annotations: {{- .annotations | toYaml | nindent 8 }} + labels: + {{- $defaultLabels := $.Values.defaultAlertLabels | default (dict) }} + {{- $customLabels := .labels | default (dict) }} + {{- $labels := merge $defaultLabels $customLabels }} + {{- with $labels }} + {{- toYaml . | nindent 8 }} + {{- end }} + {{- end }} + {{- end }} diff --git a/charts/prometheus-rules/values.yaml b/charts/prometheus-rules/values.yaml new file mode 100644 index 00000000..3535846e --- /dev/null +++ b/charts/prometheus-rules/values.yaml @@ -0,0 +1,11 @@ +defaultAlertLabels: {} + +spec: + groups: + - name: ./example.rules + rules: + - alert: ExampleAlert + expr: Vector(1) + for: 5m + labels: + severity: warning