为什么 helm 模板函数不解析 $labels var?
Why helm template function is not resolveing $labels var?
我正在定义一个 PrometheusRule 如下:
prometheusRule:
rules:
- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 10
for: 0m
labels:
severity: warning
annotations:
summary: Blackbox SSL certificate will expire soon (instance {{ $labels.instance }})
description: "SSL certificate expires in 30 days\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
以及 helm chart 中的模板 yml:
{{- if .Values.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: {{ template "prometheus-blackbox-exporter.fullname" . }}
{{- with .Values.prometheusRule.namespace }}
namespace: {{ . }}
{{- end }}
labels:
{{- include "prometheus-blackbox-exporter.labels" . | nindent 4 }}
{{- with .Values.prometheusRule.additionalLabels -}}
{{- toYaml . | nindent 4 -}}
{{- end }}
spec:
{{- with .Values.prometheusRule.rules }}
groups:
- name: {{ template "prometheus-blackbox-exporter.name" $ }}
rules: {{ tpl (toYaml .) $ | nindent 8 }}
{{- end }}
{{- end }}
当我 运行 helm template
、tpl
func 时,它没有解析 $labels 和 $values 变量。当我删除注释时,helm 模板不再抱怨。哪里失败了?
错误:
Error: template: prometheus-blackbox-exporter/templates/prometheusrule.yaml:18:16: executing "prometheus-blackbox-exporter/templates/prometheusrule.yaml" at <tpl (toYaml .) $>: error calling tpl: error during tpl function execution for "- alert: SSLCertExpiringSoon\n annotations:\n summary: Blackbox SSL certificate will expire soon (instance {{ $labels.instance\n }})\n expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 10\n for: 0m\n labels:\n release: prometheus\n severity: warning\n- alert: SSLCertExpiringSoon\n annotations: null\n expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 3\n for: 0m\n labels:\n severity: critical": parse error at (prometheus-blackbox-exporter/templates/prometheusrule.yaml:3): undefined variable "$labels"
Prometheus's alerting rules also use {{ ... $variable ... }}
syntax, similar to Helm but with a different variant on the Go text/template
syntax。当您通过 tpl
传递此文件时,Helm 会尝试评估嵌入的 {{ ... }}
模板并评估那里的任何块。由于 $labels
和 $value
不是在 Helm 级别定义的局部变量,因此您会收到此错误。
如果您只想让 Prometheus 按原样查看此文件,并且不需要在 Helm 级别替换任何内容(该文件不包含对 .Values
的引用),那么您不需要不需要 tpl
rules: {{ toYaml . | nindent 8 }}
如果您确实需要 tpl
,那么在包含的文件中,您需要使 {{
作为字符串发出而不是作为模板处理。一种语法方法是创建一个打印出 {{
:
的模板块
description: "VALUE = {{ "{{" }} $value }}"
# ^^^^^^^^^^ a {{ ... }} block that prints "{{"
语法的工作版本如下:
{{ `{{` }} $value }}
我正在定义一个 PrometheusRule 如下:
prometheusRule:
rules:
- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 10
for: 0m
labels:
severity: warning
annotations:
summary: Blackbox SSL certificate will expire soon (instance {{ $labels.instance }})
description: "SSL certificate expires in 30 days\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
以及 helm chart 中的模板 yml:
{{- if .Values.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: {{ template "prometheus-blackbox-exporter.fullname" . }}
{{- with .Values.prometheusRule.namespace }}
namespace: {{ . }}
{{- end }}
labels:
{{- include "prometheus-blackbox-exporter.labels" . | nindent 4 }}
{{- with .Values.prometheusRule.additionalLabels -}}
{{- toYaml . | nindent 4 -}}
{{- end }}
spec:
{{- with .Values.prometheusRule.rules }}
groups:
- name: {{ template "prometheus-blackbox-exporter.name" $ }}
rules: {{ tpl (toYaml .) $ | nindent 8 }}
{{- end }}
{{- end }}
当我 运行 helm template
、tpl
func 时,它没有解析 $labels 和 $values 变量。当我删除注释时,helm 模板不再抱怨。哪里失败了?
错误:
Error: template: prometheus-blackbox-exporter/templates/prometheusrule.yaml:18:16: executing "prometheus-blackbox-exporter/templates/prometheusrule.yaml" at <tpl (toYaml .) $>: error calling tpl: error during tpl function execution for "- alert: SSLCertExpiringSoon\n annotations:\n summary: Blackbox SSL certificate will expire soon (instance {{ $labels.instance\n }})\n expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 10\n for: 0m\n labels:\n release: prometheus\n severity: warning\n- alert: SSLCertExpiringSoon\n annotations: null\n expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 3\n for: 0m\n labels:\n severity: critical": parse error at (prometheus-blackbox-exporter/templates/prometheusrule.yaml:3): undefined variable "$labels"
Prometheus's alerting rules also use {{ ... $variable ... }}
syntax, similar to Helm but with a different variant on the Go text/template
syntax。当您通过 tpl
传递此文件时,Helm 会尝试评估嵌入的 {{ ... }}
模板并评估那里的任何块。由于 $labels
和 $value
不是在 Helm 级别定义的局部变量,因此您会收到此错误。
如果您只想让 Prometheus 按原样查看此文件,并且不需要在 Helm 级别替换任何内容(该文件不包含对 .Values
的引用),那么您不需要不需要 tpl
rules: {{ toYaml . | nindent 8 }}
如果您确实需要 tpl
,那么在包含的文件中,您需要使 {{
作为字符串发出而不是作为模板处理。一种语法方法是创建一个打印出 {{
:
description: "VALUE = {{ "{{" }} $value }}"
# ^^^^^^^^^^ a {{ ... }} block that prints "{{"
语法的工作版本如下:
{{ `{{` }} $value }}