我定义一个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 }}"以及头盔图中的模板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功能不是解析$labels和$values vars。当我删除注释时,舵机模板就不再抱怨了。哪里失败了?
错误:
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"发布于 2021-09-23 11:12:10
普罗米修斯的报警规则也使用{{ ... $variable ... }}语法,类似于Helm,但与语法相似。当您通过tpl传递此文件时,Helm将尝试计算嵌入的{{ ... }}模板并计算其中的任何块。因为$labels和$value不是在Helm级别上定义的局部变量,所以您将得到这个错误。
如果您只想让Prometheus看到这个文件的原样,并且不需要在Helm级别替换任何东西(该文件不包括对.Values的引用),那么您就不需要tpl了。
rules: {{ toYaml . | nindent 8 }}如果您确实需要tpl,那么在包含的文件中,需要将{{作为字符串发出,而不是作为模板处理。其中一种语法方法是创建一个模板块,打印出{{。
description: "VALUE = {{ "{{" }} $value }}"
# ^^^^^^^^^^ a {{ ... }} block that prints "{{"发布于 2022-02-10 21:15:35
语法的工作版本如下:
{{ `{{` }} $value }} https://stackoverflow.com/questions/69297354
复制相似问题