Skip to content

Instantly share code, notes, and snippets.

@jayme-github
Last active February 27, 2019 13:11
Show Gist options
  • Save jayme-github/c5fc52c98156220bf0f9faf9f28274e5 to your computer and use it in GitHub Desktop.
Save jayme-github/c5fc52c98156220bf0f9faf9f28274e5 to your computer and use it in GitHub Desktop.
Little hack to have a generic map of environment variables in a Helm chart but still require some of them to be set during install/update

values.yaml

## One may define arbitrary environment variables here
env:
  VARIABLE_ONE: "Foo"
  VARIABLE_TWO: ""

## List of all environment variables that are required by this helm chart
requiredEnvs:
  - VARIABLE_ONE
  - VARIABLE_TWO

templates/configmap.yaml

{{- if or .Values.env .Values.requiredEnvs -}}
apiVersion: v1
kind: ConfigMap 
metadata: 
  name: {{ include "mychart.fullname" . }} 
  labels:
    app.kubernetes.io/name: {{ include "mychart.name" . }} 
    helm.sh/chart: {{ include "mychart.chart" . }} 
    app.kubernetes.io/instance: {{ .Release.Name }} 
    app.kubernetes.io/managed-by: {{ .Release.Service }} 
data:
{{- /* Test if all required environment variables are set */ -}}
{{- range $idx, $key := .Values.requiredEnvs -}}
  {{- $_ := required (printf "A valid .Values.env.%s entry is required!" $key) (index $.Values.env $key) -}}
{{- end - }}
{{/* Include all environment variabled in this ConfigMap */}}
{{- range $key, $val := .Values.env }}
  {{ $key | upper }}: {{ $val | quote }}
{{- end }}
{{- end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment