{{- if .Values.components.rules.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ include "o11y-mixin.fullname" . }}-rules
  namespace: {{ .Values.global.namespace | default "self-monitoring" }}
  labels:
    {{- include "o11y-mixin.labels" . | nindent 4 }}
    release: self-monitoring # required by self-monitoring prometheus
spec:
  groups:
{{- if eq .Values.global.mode "global" }}
{{- range $path, $_ :=  .Files.Glob  "rules/global/**.yaml" }}
{{- $rules := $.Files.Get $path | fromYaml }}
{{- if not $rules }}
{{- fail (printf "Failed to parse YAML in file: %s" $path) }}
{{- end }}
{{- if not $rules.groups }}
{{- fail (printf "No 'groups' key found in YAML file: %s" $path) }}
{{- end }}
{{- range $rules.groups }}
  - {{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
{{- else if eq .Values.global.mode "regional" }}
{{- range $path, $_ :=  .Files.Glob  "rules/regional/**.yaml" }}
{{- $rules := $.Files.Get $path | fromYaml }}
{{- if not $rules }}
{{- fail (printf "Failed to parse YAML in file: %s" $path) }}
{{- end }}
{{- if not $rules.groups }}
{{- fail (printf "No 'groups' key found in YAML file: %s" $path) }}
{{- end }}
{{- range $rules.groups }}
  - {{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- with .Values.extraRuleGroups }}
{{- range . }}
  - {{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
