Sunday, September 18, 2022

To pick version from chart.yml,values.yaml of helm chart with if,else

 When a chart is created, it will have chart.yaml file.

You can refer content of chart.yaml in your templates.

Say, I want to set the configmap created using chart with dynamic name everytime.

templates\configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: first-chart-configmap-{{.Chart.Version}}
data:
  port: "8080"

So, every time when I upgrade the chart with helm upgrade, the configmap will be appended with the chart release version.

Like charts.yml content, values file content can be referred as {{ .Values.staging.sample-key }}

where in values.yml:

staging:
  sample-key: "testing"

Say I have set a property in the values.yml with name env.

Based on the env value, I can frame a condition to a configmap value.

apiVersion: v1
kind: ConfigMap
metadata:
  name: first-chart-configmap-{{.Chart.Version}}
data:
  port: "8080"
  {{if eq .Values.env "staging"}}
  allowTesting: "true"
  {{else}}
  allowTesting: "false"
  {{end}}

In values.yaml, say the content might be as below.


env: production

staging:
  sample-key: "testing"

No comments:

Post a Comment