检查模板中是否存在文件/目录/使用

时间:2017-12-27 11:48:35

标签: kubernetes kubernetes-helm

鉴于以下json:

    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: {{ template "something.server.fullname" . }}
    data:
    {{ (.Files.Glob "dashboards/*.json").AsConfig | indent 2 }}
    {{ (.Files.Glob "datasources/*.json").AsConfig | indent 2 }}

如何检查文件夹是否存在且不为空?

目前,如果文件夹丢失或者没有任何文件,helm install将会中止此消息:

Error: YAML parse error on domething/charts/grafana/templates/dashboards-configmap.yaml: error converting YAML to JSON: yaml: line 6821: could not find expected ':'

1 个答案:

答案 0 :(得分:2)

您可以将Globs拉出到变量,然后将所有内容移动到if块内,例如:

{{- $globdash := .Files.Glob "dashboards/*.json" }}
{{ if $globdash }}
{{- $globdata := .Files.Glob "datasources/*.json" }}
{{ if $globdata }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "something.server.fullname" . }}
data:
{{ ($globdash).AsConfig | indent 2 }}
{{ ($globdata).AsConfig | indent 2 }}
{{ end }}
{{ end }}