kubernetes Helm, create env var with image tag value

wgeznvg7  于 2022-12-11  发布在  Kubernetes
关注(0)|答案(2)|浏览(118)

我对helm相当陌生,因为我使用其他ppl为我们的应用程序创建的图表,我正在尝试做一些我认为很容易但无法找到方法的事情。基本上,我想将应用程序版本传递给我的react应用程序。根据我找到的一些信息,以下是我的想法

image:
    tag: 0.2.6
extraEnv:
  - name: REACT_APP_APP_VERSION
    value: {image.tag}

预先感谢

a9wyjsp7

a9wyjsp71#

I assume the code you sent is your values.yaml . Then, the first part is correct.

image:
    tag: 0.2.6

Now, you don't specify variables passed to pod in values.yaml , but in your templates/* files. For example, to pass an variable to your pod, you can use the following code:

env:
  - name: REACT_APP_APP_VERSION
    value: "{{ .Values.image.tag }}"

Check this for the complete example.
Note that you cannot use values from values.yaml inside values.yaml . So the code you sent won't work. That is because, the values.yaml file itself is not evaluated.

xpcnnkqh

xpcnnkqh2#

From Documentation
The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files. Syntax: {{ tpl TEMPLATE_STRING VALUES }}
You will have something similar to

values.yaml

image:
  repository: k8s.gcr.io/busybox
  tag: "latest"

extraEnv: "{{ .Values.image.tag }}"

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: test
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
    env:
    - name: REACT_APP_APP_VERSION
      value: {{ tpl .Values.extraEnv . }}

helm template output for pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: test
    image: "k8s.gcr.io/busybox:latest"
    env:
    - name: REACT_APP_APP_VERSION
      value: latest

相关问题