如何使用文件内的 helm 模板中定义的变量在 configmap 中使用

问题描述 投票:0回答:1

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

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
1个回答
2
投票

$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
© www.soinside.com 2019 - 2024. All rights reserved.