首页 > 解决方案 > 什么是,什么用例有点“。” 在掌舵图中?

问题描述

我目前正在浏览 helm 的文档,并且点 (.) 至少有 3 种不同的用途,是否有任何具体定义?它与 bash 使用(实际文件夹/文件)有什么共同之处吗?

文档中的一些案例

这打印之前调用的范围内的访问文件?

  {{- $files := .Files }}
  {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
  {{ . }}: |-
    {{ $files.Get . }}
  {{- end }}

这告诉“mychart.app”使用当前文件夹中的文件(类似 bash 的行为)

{{ include "mychart.app" . | indent 4 }}

而这个,我猜它会从整个文件夹中获取值???我想这是不正确的,因为它不起作用(当时它是由另一名员工制作的,我必须修复它)

{{- define "read.select-annot" -}}
{{- range $key, $value := . }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}

谢谢您的帮助

标签: kuberneteskubernetes-helmgo-templates

解决方案


一般来说,.在 Helm 中,模板与文件或目录无关。

Helm 模板语言使用 Go 的文本/模板系统。句点字符可以通过几种不同的方式出现在那里。

首先,.可以是字符串中的一个字符:

{{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
{{/*             ^^^^^^^^^^^^
       this is a literal string "config1.toml"             */}}
...
{{- end }}

其次,.可以是查找运算符。您的问题中没有任何可靠的示例,但典型用途是查找值。如果你的values.yaml文件有

root:
  key: value

然后你可以展开

{{ .Values.root.key }}

.之前rootkey在字典结构中向下导航一级。

第三种用途,也可能是让您感到困惑的用途,.它本身就是一个变量。

{{ . }}

您可以对其进行字段查找,并且您有一些示例:.Files与 相同index . "Files",并在对象上查找字段“文件” .

.在几个地方用作变量:

{{- $files := .Files }}        {{/* Get "Files" from . */}}
{{ . }}                        {{/* Write . as a value */}}
{{ include "mychart.app" . }}  {{/* Pass . as the template parameter */}}

.棘手的是它具有某种上下文含义:

  • 在顶层, Helm使用键、、和初始化.一个对象FilesReleaseValuesChart
  • define模板中,.是模板的参数。(因此,当您include或时template,您需要.作为该参数向下传递。)
  • 在一个range循环中,.是正在迭代的当前项。
  • 在一个with块中,.是否存在匹配项。

特别是,与的交互range可能很棘手。让我们看一下循环的简化版本:

# {{ . }}
{{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
- {{ . }}
{{- end }}

range循环之外,.可能是顶级 Helm 对​​象。但在range循环内部,.是文件名(tuple依次来自每个值)。这就是您需要将值保存.到局部变量中的地方:

{{/* We're about to invalidate ., so save .Files into a variable. */}}
{{- $files := .Files }}

{{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
{{/* This . is the filename from the "tuple" call */}}
{{ . }}: |-
  {{/* Call .Get, from the saved $files, passing the filename .
       as the parameter */}}
  {{ $files.Get . }}
{{- end }}

推荐阅读