<p>Hi I am new to Kubernetes and Helm Chart. A similar question has been asked and answered here (<a href="https://stackoverflow.com/questions/48457047/how-to-set-prometheus-rules-in-stable-prometheus-chart-values-yaml">How to set prometheus rules in stable/prometheus chart values.yaml?</a>)</p>

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

<p>In particular, this is what I have in my <code>values.yaml</code>:</p>

<pre><code>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 &gt; 0
        for: 1h
        labels:
          severity: critical
... 
&lt;2000 more lines&gt;
...
  rules: {}
  prometheus.yml:
    rule_files:
      - /etc/config/rules
      - /etc/config/alerts
</code></pre>

<p>And this is what I'd like to achieve in the new <code>values.yaml</code>:</p>

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

<p>And this is the <code>values-alerts.yaml</code> file that I'd like to include in <code>values.yaml</code>:</p>

<pre><code>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 &gt; 0
      for: 1h
      labels:
        severity: critical
... 
&lt;2000 more lines&gt;
...
</code></pre>

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

<p>Thank you,</p>

<p>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?</p>

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

<p>However, you can include <code>templates</code> in <code>templates</code> as outlined in the <a href="https://helm.sh/docs/chart_template_guide/#declaring-and-using-templates-with-define-and-template" rel="nofollow noreferrer">helm guide</a> (comments after ###):</p>

<pre><code>### 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
</code></pre>

<p>Yields:</p>

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

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

<p><strong>Don't forget the indentations, and that <code>helm template</code> is your friend!</strong></p>
