【问题标题】:How to pass helm values dynamically如何动态传递 helm 值
【发布时间】:2021-11-25 07:30:16
【问题描述】:

我正在尝试通过另一个变量动态访问 helm 值,因为我正在利用范围功能创建多个部署。以我的部署文件的这一部分为例

{{- range $teams := .Values.teams }}
.
.
.
        ports:
        - containerPort: {{ .Values.deployment.backend.($teams.tag).serverPort }}
          protocol: {{ .Values.deployment.backend.($teams.tag).serverProtocol }}
        - containerPort: {{ .Values.deployment.backend.($teams.tag).authPort }}
          protocol: {{ .Values.deployment.backend.($teams.tag).authProtocol }}

.
.
.
---
{{- end }}

带有一个 values.yml 文件

teams:
  - name: TeamA
    tag: teamA
  - name: TeamB
    tag: teamB
  - name: TeamC
    tag: teamC
deployment:
  backend:
    teamA:
      serverPort: 10001
      serverProtocol: TCP
      authPort: 10010
      authProtocol: TCP
    teamB:
      serverPort: 9101
      serverProtocol: TCP
      authPort: 9110
      authProtocol: TCP
    teamC:
      serverPort: 9001
      serverProtocol: TCP
      authPort: 9010
      authProtocol: TCP


例如,我无法弄清楚如何传递要评估的 $teams.tag 以返回 containerPort 的整体值。

感谢任何帮助。

干杯

【问题讨论】:

    标签: kubernetes kubernetes-helm helm3


    【解决方案1】:

    Helm 本身提供了许多函数来操作值。

    这是使用get 函数处理您的用例的一种方法。

    {{- $ := . -}}
    {{- range $teams := .Values.teams }}
    .
    .
    .
            ports:
            - containerPort: {{ (get $.Values.deployment.backend $teams.tag).serverPort }}
              protocol: {{ (get $.Values.deployment.backend $teams.tag).serverProtocol }}
            - containerPort: {{ (get $.Values.deployment.backend $teams.tag).authPort }}
              protocol: {{ (get $.Values.deployment.backend $teams.tag).authProtocol }}
    
    .
    .
    .
    ---
    {{- end }}
    

    请注意range 运算符内的范围会发生变化。因此,您需要将. 预分配给$ 才能访问根范围。

    您也可以参考此文档https://helm.sh/docs/chart_template_guide/function_list/,了解有关您可以使用的功能的更多信息。

    【讨论】:

    • 你好,先生!非常感谢
    【解决方案2】:

    最终使用了 tpl https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function

    如果有更好的方法,欢迎提出建议

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2023-01-24
    • 2021-11-05
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多