简体   繁体   中英

helm chart getting secrets and configmap values using envFrom

I m trying to inject env vars in my helm chart deployment file. my values file looks like this.

values.yaml

envFrom:
  - configMapRef:
      name: my-config
  - secretRef: 
      name: my-secret

I want to iterate through secrets and configmaps values. This is what I did in deployment.yaml file

  envFrom:
       {{- range  $item := .Values.envFrom }}
      
          {{- $item | toYaml | nindent 14 }}
       {{- end }}

But i didn t get the desired result

You can directly use the defined value like:

...
      envFrom:
      {{- toYaml .Values.envFrom | nindent 6 }}
...

Or Instead of use range, you can use with .
Here is an example:

values.yaml :

envFrom:
  - configMapRef:
      name: my-config
  - secretRef: 
      name: my-secret

pod.yaml :

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
  namespace: test
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      # {{- with .Values.envFrom }} can be here if you dont
      # want to define envFrom in this container if envFrom
      # is not defined in values.yaml.
      # If you want to do that, remove the one below.
      envFrom:
      {{- with .Values.envFrom }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
  restartPolicy: Never

The output is:

c[_] > helm template test .
---
# Source: test/templates/test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
  namespace: test
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
        - configMapRef:
            name: my-config
        - secretRef:
            name: my-secret
  restartPolicy: Never

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM