HELM语法


模板函数

 

{{ quote .values.drink }}  == {{ .values.drink | quote }}

quote字符加双引号

upper大写

repeat [number]重复几次

default 变量不存在用默认值替换

title 首字母大写

b64enc 转化为base64编码

 

控制结构:

 

if/else 创建条件块,结束用end

 

{{- if values.drink "ccc" }}

 

-代表删除空格,前面的删除前面的空格

 

{{- end }}

 

with 指定一个范围

 

[root@master test]# cat Values.yaml data: drink: tea food: apple [root@master templates]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: {{- with .Values.data }} drink: {{ .drink | quote }} test: {{ .tea | default "tea" | quote }} food: {{ .food | repeat 5 | upper | quote }} {{- if eq .drink "tea" }} green: true {{- end -}} {{- end -}}

上面例子就相当于给定了一个范围在这个范围内使用范围内的值,可以省略一些前缀。类似于绝对路径与相对路径查看文件。

都一点需要注意如果用with圈定了范围,其他再从顶级调用值就会失败。

 

例如在wiht中调用Release.Name。如果还是想在with调用范围外的,可以在范围之外把想用的顶级内置函数定义为变量{{- $name := .Release.Name }},在后面引用变量即可。

 

如下:

 

 

[root@master templates]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data:  {{- $name := .Release.Name }} {{- with .Values.data }} drink: {{ .drink | quote }} test: {{ .tea | default "tea" | quote }} release: {{ $name }} {{- if eq .drink "tea" }} green: true {{- end }} {{- end }} pizza: |- {{- range .Values.pizza }} - {{ . | quote }} {{- end }}

 

range 提供了一个像for样式的循环(这个有go语言循环的那种特性range key,value := .Values支持匿名函数)

 

[root@master templates]# cat ../Values.yaml data: drink: tea food: apple pizza: - cheese - peppers - apple [root@master templates]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: {{- with .Values.data }} drink: {{ .drink | quote }} test: {{ .tea | default "tea" | quote }} {{- if eq .drink "tea" }} green: true {{- end }} {{- end }} pizza: |-
#range前面如果有end记得结尾部分不能有去空格字符会报错,具体原因应该是格式导致的
{{
- range .Values.pizza }} - {{ . | title| quote }}
#点意味着当前范围的或者换句话说就是顶级目录test(项目根目录)/如果类似与在for range的范围内代表去当前变量中的值
{{
- end }}

 

 

 

 

声明:

 

define 在模板中宣告一个新的命名模板

 

 

[root@master templates]# cat configmap.yaml {{- define "test.labels" }} labels: app: test date: {{ now | htmlDate }} {{- end }} apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap  {{- template "test.labels" }} data: hello: world

 

可以定义在_helpers.tpl里面,同时也可以添加注释。方法是{{/*。。。。*/}}

 

 

 

 

 

template 导入一个命名模板

 

有一个细节,在导入模板被呈现的时候,它会接收到模板调用传入范围。如果定义的东西有不在范围内的,那么就会报错或者不显示

 

例如:{{- template "test.labels" }}

 

这个很好解决如果没有范围,那么我们就给他一个范围

 

例如:{{- template "test.labels" . }}点意味着当前范围的或者换句话说就是顶级目录test(项目根目录)

 

 

下面是代码示例:

 

 

[root@master templates]# cat configmap.yaml {{- define "test.labels" }} labels: app: test date: {{ now | htmlDate }}  chart: {{ .Chart.Name }} food: {{ .Values.data.food }} {{- end }} apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap  {{- template "test.labels" }} data: hello: world 执行后报错如下: [root@master templates]# helm install test  /root/test/ --debug --dry-run -f /root/test/Values.yaml install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /root/test Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: [unknown object type "nil" in ConfigMap.metadata.labels.chart, unknown object type "nil" in ConfigMap.metadata.labels.food] helm.go:81: [debug] error validating "": error validating data: [unknown object type "nil" in ConfigMap.metadata.labels.chart, unknown object type "nil" in ConfigMap.metadata.labels.food]

 

原因参数不在定义范围之内。

 

解决办法:

 

 

[root@master templates]# cat configmap.yaml {{- define "test.labels" }} labels: app: test date: {{ now | htmlDate }} chart: {{ .Chart.Name }} food: {{ .Values.data.food }} {{- end }} apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap {{- template "test.labels" . }} data: hello: world 执行结果正常: # Source: test/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: test-configmap labels: app: test date: 2021-05-02 chart: test food: apple data: hello: world

 

还有一点,请注意,下面示例中app_version在两个位置上的缩进都是错误的。为什么?因为替换的模板的文本右对齐。因为template模板是一个动作不是函数。所以没有办法将调用的输出传递给其他函数,数据只会被简单的插入,那么在Kubernetes中有可能因为格式导致部署失败。

 

示例如下:

 

 

{{- define "test.labels" -}}
app_name: {{ .Chart.Name }}
app_version: "{{ .Chart.Version }}+{{ .Release.Time.Seconds }}"
{{- end -}}

 

现在说要将此插入到labels:模板的data:部分以及该部分中:

 

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  labels:
    {{ template "test.labels" .}}
data:
  myvalue: "Hello World"
  {{- range $key, $val := .Values.favorite }}
  {{ $key }}: {{ $val | quote }}
  {{- end }}
{{ template "test.labels" . }}

 

输出将不会是我们期望的:

 

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: measly-whippet-configmap
  labels:
    app_name: mychart
app_version: "0.1.0+1478129847"
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "pizza" app_name: mychart app_version: "0.1.0+1478129847"

 

 

 

 

解决办法:

 

Helm提供了一种替代方法,template可以将模板的内容导入到当前管道中,然后可以将其传递给管道中的其他功能。

 

{{ include “test.labels” . | indent 2 }}

 

indent缩进得意思,可以理解为空格。

 

indent缩进是根据上级结构计算。如果上级目录是data就需要与data对齐,不能超过data。

 

.Files通常出于安全原因,某些文件无法通过该对象访问

 

  • templates/无法访问中的文件。
  • .helmignore无法访问使用排除的文件。

 

 

block 声明一种特殊的可填充模板区域

 

 

判断为false:

bool false

0

空值

空串

空集合(map,slice,tuple,dict,array)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM