Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/helm_lib/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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."
8 changes: 5 additions & 3 deletions charts/helm_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1234,10 +1234,11 @@ list:

returns all the prometheus rules from <root dir>/
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 . <namespace> <root dir> [current dir]) }} `
`{{ include "helm_lib_prometheus_rules_recursion" (list . <namespace> <root dir> [current dir] [file list]) }} `

#### Arguments

Expand All @@ -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 . <namespace>) }} `
`{{ include "helm_lib_prometheus_rules" (list . <namespace> [fileList]) }} `

#### Arguments

Expand Down
31 changes: 24 additions & 7 deletions charts/helm_lib/templates/_monitoring_prometheus_rules.tpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
{{- /* Usage: {{ include "helm_lib_prometheus_rules_recursion" (list . <namespace> <root dir> [current dir]) }} */ -}}
{{- /* Usage: {{ include "helm_lib_prometheus_rules_recursion" (list . <namespace> <root dir> [current dir] [file list]) }} */ -}}
{{- /* returns all the prometheus rules from <root dir>/ */ -}}
{{- /* 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" -}}
Expand All @@ -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 }}
Expand Down Expand Up @@ -78,6 +89,7 @@ spec:
{{- $definition | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

{{- $subDirs := list }}
Expand All @@ -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 . <namespace>) }} */ -}}
{{- /* returns all the prometheus rules from monitoring/prometheus-rules/ */ -}}
{{- /* Usage: {{ include "helm_lib_prometheus_rules" (list . <namespace> [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 }}

Expand All @@ -113,4 +130,4 @@ spec:
{{- else -}}
{{ $timeout }}s
{{- end }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -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) }}
5 changes: 5 additions & 0 deletions tests/testdata/monitoring/livebeef/propagated-caps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: kubernetes.caps.group-one
rules:
- alert: CapsInstanceUnavailable
expr: vector(1)

18 changes: 18 additions & 0 deletions tests/tests/helm_lib_prometheus_rules_recursion_filter_test.yaml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 13 additions & 3 deletions tests/tests/helm_lib_prometheus_rules_recursion_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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