Using multiple values.yaml, Functions "with" "range" & Variables - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki

Runtime parameters:

It would be difficult to set/change the required values to those keys defined in "values.yaml" when we use Helm charts in CI/CD pipelines as the values.yaml needs to be edited. There comes the handy run time parameters.

Example:

helm template test ./test-func/ --set image.repository="k8s/nginx"

======

Dealing with Multiple Values files

Scenario:

We've 3 environments such as Dev,QA & Prod to support. Each environment has unique settings which are stored in 2 files - values.yaml & config.yaml. The application settings are stored in config.yaml which is used to generate the configmap template. Each environment has its own directory under the path - ~/config/ to store the "config.yaml" for it. For example, prod environment has its @ ~/config/prod/config.yaml.

The key/values of "~/config/prod/config.yaml":

timeout: 2s
logs: /var/log/app.log
dbip: 34.12.133.64
activeconnections: 600
xms: 16G
xmx: 64G

The key/values of "values-prod.yaml":

configfilename: application.proprties
env: prod
imagename: nginx

hpa:
  enabled: true
  max: 10
  min: 2
  utilization: 60

The configmap template looks like below:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-configmap
  labels:
    app: mysql
data:
  {{ .Values.configfilename }}: |
{{ $path := (printf "config/%v/config.yaml" .Values.env )}}
{{ .Files.Get $path | indent 4 }}

A variable "$path" is used here to capture the path of config.yaml, and ".Files.Get" is used to fetch the contents of the same.

  • Execute Helm for creating templates:
helm template test -f values-prod.yaml ./test 

The rendered configmap.yaml :

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-configmap
  labels:
    app: mysql
data:
  application.properties: |
     timeout: 2s
     logs: /var/log/app.log
     dbip: 34.12.133.64
     activeconnections: 600
     xms: 16G
     xmx: 64G

======

"with" Control:

It sets a scope to reduce repetition with template files.

Example:

Values.yaml

favourite:
  drink: Coffee
  food: Pizza

Using "with" in a template file:

{{- with .Values.favourite. }}
drink: {{ .drink | default "Tea" | quote }}
food: {{ .food | upper | quote }}
{{- end }}

When executed, this will result in :

drink: "Coffee"
food: "PIZZA"

  • Looping through range of value with "Range" function:

Values.yaml


pizzaToppings:
  - mushrooms
  - cheese
  - peppers
  - onions

Template file:

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 }}
  toppings: |-
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }}

Note:

The title function coverts the given into camel text. See the output below.

 helm template mytest .

Output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mytest-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  toppings: |-
    - "Mushrooms"
    - "Cheese"
    - "Peppers"
    - "Onions"

======

  • Variables

Example to declare,assign & print a variable:

{{- with $relname := .Release.Name -}}

release: {{ $relname }} 

=====