Check if files/dirs/ used in templates exists

12/27/2017

Given the following 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 }}

How can I check if the folder exists and is not empty?

Currently, if the folder is missing or doesn't have any files, helm install will abort with this message:

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

1 Answer

9/24/2018

You can pull your Globs out to variables, and then move everything within if blocks, e.g.:

{{- $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 }}
-- eversMcc
Source: StackOverflow