How to include prometheus rules from another yaml file in stable/prometheus chart values.yaml?

7/8/2019

Hi I am new to Kubernetes and Helm Chart. A similar question has been asked and answered here (How to set prometheus rules in stable/prometheus chart values.yaml?)

But I am looking for a way to have the rules defined in another file, and then include the file in the values.yaml, for easier maintenance (since I have more than 2000+ lines of alerts...)

In particular, this is what I have in my values.yaml:

serverFiles:
  alerts:
    groups:
    - name: kubernetes-apps
      rules:
      - alert: KubePodCrashLooping
        annotations:
          message: Pod {{ $labels.namespace }}/{{ $labels.pod }} ({{ $labels.container
            }}) is restarting {{ printf "%.2f" $value }} times / 5 minutes.
          runbook_url: https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubepodcrashlooping
        expr: rate(kube_pod_container_status_restarts_total{component="kube-state-metrics"}[15m])
          * 60 * 5 > 0
        for: 1h
        labels:
          severity: critical
... 
<2000 more lines>
...
  rules: {}
  prometheus.yml:
    rule_files:
      - /etc/config/rules
      - /etc/config/alerts

And this is what I'd like to achieve in the new values.yaml:

serverFiles:
  alerts: {{ include from values-alerts.yaml }}
  rules: {}
  prometheus.yml:
    rule_files:
      - /etc/config/rules
      - /etc/config/alerts

And this is the values-alerts.yaml file that I'd like to include in values.yaml:

alerts:
  groups:
  - name: kubernetes-apps
    rules:
    - alert: KubePodCrashLooping
      annotations:
        message: Pod {{ $labels.namespace }}/{{ $labels.pod }} ({{ $labels.container
          }}) is restarting {{ printf "%.2f" $value }} times / 5 minutes.
        runbook_url: https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubepodcrashlooping
      expr: rate(kube_pod_container_status_restarts_total{component="kube-state-metrics"}[15m])
        * 60 * 5 > 0
      for: 1h
      labels:
        severity: critical
... 
<2000 more lines>
...

Please advise if this is doable, or if there is other better approach to do so.

Thank you,

-- Maxim Mai
kubernetes
kubernetes-helm
prometheus

1 Answer

7/8/2019

My understanding is that you want to duplicate the code outlined in your last code snippet and you don't want to add default values to the snippet itself, am I right?

If that's the case, then you shouldn't use the values.yaml. The values.yaml file should contain default values for things like key-value-pairs for your templates.

However, you can include templates in templates as outlined in the helm guide (comments after ###):

### Define a template (this can be a seperate file)
{{- define "mychart.labels" }}  
  labels:
    generator: helm
    date: {{ now | htmlDate }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  {{- template "mychart.labels" }} ### Include the template

Yields:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: running-panda-configmap
  labels:
    generator: helm
    date: 2016-11-02

So instead of including the values-alert in values.yaml you can include the file in the templates/ you need and {{ template }} out the labels.

Don't forget the indentations, and that helm template is your friend!

-- Todai
Source: StackOverflow