Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if files/dirs/ used in templates exists

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 ':'
like image 365
cristi Avatar asked Dec 27 '17 11:12

cristi


1 Answers

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 }}
like image 92
eversMcc Avatar answered Oct 03 '22 04:10

eversMcc