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".

猜你喜欢

转载自blog.csdn.net/qq_26884501/article/details/108199657