charts written presentation

Start

Quickly create a chart template helm create mychart, after executing the command to generate a mychart local directory.

The directory structure chart

  • Chart.yaml: The chart description files, including ico address, version information,
  • Variable to the template file used: vakues.yaml
  • charts: file other packages depend charts
  • requirements.yaml: rely on charts
  • README.md: the developer's own reading of the document
  • templates: a template file storage directory k8s
    • NOTES.txt documentation presented to the user after the helm install to see content
    • deployment.yaml create yaml file k8s resources
    • _helpers.tpl: initial underscore files that can be referenced by other templates.

A minimum of chart catalog, need only contain a Chart.yaml, and templates directory k8s a resource file such as:

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

The principle helm of template syntax:

- go-template双
- sprig

helm template syntax

  1. Template reference, {{ .Release.Name }}by injecting double parentheses, at the beginning of the decimal point represent namespace reference from the top level.

  2. helm built-in objects
    # Release, release相关属性
    # Chart, Chart.yaml文件中定义的内容
    # Values, values.yaml文件中定义的内容
  3. Pipes used in the template
    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. The if statement
    {{ if PIPELINE }}
    # Do something
    {{ else if OTHER PIPELINE }}
    # Do something else
    {{ else }}
    # Default case
    {{ end }}
  5. Operator, 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. Control statements block generation after rendering template will be more blank lines, we need {{- if ...}}ways to eliminate this blank line such as:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      {{- if eq .Values.favorite.drink "coffee"}}
      mug: true
      {{- end}}
  7. The introduction of relatively namespace, with the command:
    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 command to achieve the cycle, such as:

    # 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. Variable assignment
    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. Public templates, define the definition, template introduced in the default templates directory _ underscore beginning of the file as a common template (_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. an upgraded version of the statement include template, template statement is not connected to the pipe character in the back doing the introduction of the definition of variables,
    the include this feature is implemented.

    # _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. A pit 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 httpGet defined in values.yaml, it is necessary to manually set to null, and then set the exec probe.