<p>ı would like to set proxy as an env variable in deployment.yaml which I created via Helm v3.What should be the correct format to add env in helm template and meanwhile is there any way to pass this value in values.yaml? </p>

<pre><code>  - env:
  - name: "HTTP_PROXY"
    value: "http://&lt;user&gt;:&lt;password&gt;@&lt;ip_addr&gt;:&lt;port&gt;"




    containers:
            - name: {{ .Chart.Name }}
              securityContext:
                {{- toYaml .Values.securityContext | nindent 12 }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              ports:
                - name: http
                  containerPort: 8080
                  protocol: TCP
              resources:
                {{- toYaml .Values.resources | nindent 12 }}
</code></pre>

semural
<p>Taking into consideration that Helm's syntax has been discussed a few times on Stack, I'm going just to sum it up in order to have less orphaned questions and post it as a community wiki, because I didn't do much here. </p>

<hr>

<p>As it was discussed <a href="https://stackoverflow.com/questions/49928819/how-to-pull-environment-variables-with-helm-charts">here</a>, there are a few possible ways to add <code>env</code>'s. </p>

<p><strong>Way 1</strong>. <em>Proper way of doing that (with secret).</em> </p>

<p>As you don't want to expose the data, so it's better to have it saved as secret in kubernetes. </p>

<p>First of all, add all the needed lines in your <code>Values</code> file, so that these values can be set from outside. </p>

<p>Example:</p>

<pre><code>username: root
password: password
</code></pre>

<p>Then, add a <code>secret.yaml</code> file inside your <code>template</code> folder and copy needed data into that file. </p>

<p>Example:</p>

<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-auth
data:
  password: {{ .Values.password | b64enc }}
  username: {{ .Values.username | b64enc }}
</code></pre>

<p>Now tweak your deployment yaml template and make changes in <code>env</code> section, like this (of course you can combine envs in a way you need):</p>

<pre><code>spec:
  restartPolicy: Always
   containers:
     - name: sample-app
       image: "sample-app:latest"
       imagePullPolicy: Always
         env:          
           - name: "USERNAME"
             valueFrom:
             secretKeyRef:
               key:  username
               name: {{ .Release.Name }}-auth
           - name: "PASSWORD"
             valueFrom:
               secretKeyRef:
                 key:  password
                 name: {{ .Release.Name }}-auth
</code></pre>

<hr>

<p><strong>Way 2</strong>. <em><code>export</code> the variable and use it while running <code>helm install</code></em>.</p>

<p>Before that, you have to modify your chart so that the value can be <code>set</code> while installation. </p>

<p>If you have modified your template correctly for <code>--set</code> flag,
you can set this using environment variable.</p>

<pre><code>$ export USERNAME=root-user
</code></pre>

<p>Now use this variable while running helm install,</p>

<pre><code>$ helm install --set username=$USERNAME ./mychart
</code></pre>

<p>If you run this <code>helm install</code> in <code>dry-run</code> mode, you can verify the changes,</p>

<pre><code>$ helm install --dry-run --set username=$USERNAME --debug ./mychart
[debug] Created tunnel using local port: '44937'

[debug] SERVER: "127.0.0.1:44937"

[debug] Original chart version: ""
[debug] CHART PATH: /home/maruf/go/src/github.com/the-redback/kubernetes-yaml-drafts/helm-charts/mychart

NAME:   irreverant-meerkat
REVISION: 1
RELEASED: Fri Apr 20 03:29:11 2018
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
username: root-user

COMPUTED VALUES:
password: password
username: root-user

HOOKS:
MANIFEST:

---
# Source: mychart/templates/secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: irreverant-meerkat-auth
data:
  password: password
  username: root-user
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: irreverant-meerkat
  labels:
    app: irreverant-meerkat
spec:
  replicas: 1
  template:
    metadata:
      name: irreverant-meerkat
      labels:
        app: irreverant-meerkat
    spec:
      containers:
      - name: irreverant-meerkat
        image: alpine
        env:
        - name: "USERNAME"
          valueFrom:
            secretKeyRef:
              key:  username
              name: irreverant-meerkat-auth
        - name: "PASSWORD"
          valueFrom:
            secretKeyRef:
              key:  password
              name: irreverant-meerkat-auth

        imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: irreverant-meerkat
</code></pre>

<p>You can see that the data of username in secret has changed to <code>root-user</code>.</p>

<p><a href="https://github.com/the-redback/kubernetes-yaml-drafts/tree/master/helm-charts/mychart" rel="nofollow noreferrer">this example</a> has been added into github repo.</p>

<p>There is also some discussion in <a href="https://github.com/kubernetes/helm" rel="nofollow noreferrer">kubernetes/helm</a> repo regarding this. You can see <a href="https://github.com/kubernetes/helm/issues/944" rel="nofollow noreferrer">this issue</a> to know about all other ways to use environment variables.</p>

<hr>

<p>Way 3. <em>Hardcode values <strong>directly</strong> in Yaml (quicker but not that secure/easy to maintain)</em>.</p>

<pre><code>   spec:
      restartPolicy: Always
      containers:
            - name: {{ .Chart.Name }}
              securityContext:
                {{- toYaml .Values.securityContext | nindent 12 }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              ports:
                - name: http
                  containerPort: 8080
                  protocol: TCP
              resources:
                {{- toYaml .Values.resources | nindent 12 }}
              env:
                - name: "HTTP_PROXY"
                  value: "http://&lt;user&gt;:&lt;password&gt;@&lt;ip_addr&gt;:&lt;port&gt;"
</code></pre>
