Syntax highlighting of devops/kubernetes/helm

= Helm =

<<TableOfContents()>>

[[https://helm.sh/||class=" moin-https"]]

[[https://helm.sh/docs/intro/install/|Installing Helm|class=" moin-https"]]

[[https://codersociety.com/blog/articles/helm-best-practices]]

== Commands ==

=== Env ===

{{{
helm env
# ...
# HELM_NAMESPACE

}}}

=== repo ===

https://helm.sh/docs/helm/helm_repo/

{{{#!highlight bash
# --- список "добавленных" репозиториев
helm repo list

# --- добавить репозиторий
helm repo add \
  nexus https://repo.corp.tander.ru/repository/app-charts \
  --ca-file ./certs/TanderCorpCA.crt

# --- обновление репозитория
#     выполняется всегда, после helm add
helm repo update

}}}

=== search ===

https://helm.sh/docs/helm/helm_search/

Helm Hub

{{{#!highlight bash
# --- search for charts in Helm Hub
helm search hub loki
# --- search for carts without output truncate
helm search hub loki --max-col-width=0

}}}

Repo

{{{#!highlight bash
# --- поиск крайней версии схемы в репозиториях, которые добавляются через helm repo addи (всех добавленных репозиториях?)
helm search repo kdk-standalone
# --- поиск крайней версии схемы для мажорной версии 0
#     подходят версии: 0.0.1, 0.1.0, 0.1.1, 0.123.123
#     не подходят версии: 1.0.0, 2.х.y
helm search repo kdk-standalone --version 0
# --- поиск крайней версии для мажорной и минорной версии
#     подходят версии: 1.2.0, 1.2.1, 1.2.123
#     не подходят версии: 1.1.0, 1.3.0, 2.x.y
helm search repo kdk-standalone --version 1.2

# --- поиск всех версий схемы
helm search repo kdk-cluster --versions
}}}

=== show chart info ===

{{{#!highlight bash
# --- show the chart definition
helm show chart bitnami/wordpress
# --- show chart README
helm show readme bitnami/wordpress
# --- show chart values
helm show values bitnami/wordpress
# --- show all information about chart
helm show all bitnami/wordpress

}}}

=== inspecting release ===

{{{#!highlight bash
helm get hooks RELEASE_NAME --namespace NAMESPACE
helm get manifest RELEASE_NAME --namespace NAMESPACE
helm get notes RELEASE_NAME --namespace NAMESPACE
helm get values RELEASE_NAME --namespace NAMESPACE
# --- history
helm history RELEASE_NAME

}}}

=== install / upgrade ===

{{{#!highlight bash
helm install --debug --dry-run test .

# --set    - values from command line
# --values - values from YAML file or URL

# --- install wordpress
helm install wordpress bitnami/wordpress \
  --values=wordpress-values.yaml \
  --namespace chapter3 \
  --version 8.1.0
# --- upgrade wordpress
helm upgrade ...

}}}

=== rollback / uninstall ===

{{{#!highlight bash
helm rollback RELEASE_NAME [REVISION] [flags]
helm uninstall RELEASE_NAME

}}}

== Creating chart ==

Scaffolding the initial file scructure

{{{#!highlight bash
helm create guestbook
cd guestbook

}}}

Modifying values

{{{#!highlight yaml
# values.yaml
redis:
  fullnameOverride: redis
  auth.enabled: false
  commonConfiguration: |-
    appendonly no

}}}

Install chart

{{{#!highlight bash
helm install my-guestbook .

}}}

Access to application

{{{#!highlight bash
k get po
export NODE_PORT=$(kubectl get --namespace chapter5 -o jsonpath="{.spec.ports[0].nodePort}" services my-guestbook)
export NODE_IP=$(kubectl get nodes --namespace chapter5 -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT

}}}

== Dependencies ==

    . See p. 120 in Learn Helm Packt https://trello.com/c/FmnGbXCi

Find char dependency

{{{#!highlight bash
helm search hub redis
helm add repo bitnami https://charts.bitnami.com
helm search repo redis --versions | head

}}}

Adding dependency and downloading it

{{{#!highlight yaml
# Chart.yaml
dependencies:
  - name: redis
    version: 16.8.9
    repository: https://charts.bitnami.com/bitnami

}}}

{{{#!highlight bash
helm dependency update

}}}

Show dependency values

{{{#!highlight bash
helm show values charts/redis-VERSION.tgz

}}}

== Templates ==

=== if / else ===

{{{
readinessProbe:
{{- if .Values.probeType.httpGet }}
  httpGet:
    path: /healthz
    port: 8080
    scheme: HTTP
{{- else }}
  tcpSocket:
    port: 8080
{{- end }}
  initialDelaySeconds: 30
  periodSeconds: 10

}}}

=== with ===

{{{
# --- NO with
cpu: {{ .Values.application.resources.limits.cpu }}
memory: {{ .Values.application.resources.limits.memory }}

# --- YES with
{{- with .Values.application.resources.limits }}
cpu: {{ .cpu }}
memory: {{ .memory }}
{{- end }}

}}}

=== range ===

{{{
# values.yaml
servicePorts:
  - name: http
    port: 8080
  - name: https
    port: 8443
  - name: jolokia
    port: 8778

# templates/foo.yaml
spec:
  ports:
{{- range .Values.servicePorts }}
    - name: {{ - name }}
      port: {{ .port }}
{{- end }}
# --- or
{{- range .Values.servicePorts }}
  - name: {{ $.Release.Name }}-{{ .name }}
    port: {{ .port }}
{{- end }}

}}}

=== Variables ===

{{{
{{ $myvar := 'Hello World!' }}
{{ $myvar := .Values.greeting }}
# ...
data:
  greeting.txt: |
    {{ $myvar }}

}}}

Using variables in a range block with index

{{{
data:
  greetings.txt: |
{{- range $index, $value := .Values.greetings }}
    Greeting {{ $index }}: {{ $value }}
{{- end }}
# --- same as
data:
  greetings.txt: |
    Greeting 0: Hello
    Greeting 1: Hola
    Greeting 2: Hallo

}}}

Using variables in a range block with key (???)

{{{
data:
  greetings.txt: |
{{- range $key, $val := .Values.greetings }}
    Greeting in {{ $key }}: {{ $val }}
{{- end }}
# --- same as
data:
  greetings.txt: |
    Greeting in English: Hello
    Greeting in Spain: Hola
    Greeting in German: Hallo

}}}

Using variables to refer to a value outside of the current scope

{{{
# --- FAIL
#     .Release.Name is not under scope of .Values.application.configuration
{{- with .Values.application.configuration }}
My application is called {{ .Release.Name }}
{{- end }}

# --- OK
{{ $appName := .Release.Name }}
{{- with .Values.application.configuration }}
My application is called {{ $appName }}
{{- end }}

}}}

=== Template functions ===

See https://helm.sh/docs/chart_template_guide/function_list/

=== Named Templates ===

{{{
# templates/_helper.tpl
{{- define 'mychart.labels' }}
labels:
  'app.kubernetes.io/instance': {{ .Release.Name }}
  'app.kubernetes.io/managed-by': {{ .Release.Service }}
{{- end }}

}}}

Using named template by `include [TEMPLATE_NAME] [SCOPE]`

{{{
# templates/some-resource.yaml
metadata:
  name: {{ .Release.Name }}
{{- include 'mycharts.labels' . | indent 2 }}

}}}

We can use `template` action instead `include`, but we can't use pipelines in this case.

== Hooks ==

{{{
apiVersion: batch/v1
kind: Job
metadata:
  name: helm-auditing
  annotations:
    'helm.sh/hook': pre-install,post-install
spec:
  template:
    metadata:
      name: helm-auditing
    spec:
      restartPolicy: Never
      containers:
        - name: helm-auditing
          command: ["/bin/sh", "-c", "echo Hook executed at $(date)"]
          image: alpine

}}}

{{https://clck.ru/sSM2x|helm hook annotations}}

== Troubleshooting ==

=== Error: directory charts/redis not found ===

Для новых зависимостей команда обновления выдает ошибку

{{{
helm dependency update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈
Error: directory charts/redis not found

}}}

Ошибка выдавалась по причине ошибки в слове `repository` в описании зависимости в Chart.yaml.install

{{{
helm install --debug --dry-run test .

}}}

    . Written with [[https://stackedit.io/|StackEdit|class=" moin-https"]].