helm中-的作用

测试

  • 测试1
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{ end }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug2: true
  {{ end }}

渲染效果1

data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: true
  #空行
  #空行
  mug2: true
  #空行
  #空行
  • 测试2
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{ end }}
  {{- if eq .Values.favorite.drink "coffee" }}
  mug2: true
  {{ end }}

渲染效果2

data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: true
  #空行
  mug2: true
  #空行
  #空行
  • 测试3
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{ end }}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{ end }}

渲染效果3

data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: true
  mug2: true
  #空行
  #空行
  • 测试4
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{ end }}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{- end }}

渲染效果4

# Source: mychart/templates/cmdemo.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: release-name-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: true
  mug2: true
  #空行
  • 测试5
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{ end }}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{- end -}}

渲染效果5

# Source: mychart/templates/cmdemo.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: release-name-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: true
  mug2: true
  • 测试6
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{- end -}}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{- end -}}

渲染效果6

# Source: mychart/templates/cmdemo.yaml
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  #空行
  mug1: truemug2: true
  • 测试7
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{- end -}}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{- end -}}

渲染效果7

# Source: mychart/templates/cmdemo.yaml
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug1: truemug2: true
  • 测试8
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug1: true
  {{- end -}}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug2: true
  {{- end -}}

渲染效果8

# Source: mychart/templates/cmdemo.yaml
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"mug1: truemug2: true
  • 测试9
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee" }}
  mug1: true
  {{- end }}
  {{- if eq .Values.favorite.drink "coffee" }}
  mug2: true
  {{- end -}}

渲染效果9

data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug1: true
  mug2: true

结论

  • 总结

1、一个{{ and }}符号占用一个空行
2、{{- and }}消除左边产生的空行,即上面产生的空行
3、{{ and -}}消除右边产生的空行,即下面面产生的空行
4、多个{{ and }}时,注意只是用一个-号确保正确,最后一个{{ and }}要加入两个-消除尾部的空行,见测试9

helm官网解释

官网直通车

Notice that we received a few empty lines in our YAML. Why? When the template engine runs, it removes the contents inside of {{ and }}, but it leaves the remaining whitespace exactly as is.
YAML ascribes meaning to whitespace, so managing the whitespace becomes pretty important. Fortunately, Helm templates have a few tools to help.
First, the curly brace syntax of template declarations can be modified with special characters to tell the template engine to chomp whitespace. {{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed. Be careful! Newlines are whitespace!
Make sure there is a space between the - and the rest of your directive. {{- 3 }} means "trim left whitespace and print 3" while {{-3 }} means "print -3".

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了流程函数,Helm还提供了一些内置的函数,用于在Chart模板执行一些操作,例如字符串操作、数学计算、日期处理等。下面介绍一些常用的Helm函数及其作用。 1. `default`函数:用于设置默认值,如果变量未定义,则使用默认值。例如,可以使用`{{default "myapp" .Values.name}}`设置`.Values.name`的默认值为"myapp"。 2. `lower`函数:用于将字符串全部转换为小写。例如,可以使用`{{lower .Values.name}}`将`.Values.name`转换为小写。 3. `upper`函数:用于将字符串全部转换为大写。例如,可以使用`{{upper .Values.name}}`将`.Values.name`转换为大写。 4. `trim`函数:用于去除字符串前后的空格。例如,可以使用`{{trim .Values.name}}`去除`.Values.name`前后的空格。 5. `quote`函数:用于将字符串用引号括起来。例如,可以使用`{{quote .Values.name}}`将`.Values.name`用引号括起来。 6. `add`函数:用于将两个数相加。例如,可以使用`{{add .Values.replicas 1}}`将`.Values.replicas`加1。 7. `sub`函数:用于将两个数相减。例如,可以使用`{{sub .Values.replicas 1}}`将`.Values.replicas`减1。 8. `mul`函数:用于将两个数相乘。例如,可以使用`{{mul .Values.replicas 2}}`将`.Values.replicas`乘以2。 9. `div`函数:用于将两个数相除。例如,可以使用`{{div .Values.memory "1Gi"}}`将`.Values.memory`除以1Gi。 10. `now`函数:用于获取当前时间。例如,可以使用`{{now}}`获取当前时间。 这些内置函数可以简化Chart模板的编写,提高Chart的可读性和可维护性。同时,Helm还支持自定义函数,可以根据需要编写自己的函数来扩展Helm的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值