Dynamically set the config map name and values to read in helm?

12/4/2017

I am new to helm charts. So please correct me if I am going wrong in understanding. I have a service which I am trying to deploy using helm charts. I want to change the config map name and its key values to read depending on deployment environment. Hence I want to add conditional logic in values.yaml.

Can someone point me to some document/link which explains how to add conditional logic in values.yaml?

-- Parag Kulkarni
kubernetes
kubernetes-helm

2 Answers

12/4/2017

A chart's values.yaml is primarily used to set default values, regardless of the environment. It exists to fill chart templates with values. It is not designed to be a template itself, so there is no logic you can apply inside a values.yaml file.

Each environment should have its own values.yaml file. You could store those inside the chart itself, like:

.
├── Chart.yaml
├── README
├── templates
│   ├── config.yaml
│   ├── deployment.app.yaml
│   └── service.app.yaml
├── values.prod.yaml
├── values.test.yaml
└── values.yaml

Now, when you deploy a chart, you can use the environment specific values.<env>.yaml to override the default values. For your test environment this may look like this:

helm upgrade --install my-chart path/to/my/chart --values path/to/my/chart/values.test.yaml

Of course you could store the values.<env>.yaml files also outside of your chart directory. You just need to find a way to make them available at chart upgrade/install time to override the chart templates default values.yaml.

-- fishi0x01
Source: StackOverflow

3/1/2019

One way of doing it would be to pass one value in with helm install like:

--set environment=<value>

And then have multiple set of values in your values file for different environments like:

environment: <default>
env1:
  prop1: <value1>
  prop2: <value2>
env2:
  prop1: <value1>
  prop2: <value2>

Now in your configMap file make use of it like:

{{- if eq .Values.environment "env1" }}
  somekey: {{ .Values.env1.prop1 }}
{{- else }}
  somekey: {{ .Values.env2.prop1 }}
{{- end }}

That should do the trick for setting dynamic values according to environment or any such condition.

Apart from that there is one more thing I would like to bring to your notice that helm has few more inbuilt object just like .Values, one of which is .Capabilities so may be you can make use of .Capabilities.KubeVersion.Platform to find OS of the system

-- Vikas Tawniya
Source: StackOverflow