【问题标题】:how to use variables defined in helm templates inside files to be used in configmap如何使用 helm 模板中定义的变量在 configmap 中使用的文件中
【发布时间】:2021-12-03 11:19:09
【问题描述】:

Helm 新手在这里。我正在尝试遍历文件并使用其内容创建一个配置映射。文件内容需要在循环内定义变量。下面是我正在使用的配置图。

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "proj.name" . }}
  labels:
    app.kubernetes.io/name: {{ include "proj.name" . }}
    app.kubernetes.io/version: {{ include "proj.version" . }}
data:
  {{- range $path, $_ := .Files.Glob "myconfig/*.txt" -}}
  {{ $path | base | nindent 2 }}: |
  {{- tpl ($.Files.Get $path) $ | nindent 4 }}
  {{- end -}}

myconfig/name.txt 的内容

i am able to access this -> {{ .Values.somekey }}
But not this -> {{ $path }}

我收到错误:未定义变量“$path” 任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: kubernetes kubernetes-helm helm3


    【解决方案1】:

    $path 变量是一个局部变量。它不能从其他模板函数或tpl 扩展中访问。特殊的“根上下文”变量$ 也不包含局部变量。

    在这种情况下,您可以为tpl 函数定义自己的上下文:

    {{- $context := dict "somekey" .Values.somekey "path" $path }}
    {{- tpl ($.Files.Get $path) $context | nindent 4 }}
    

    然后在文件中,您将引用 dict 中提供的键(但不是该显式上下文之外的其他内容)

    i am able to access this -> {{ .somekey }}
    and also this -> {{ .path }}
    but i wouldn't be able to reference .Values.anything
    

    【讨论】:

    • 我在尝试这个时收到错误“BasePath is not a value”。我认为这个错误可能是由于 tpl 在范围内的放置。到目前为止,我还没有让它工作。但也许我越来越近了。谢谢!
    猜你喜欢
    • 2022-08-03
    • 2021-09-02
    • 1970-01-01
    • 2019-03-26
    • 2019-06-25
    • 1970-01-01
    • 2019-04-25
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多