$ 无法从 helm 模板中解析为根上下文

Rod*_*eas 7 kubernetes-helm

.Files.Glob我正在尝试在 Helm 模板中进行迭代。我将一个字符串值传递到模板中,因此我已切换为使用$.Files.Glob来引用根上下文。但是我收到一条错误消息,指出字符串类型上没有“文件”字段。

错误:

error calling include: template: example/templates/_helpers.tpl:77:29: executing "apiMounts" at <$.Files.Glob>: can't evaluate field Files in type string
Run Code Online (Sandbox Code Playgroud)

模板:

{{- define "apiMounts" -}}
{{- range $path, $_ := $.Files.Glob . }}
{{- $name := (base $path) }}
- name: specs
  mountPath: {{ printf "/etc/apis/%s" $name }}
  subPath: {{ sha256sum $name }}
{{- end }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)

我如何使用模板(部署):

          volumeMounts:
            {{- include "apiMounts" "common/**.json" | indent 12 }}
            {{- include "apiMounts" "v1/**.json" | indent 12 }}
Run Code Online (Sandbox Code Playgroud)

根据Helm 文档$应该始终引用根上下文,但在这种情况下,它似乎仍然引用我传递给模板的字符串。

然而,有一个变量始终是全局的——$该变量将始终指向根上下文。当您在某个范围内循环并且需要知道图表的版本名称时,这非常有用。

如何.Files.Glob在这个模板中使用,同时仍然传递字符串值?

eri*_*sas 13

您传递给 include 函数的值将成为根上下文。

您可以通过传入包含它和您的参数的列表来获取模板函数内的原始根上下文:

{{- include "apiMounts" (list $ "common/**.json") | indent 12 }}
Run Code Online (Sandbox Code Playgroud)

在模板函数中,恢复“根”上下文并获取您的参数:

{{- define "apiMounts" -}}
{{- $ := index . 0 }}
{{- $arg := index . 1 }}
{{- range $path, $_ := $.Files.Glob $arg }}
...
Run Code Online (Sandbox Code Playgroud)