On the road again

The article describes k8s Helm chart structure and basic concepts.

Chard directory structure:
mychart/
  Chart.yaml          # A YAML file containing information about the chart
  LICENSE             # OPTIONAL: A plain text file containing the license for the chart
  README.md           # OPTIONAL: A human-readable README file
  requirements.yaml   # OPTIONAL: A YAML file listing dependencies for the chart
  values.yaml         # The default configuration values for this chart
  charts/             # A directory containing any charts upon which this chart depends.
  templates/          # A directory of templates that, when combined with values,
                      # will generate valid Kubernetes manifest files.
  templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes
 
Create chart:
helm create mychart
Remove everything under ./mychart/templates
Create a configmap.yml file under ./mychart/templates:
cat ./mychart/templates/configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
{{- with .Values.database }}
  database: {{ .name | quote }}
  user: {{ .user | default "gnida" | upper }}
  password: {{ .password }}
{{- end }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
  files: |-
{{- range .Values.files }}
    - {{ . }}
{{- end }}
data:
  myvalue: "Hello World"
 
cat ./mychart/values.yaml:
database:
  name: testdb
  user: grudev
  password: djkujuhf9
files:
  - /usr/share
  - /etc/passwd
  - /var/log
 
cat ./mychart/templates/_helpers.tpl
{{/* Generate basic labels */}}
{{- define "mychart.labels" -}}
generator: helm
date: {{ now | htmlDate }}
chart: {{ .Chart.Name }}
version: {{ .Chart.Version }}
{{- end }}
 
cat ./mychart/templates/NOTES.txt
Thank you for installing {{ .Chart.Name }}.
 
### Values in "values.yaml" can be overridden with "--set=" command while installing chart. _helpers.tpl is generally used for some helpers e.g. named templates which can be included in other templates. NOTES.txt file contains post-installation notes to the user. It is parsed as a template.
 
This will create a ConfigMap k8s object when chart is installed.
Install newly created chart:
helm install ./chart
 
Debugging:
View all k8s objects installed by the chart:
helm get manifest <release>
It is possible just to check rendering of a chart without actual installation:
helm install --debug --dry-run ./mychart
Check if the chart meets standards and best practices:
helm lint
 
Template syntax.
IF statements:
{{ if and .Values.fooString (eq .Values.fooString "foo") }}
    {{ ... }}
{{ end }}
 
With white spaces control:
**{{- if eq .Values.favorite.drink "coffee"}}
  mug: true*
**{{- end}}
 
Modifying scope using "with":
{{- with .Values.database }}
  database: {{ .name | quote }}
  user: {{ .user | default "gnida" | upper }}
  password: {{ .password }}
{{- end }}
 
Assigning variables:
{{- $relname := .Release.Name -}}
 
Iterating through keys and values:
{{- range $key, $val := .Values.favorite }}
  {{ $key }}: {{ $val | quote }}
  {{- end}}
 
Working with files:
{{- $files := .Files }}
  {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
  {{ . }}: |-
    {{ $files.Get . }}
  {{- end }}

{{ $root := . }}
{{ range $path, $bytes := .Files.Glob "**.yaml" }}
{{ $path }}: |-
{{ $root.Files.Get $path }}
{{ end }}
Добавить комментарий