operations

show all releases in all namespaces without any filter applied..

helm list -Aa

get the values for a helm release..

helm get values ${NAME}
helm get values ${NAME} --all

render templates..

helm template ${NAME} . -f values.yaml -s templates/deployment.yaml

get the helm release secret in plain text..

kubectl get secrets sh.helm.release.v1.traefik.v1 -n traefik -o jsonpath='{.data.release}' | base64 --decode | base64 --decode | gzip -d | jq

service accounts

https://github.com/kubernetes/kubernetes/issues/76367#issuecomment-481689160


mount directories

Mounting multiple directories to a pod while preserving the original directory structure.

Let’s say we have a directory with three files and a subdirectory with three more files inside it and we want to mount all of these into a pod..

.
├── animals
│   ├── cat
│   ├── dog
│   └── mouse
├── blue
├── green
└── magenta

We do the following..

  1. inside the Helm chart we create a directory called data and copy all the files
  2. under the templates directory we create a configmap.yaml file.. 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: data
    data:
    {{ $files                  := .Files }}
    {{ $filesLocalPath         := "data/**" }}
    {{ $trimPrefixUnderscores  := "data_" }}
    {{ range $path, $content   := .Files.Glob $filesLocalPath }}
    {{ regexReplaceAll "\\/+" $path "_" | trimPrefix $trimPrefixUnderscores | indent 2 }}: |-
    {{ $files.Get $path | indent 4  }}
    {{ end }}
    
  3. again under the templates directory we create a deployment.yaml file..
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: demo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: demo
      template:
        metadata:
          labels:
            app: demo
          annotations:
            checksum/configmap: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }} # we add this
        spec:
          containers:
            - name: app
              image: nginx
              volumeMounts:                                                                               # we add these
              {{ $filesLocalPath         := "data/**" }}                                                  #
              {{ $trimPrefixSlashes      := "data/" }}                                                    #
              {{ $trimPrefixUnderscores  := "data_" }}                                                    #
              {{ range $path, $content   := .Files.Glob $filesLocalPath }}                                #
              - name: data                                                                                #
                mountPath: /etc/data/{{ $path | trimPrefix $trimPrefixSlashes }}                          #
                subPath: {{ regexReplaceAll "\\/+" $path "_" | trimPrefix $trimPrefixUnderscores }}       #
                readOnly: true                                                                            #
              {{ end }}                                                                                   #
          volumes:                                                                                        #
          - name: data                                                                                    #
            configMap:                                                                                    #
              name: data                                                                                  #
    

How it works..

  • we use a ConfigMap to store the path and the content of the files in a key-value format
  • since the ConfigMap can not store file paths because a ConfigMap key can only contain alphanumeric characters and the ., - and _ symbols, we replace the / from the paths with _ so the file animals/cat is stored as animals_cat
  • when we mount the file to the pod we do the opposite and replace the _ with /

Considerations..

  • a disadvantage of this approach is that we can’t have files with names that contain underscores
  • the files from the ConfigMap are mounted when the pod starts and to update them the pod must be restarted or redeployed, to automate this we add an annotation to the pod with the SHA256 checksum of the rendered ConfigMap so when it changes the pod is automatically recreated
  • the files from the ConfigMap are mounted on the pod individually and not as a single directory, meaning that the contents of the animals directory are mounted as data/animals/cat, data/animals/dog and data/animals/mouse rather than replacing the entire data/animals directory of the pod, so if the data/animals directory already exists in the pod and contains files not defined in the ConfigMap (for example data/animals/tiger) those files will remain present after the ConfigMap is mounted

extra objects

When we need to apply additional YAML manifests alongside a Helm chart..

  1. under templates we create a file called extra-manifests.yaml with the following content..
    {{ range .Values.extraObjects }}
    ---
    {{ if typeIs "string" . }}
        {{- tpl . $ }}
    {{- else }}
        {{- tpl (toYaml .) $ }}
    {{- end }}
    {{ end }}
    
  2. in the values.yaml file we add the following block
    extraObjects: []
    

Example usage..

extraObjects:
- apiVersion: v1
  kind: ConfigMap
  metadata:
    labels:
    name: blue
  data:
    color: "blue"
- apiVersion: v1
  kind: ConfigMap
  metadata:
    labels:
    name: green
  data:
    color: "green"