我有一個 configMap,它有一條如下所示的警報:
alert_output:
outputs:
- type: webhook
enabled: true
template: |
{{- if ne (print .PolicyType) "Heartbeat" -}}
{"text": "New Alert\n Type: {{.PolicyType}}\n Details: {{.Description}}"
{{- end -}}
如何在 Helm 中對其進行模板化,以便可以通過像這樣text的值檔案自定義內部?outputs.template
alert_output:
outputs:
- type: webhook
enabled: true
template: |
{{- if ne (print .PolicyType) "Heartbeat" -}}
{"text": {{ .Values.custom_alert_text }}
{{- end -}}
valus.yaml
custom_alert_test: "New Alert\n Type: {{.PolicyType}}\n Details: {{.Description}}"
當然,原始{{}}版本被解釋為 Helm 模板,而不是應用程式模板語言。
uj5u.com熱心網友回復:
你需要做兩件事來完成這項作業。
在模板本身中,您需要使 Helm(或更準確地說是 Gotext/template引擎)輸出 a{{而不是將其解釋為模板標記。我發現最簡單的方法是撰寫一個發出花括號的模板塊:
{{/* A template that evaluates to the literal string "{{" */}}
{{"{{"}}- if ne ... -}}
花括號不會在values.yaml檔案中解釋,因此您無需在此處進行更改。但是,決議 YAML 檔案會將\n轉義序列轉換為換行符并使用可能存在的任何參考。Helm 包含一個toJson函式,該函式會將任何內容呈現為有效的 JSON,并且應該為您執行所需的參考和轉義。
在您的示例中,還需要在與JSON 物件語法}匹配的行末尾再添加一個。{
把這些東西放在一起,你應該得到類似的東西:
alert_output:
outputs:
- type: webhook
enabled: true
template: |
{{"{{"}}- if ne (print .PolicyType) "Heartbeat" -}}
{"text": {{ toJson .Values.custom_alert_text }}}
{{"{{"}}- end -}}
您還可以用模板語言組裝整個物件,然后將其序列化;這看起來像
template: |
{{"{{"}}- if ne (print .PolicyType) "Heartbeat" -}}
{{ dict "text" .Values.custom_alert_text | toJson }}
{{"{{"}}- end -}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433969.html
標籤:模板 kubernetes-helm 去模板
