From fb830aa24e74e4ad8aca5007af2db2f247777325 Mon Sep 17 00:00:00 2001 From: borg-z Date: Wed, 17 Dec 2025 16:43:12 +0300 Subject: [PATCH] helm_lib_prometheus_rules_recursion with optional filter Signed-off-by: borg-z --- charts/helm_lib/Chart.yaml | 2 +- charts/helm_lib/README.md | 8 +++-- .../_monitoring_prometheus_rules.tpl | 31 ++++++++++++++----- ...lib_prometheus_rules_recursion-filter.yaml | 5 +++ .../monitoring/livebeef/propagated-caps.yaml | 5 +++ ...rometheus_rules_recursion_filter_test.yaml | 18 +++++++++++ ...m_lib_prometheus_rules_recursion_test.yaml | 16 ++++++++-- 7 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 tests/templates/helm_lib_prometheus_rules_recursion-filter.yaml create mode 100644 tests/testdata/monitoring/livebeef/propagated-caps.yaml create mode 100644 tests/tests/helm_lib_prometheus_rules_recursion_filter_test.yaml diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index 7b4273a..f105506 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 type: library name: deckhouse_lib_helm -version: 1.67.1 +version: 1.68.0 description: "Helm utils template definitions for Deckhouse modules." diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index 749bcc1..42914a9 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -1234,10 +1234,11 @@ list: returns all the prometheus rules from / current dir is optional — used for recursion but you can use it for partially generating rules + file list is optional - list of files to include (filters all files if provided) #### Usage -`{{ include "helm_lib_prometheus_rules_recursion" (list . [current dir]) }} ` +`{{ include "helm_lib_prometheus_rules_recursion" (list . [current dir] [file list]) }} ` #### Arguments @@ -1246,15 +1247,16 @@ list: - Namespace for creating rules - Rules root dir - Current dir (optional) +- File list for filtering (optional) ### helm_lib_prometheus_rules - returns all the prometheus rules from monitoring/prometheus-rules/ + returns all the prometheus rules from monitoring/prometheus-rules/ optionally filtered by fileList #### Usage -`{{ include "helm_lib_prometheus_rules" (list . ) }} ` +`{{ include "helm_lib_prometheus_rules" (list . [fileList]) }} ` #### Arguments diff --git a/charts/helm_lib/templates/_monitoring_prometheus_rules.tpl b/charts/helm_lib/templates/_monitoring_prometheus_rules.tpl index 149f758..cc08cf4 100644 --- a/charts/helm_lib/templates/_monitoring_prometheus_rules.tpl +++ b/charts/helm_lib/templates/_monitoring_prometheus_rules.tpl @@ -1,17 +1,29 @@ -{{- /* Usage: {{ include "helm_lib_prometheus_rules_recursion" (list . [current dir]) }} */ -}} +{{- /* Usage: {{ include "helm_lib_prometheus_rules_recursion" (list . [current dir] [file list]) }} */ -}} {{- /* returns all the prometheus rules from / */ -}} {{- /* current dir is optional — used for recursion but you can use it for partially generating rules */ -}} +{{- /* file list is optional - list of files to include (filters all files if provided) */ -}} {{- define "helm_lib_prometheus_rules_recursion" -}} {{- $context := index . 0 }} {{- /* Template context with .Values, .Chart, etc */ -}} {{- $namespace := index . 1 }} {{- /* Namespace for creating rules */ -}} {{- $rootDir := index . 2 }} {{- /* Rules root dir */ -}} {{- $currentDir := "" }} {{- /* Current dir (optional) */ -}} + {{- $fileList := list }} {{- /* File list for filtering (optional) */ -}} + {{- if gt (len .) 3 }} {{- $currentDir = index . 3 }} {{- else }} {{- $currentDir = $rootDir }} {{- end }} + {{- if gt (len .) 4 }} {{- $fileList = index . 4 }} {{- end }} + {{- $currentDirIndex := (sub ($currentDir | splitList "/" | len) 1) }} {{- $rootDirIndex := (sub ($rootDir | splitList "/" | len) 1) }} {{- $folderNamesIndex := (add1 $rootDirIndex) }} {{- range $path, $_ := $context.Files.Glob (print $currentDir "/*.{yaml,tpl}") }} + {{- /* Filter files if fileList is provided */ -}} + {{- $shouldProcess := true }} + {{- if gt (len $fileList) 0 }} + {{- $shouldProcess = has $path $fileList }} + {{- end }} + + {{- if $shouldProcess }} {{- $fileName := ($path | splitList "/" | last ) }} {{- $definition := "" }} {{- if eq ($path | splitList "." | last) "tpl" -}} @@ -24,7 +36,6 @@ {{/* Patch expression based on `d8_ignore_on_update` annotation*/}} - {{ $definition = printf "Rules:\n%s" ($definition | nindent 2) }} {{- $definitionStruct := ( $definition | fromYaml )}} {{- if $definitionStruct.Error }} @@ -78,6 +89,7 @@ spec: {{- $definition | nindent 4 }} {{- end }} {{- end }} + {{- end }} {{- end }} {{- $subDirs := list }} @@ -87,18 +99,23 @@ spec: {{- end }} {{- range $subDir := ($subDirs | uniq) }} -{{ include "helm_lib_prometheus_rules_recursion" (list $context $namespace $rootDir $subDir) }} +{{ include "helm_lib_prometheus_rules_recursion" (list $context $namespace $rootDir $subDir $fileList) }} {{- end }} {{- end }} -{{- /* Usage: {{ include "helm_lib_prometheus_rules" (list . ) }} */ -}} -{{- /* returns all the prometheus rules from monitoring/prometheus-rules/ */ -}} +{{- /* Usage: {{ include "helm_lib_prometheus_rules" (list . [fileList]) }} */ -}} +{{- /* returns all the prometheus rules from monitoring/prometheus-rules/ optionally filtered by fileList */ -}} {{- define "helm_lib_prometheus_rules" -}} {{- $context := index . 0 }} {{- /* Template context with .Values, .Chart, etc */ -}} {{- $namespace := index . 1 }} {{- /* Namespace for creating rules */ -}} + {{- $rootDir := "monitoring/prometheus-rules" }} + {{- $fileList := list }} + {{- if gt (len .) 2 }} + {{- $fileList = index . 2 }} + {{- end }} {{- if ( $context.Values.global.enabledModules | has "operator-prometheus-crd" ) }} -{{- include "helm_lib_prometheus_rules_recursion" (list $context $namespace "monitoring/prometheus-rules") }} +{{- include "helm_lib_prometheus_rules_recursion" (list $context $namespace $rootDir $rootDir $fileList) }} {{- end }} {{- end }} @@ -113,4 +130,4 @@ spec: {{- else -}} {{ $timeout }}s {{- end }} -{{- end }} +{{- end }} \ No newline at end of file diff --git a/tests/templates/helm_lib_prometheus_rules_recursion-filter.yaml b/tests/templates/helm_lib_prometheus_rules_recursion-filter.yaml new file mode 100644 index 0000000..4e52a0e --- /dev/null +++ b/tests/templates/helm_lib_prometheus_rules_recursion-filter.yaml @@ -0,0 +1,5 @@ +{{- $prometheusRules := list + "testdata/monitoring/livebeef/propagated-caps.yaml" +}} + +{{- include "helm_lib_prometheus_rules_recursion" (list . "d8-observability" "testdata/monitoring" "testdata/monitoring" $prometheusRules) }} diff --git a/tests/testdata/monitoring/livebeef/propagated-caps.yaml b/tests/testdata/monitoring/livebeef/propagated-caps.yaml new file mode 100644 index 0000000..edf5dc5 --- /dev/null +++ b/tests/testdata/monitoring/livebeef/propagated-caps.yaml @@ -0,0 +1,5 @@ +- name: kubernetes.caps.group-one + rules: + - alert: CapsInstanceUnavailable + expr: vector(1) + diff --git a/tests/tests/helm_lib_prometheus_rules_recursion_filter_test.yaml b/tests/tests/helm_lib_prometheus_rules_recursion_filter_test.yaml new file mode 100644 index 0000000..f35e9bd --- /dev/null +++ b/tests/tests/helm_lib_prometheus_rules_recursion_filter_test.yaml @@ -0,0 +1,18 @@ +suite: test helm_lib_prometheus_rules_recursion with filter +templates: + - helm_lib_prometheus_rules_recursion-filter.yaml +tests: + - it: should render only specified PrometheusRule + set: + global.enabledModules: ["observability"] + global.discovery.prometheusScrapeInterval: 30s + global.discovery.apiVersions: [] + asserts: + - equal: + path: kind + value: PrometheusRule + - equal: + path: metadata.name + value: test-module-livebeef-propagated-caps + - hasDocuments: + count: 1 diff --git a/tests/tests/helm_lib_prometheus_rules_recursion_test.yaml b/tests/tests/helm_lib_prometheus_rules_recursion_test.yaml index a6c7c3c..47931b3 100644 --- a/tests/tests/helm_lib_prometheus_rules_recursion_test.yaml +++ b/tests/tests/helm_lib_prometheus_rules_recursion_test.yaml @@ -11,11 +11,17 @@ tests: - equal: path: kind value: PrometheusRule + documentIndex: 0 - equal: path: metadata.name - value: test-module-deadbeef-propagated-ingress-nginx + value: test-module-deadbeef-propagated-ingress-nginx + documentIndex: 0 + - equal: + path: metadata.name + value: test-module-livebeef-propagated-caps + documentIndex: 1 - hasDocuments: - count: 1 + count: 2 - it: should generate ClusterObservabilityPropagatedMetricsRulesGroup set: global.enabledModules: ["observability"] @@ -33,5 +39,9 @@ tests: path: metadata.name value: test-module-deadbeef-ingress-nginx-1 documentIndex: 1 + - equal: + path: metadata.name + value: test-module-livebeef-caps-0 + documentIndex: 2 - hasDocuments: - count: 2 + count: 3