<p>My current PrometheusRule at Kubernetes is implemented as follows: (note there is a hardcoded number 10000000000:</p>
<pre><code>---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  namespace: mytest
  labels:
    team: myteam
  name: myname
spec:
  groups:
  - name: mygroup
    rules:
    - alert: MemoryUsage
      annotations:
        message: MemoryUsage at ec2 {{ $labels.cluster_id }} is {{printf &quot;%.2f&quot; $value}}%.  Threshold is 80%.
      expr: 100 * memory_average{container=&quot;XXX&quot;...} / 10000000000 &gt; 80
      for: 20m
      labels:
        severity: warning
</code></pre>
<p>Question 1: How can I define a variable to replace the  hardcoded number 10 000 000 000? I.e. Where and how to define something like this in PromethusRole:
<code>max_memory=10 000 000 000</code> , and then replace the expr to something like this?</p>
<pre><code>expr: 100 * memory_average{container=&quot;XXX&quot;...} / var.max_memory &gt; 80
</code></pre>
<p>Question2: I want the value of max_memory different for different clusters. In PromethusRole, how can I define the variable with the logic as below? Thank you!</p>
<pre><code>if  {{ $labels.cluster_id }} == &quot;ClusterA&quot;:
    max_memory = 10000000000
else If  {{ $labels.cluster_id }} == &quot;ClusterB&quot;:
    max_memory = 7000000000
</code></pre>

user389955
<p>You are talking about <code>templatizing</code> your manifests. You will need a template system for this which will generate these YAMLs for you <em><strong>before</strong></em> you can apply them to your cluster.</p>
<p>There are multiple ways to achieve this, you can try <a href="https://mustache.github.io/" rel="nofollow noreferrer">mustache</a>, or <a href="https://kustomize.io/" rel="nofollow noreferrer">kustomize</a> to begin with. These will allow you to create templates in the way you are describing, and when you generate your manifests, these values will be substituted and you will have ready manifests you can apply to your cluster.</p>
<p>For more advanced applications you can look into <a href="https://jinja.palletsprojects.com/en/3.1.x/" rel="nofollow noreferrer">jinja</a> template system, which is Python based. This is also the system <code>ansible</code> uses to generate the playbook manifests.</p>
