helm upgrade 失败,显示“函数“X”未定义”。

8

我正在尝试升级一个helm图表,

我遇到了错误:“函数” pod“未定义”,这很有道理,因为我确实没有这样的函数。

“pod”来自一个json文件,我将其转换为configmap,并且helm将此值读取为函数而不是json文件中的字符串。

这是我的configmap的片段:

# Generated from 'pods' from https://raw.githubusercontent.com/coreos/prometheus-operator/master/contrib/kube-prometheus/manifests/grafana-dashboardDefinitions.yaml
# Do not change in-place! In order to change this file first read following link:
# https://github.com/helm/charts/tree/master/stable/prometheus-operator/hack
{{- if and .Values.grafana.enabled .Values.grafana.defaultDashboardsEnabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-%s" (include "prometheus-operator.fullname" $) "services-health" | trunc 63 | trimSuffix "-" }}
  labels:
    {{- if $.Values.grafana.sidecar.dashboards.label }}
    {{ $.Values.grafana.sidecar.dashboards.label }}: "1"
    {{- end }}
    app: {{ template "prometheus-operator.name" $ }}-grafana
{{ include "prometheus-operator.labels" $ | indent 4 }}
data:
  services-health.json: |-
    {
      "annotations": {
        "list": [
          {
            "builtIn": 1,
            "datasource": "-- Grafana --",
            "enable": true,
            "hide": true,
            "iconColor": "rgba(0, 211, 255, 1)",
            "name": "Annotations & Alerts",
            "type": "dashboard"
          }
        ]
      },
      "targets": [
        {
          "expr": "{__name__=~\"kube_pod_container_status_ready\", container=\"aggregation\",kubernetes_namespace=\"default\",chart=\"\"}",
          "format": "time_series",
          "instant": false,
          "intervalFactor": 2,
          "legendFormat": "{{pod}}",
          "refId": "A"
        }
}
{{- end }}

我遇到的错误来自这行代码:“legendFormat”:“{{pod}}”, 我收到的错误信息如下:

helm upgrade --dry-run prometheus-operator-chart /home/ubuntu/infra-devops/helm/vector-chart/prometheus-operator-chart/ 错误:升级失败:“prometheus-operator/templates/grafana/dashboards/services-health.yaml”中存在解析错误: 模板: prometheus-operator/templates/grafana/dashboards/services-health.yaml:1213: 函数“pod”未定义

我尝试了转义字符,但是没有效果。有人有什么想法可以解决这个问题吗?

1
你尝试过使用“helm lint”和“helm template”来查看生成的资源是否符合预期吗? - Yaniv Oliver
我得到了相同的结果:[ERROR] templates /:在“prometheus-operator/templates/grafana/dashboards/services-health.yaml”中解析错误:模板:prometheus-operator/templates/grafana/dashboards/services-health.yaml:176:未定义函数“pod”。 - Shahar Hamuzim Rajuan
4个回答

15

使用反引号可以转义gotpl占位符(backticks)。例如,在您的场景中,您可以写成{{` {{ pod }} `}} 而不是{{ pod }}


这不是一个 pod 变量,它只是我 JSON 文件中被称为 "{{pod}}" 的字符串。 - Shahar Hamuzim Rajuan
2
那么传递“--set pod=pod”的合理性是什么? - Yaniv Oliver

4

将您的仪表板json移动到一个单独的文件中,比如命名为dashboard.json。 然后在您的configmap文件中:不要将json内联列出,而是通过键入以下内容引用dashboard.json文件:

data:
  services-health.json: |-
{{ .Files.Get "dashboard.json" | indent 4 }}

那会解决问题!


我喜欢这个解决方案。它使得我们可以拥有纯净的JSON文件,与yaml中的json分开保存。 - Bartosz Bilicki

2
在我的实验中,我将 "legendFormat": "{{ pod }}", 替换为 "legendFormat": "{{ "{{ pod }}" }}", 这样做可以很好地返回所需的语法(特别是适用于grafana-operator GrafanaDashboard CRD)。请注意保留HTML标签。

1
将json文件放在config map之外并在config map中进行调用是可行的,但是在使用helm时要确保将json文件放在模板目录之外,否则它会尝试搜索{{ pod }}。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接