kubernetes - 什么是,什么用例有点“。” 在掌舵图中?
问题描述
我目前正在浏览 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 }}
谢谢您的帮助
解决方案
一般来说,.
在 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 }}
和.
之前root
并key
在字典结构中向下导航一级。
第三种用途,也可能是让您感到困惑的用途,.
它本身就是一个变量。
{{ . }}
您可以对其进行字段查找,并且您有一些示例:.Files
与 相同index . "Files"
,并在对象上查找字段“文件” .
。
您.
在几个地方用作变量:
{{- $files := .Files }} {{/* Get "Files" from . */}}
{{ . }} {{/* Write . as a value */}}
{{ include "mychart.app" . }} {{/* Pass . as the template parameter */}}
.
棘手的是它具有某种上下文含义:
- 在顶层, Helm使用键、、和初始化
.
一个对象。Files
Release
Values
Chart
- 在
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 }}
推荐阅读
- angular - npm 包版本的依赖错误,但它们已安装
- django - 无法在“django”中找到属性“应用程序”
- ios - 如何从 PHPickerResult 获取 PHAsset?iOS 14
- android - 在 Kotlin Flow android 中从 OKHTTP 拦截器捕获自定义异常
- go - 如何在 .proto 文件中允许包名称的减号?
- wdk - windows WDK 例程命名规则
- laravel - how collect numbers in database in laravel
- javascript - VueJS - 无法将道具从父组件传递给子组件
- javascript - node js - sscanf 在使用以下格式“P%fM%f”时抛出类型错误
- javascript - CSS - 如何堆叠两个元素相互叠加在一起调整大小?