kubernetes helm chart使用envFrom获取secret和configmap值

ewm0tg9j  于 2023-05-06  发布在  Kubernetes
关注(0)|答案(1)|浏览(323)

我正试图注入环境变量在我的 Helm 图表部署文件。我的值文件看起来像这样。
values.yaml

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

我想遍历secrets和configmaps值。这就是我在deployment.yaml文件中所做的

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

但是我没有得到想要的结果

8zzbczxx

8zzbczxx1#

您可以直接使用定义的值,如:

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

或者,您可以使用with代替use range。
下面是一个例子:
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.
     {{- with .Values.envFrom }}
      envFrom:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  restartPolicy: Never

输出为:

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

相关问题