Search code examples
kuberneteskubernetes-helm

How to set environment variables in helm?


I have the following deployment definition:

...
containers:
        - name: {{ .Release.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{ if .Values.env.enabled }}
          env:
          {{- range .Values.env.vars }}
             ?????What comes here?????
          {{- end }}
          {{ end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
...

in the values.yaml, I have defined:

env:
  enabled: false
  vars: [] 

What I would like to do is, to set environment dynamically via --set, for instance:

helm template user-svc \
  --set image.tag=0.1.0 \
  --set image.repository=user-svc \
  --set env.enabled=true \
  --set env.vars.POSTGRES_URL="jdbc:postgresql://localhost:5432/users" \
  --set env.vars.POSTGRES_USER="dbuser" \
  ./svc

after rendering, it should show:

...
containers:
- name: demo
  image: game.example/demo-game
  env:
    - name: POSTGRES_URL
      value: jdbc:postgresql://localhost:5432/users
...

and how to set the following option via --set:

- name: UI_PROPERTIES_FILE_NAME
  valueFrom:
    configMapKeyRef:
      name: game-demo
      key: ui_properties_file_name 

Solution

  • You can access the --set option using .Values.

    {{- if eq .Values.env.enabled "true" -}}
     env:
       - name: {{ .Values.env.vars.POSTGRES_USER }}
         value: {{ .Values.env.vars.env.vars.POSTGRES_URL}}
    {{- end }}
    

    Try the above.