helm模板文件chart編寫語法詳解

charts編寫介紹

開始

快速創建一個chart模板,helm create mychart,執行命令後本地生成一個mychart目錄.

chart目錄結構

  • Chart.yaml: 該chart的描述文件,包括ico地址,版本信息等
  • vakues.yaml: 給模板文件使用的變量
  • charts: 依賴其他包的charts文件
  • requirements.yaml: 依賴的charts
  • README.md: 開發人員自己閱讀的文件
  • templates: 存放k8s模板文件目錄
    • NOTES.txt 說明文件,helm install之後展示給用戶看的內容
    • deployment.yaml 創建k8s資源的yaml文件
    • _helpers.tpl: 下劃線開頭的文件,可以被其他模板引用.

一個最小的chart目錄,只需要包含一個Chart.yaml,和templates目錄下一個k8s資源文件.如:

    # mychart/Chart.yaml
    apiVersion: v1
    appVersion: 2.9.0
    version: 1.1.1

    # mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mychart-configmap
    data:
      myvalue: "Hello World"

helm的模板語法實現原理:

- go-template雙
- sprig

helm模板語法

  1. 模板引用方式,{{ .Release.Name }}, 通過雙括號注入,小數點開頭表示從最頂層命名空間引用.

  2. helm內置對象
    # Release, release相關屬性
    # Chart, Chart.yaml文件中定義的內容
    # Values, values.yaml文件中定義的內容
  3. 模板中使用管道
    apiVersion: v1
    kind: ConfigMap
    metadata:
       name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      drink: {{ .Values.favorite.drink | repeat 5 | quote }}
      food: {{ .Values.favorite.food | upper | quote }}
  4. if語句
    {{ if PIPELINE }}
    # Do something
    {{ else if OTHER PIPELINE }}
    # Do something else
    {{ else }}
    # Default case
    {{ end }}
  5. 操作符, and/eq/or/not

    {{/* include the body of this if statement when the variable .Values.fooString exists and is set to "foo" */}}
    {{ if and .Values.fooString (eq .Values.fooString "foo") }}
        {{ ... }}
    {{ end }}
    
    {{/* do not include the body of this if statement because unset variables evaluate to false and .Values.setVariable was negated with the not function. */}}
    {{ if or .Values.anUnsetVariable (not .Values.aSetVariable) }}
       {{ ... }}
    {{ end }}
  6. 控制語句塊在渲染後生成模板會多出空行,需要使用{{- if ...}}的方式消除此空行.如:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      {{- if eq .Values.favorite.drink "coffee"}}
      mug: true
      {{- end}}
  7. 引入相對命名空間,with命令:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      {{- with .Values.favorite }}
      drink: {{ .drink | default "tea" | quote }}
      food: {{ .food | upper | quote }}
      {{- end }}
  8. range命令實現循環,如:

    # values.yaml
    favorite:
      drink: coffee
      food: pizza
    pizzaToppings:
      - mushrooms
      - cheese
      - peppers
      - onions
    
    #configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      toppings: |-
        {{- range .Values.pizzaToppings }}
        - {{ . }}
        # .表示range的命令空間下的取值
        {{- end }}
        {{- range $key, $val := .Values.favorite }}
        {{ $key }}: {{ $val | quote }}
        {{- end}} 
  9. 變量賦值
    ApiVersion: v1
    Kind: ConfigMap
    Metadata:
      name: {{ .Release.Name }}-configmap
    Data:
      myvalue: "Hello World"
      # 由於下方的with語句引入相對命令空間,無法通過.Release引入,提前定義relname變量
      {{- $relname := .Release.Name -}}
      {{- with .Values.favorite }}
      food: {{ .food }}
      release: {{ $relname }}
      # 或者可以使用$符號,引入全局命名空間
      release: {{ $.Release.Name }}
      {{- end }}
  10. 公共模板,define定義,template引入,在templates目錄中默認下劃線_開頭的文件爲公共模板(_helpers.tpl)

    # _helpers.tpl文件
    {{- define "mychart.labels" }}
      labels:
        generator: helm
        date: {{ now | htmlDate }}
    {{- end }}
    
    # configmap.yaml文件
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
      {{- template "mychart.labels" }}
    data:
      myvalue: "Hello World"
  11. template語句的升級版本include,template是語句無法在後面接管道符來對引入變量做定義,
    include實現了此功能.

    # _helpers.tpl文件
    {{- define "mychart.app" -}}
    app_name: {{ .Chart.Name }}
    app_version: "{{ .Chart.Version }}+{{ .Release.Time.Seconds }}"
    {{- end -}}
    
    # configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
      labels:
        {{- include "mychart.app" . | nindent 4 }}
    data:
      myvalue: "Hello World"
      {{- range $key, $val := .Values.favorite }}
      {{ $key }}: {{ $val | quote }}
      {{- end }}
      {{- include "mychart.app" . | nindent 2 }}
    
    # 如果使用template只能手動空格,不能使用管道後的nindent函數來做縮進
  12. 一個坑helm install stable/drupal --set image=my-registry/drupal:0.1.0 --set livenessProbe.exec.command=[cat,docroot/CHANGELOG.txt] --set livenessProbe.httpGet=null<br/>,livenessProbe在values.yaml中定義了httpGet,需要手動設置爲null,然後設置exec的探針.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章