I have two helm charts, the second one is nested to the first tree output:
.
└── outerChart
├── charts
│ └── innerChart
│ ├── charts
│ ├── Chart.yaml
│ ├── templates
│ │ ├── deployment.yaml
│ │ ├── _helpers.tpl
│ │ ├── ingress.yaml
│ │ ├── NOTES.txt
│ │ ├── serviceaccount.yaml
│ │ └── service.yaml
│ └── values.yaml
├── Chart.yaml
├── templates
└── values.yaml
I have a global variable in the outer values.yaml:
global:
myreplicacount: 4
In the nested chart there is a values file with
replicaCount: 1
and a deployment that uses it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "foo.fullname" . }}
labels:
{{ include "foo.labels" . | indent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
The straight forward way in order to use the global var is to replace:
replicas: {{ .Values.replicaCount }}
with:
replicas: {{ .Values.global.myreplicacount }}
in the deployment.yaml.
But it possible to use that in the nested values.yaml? I would like to keep the inner chart intact. I am stuck in a situation where one software team provides me the inner chart and another one provides needed configuration through global variables. replicaCount is just an example, they have many variables that need to change and if I edit the inner chart that will make inner chart updates difficult.
Just adding:
replicaCount: {{ .Values.global.myreplicacount }}
in outerChart/charts/innerChart/values.yaml returns
): error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{".Values.global.myreplicacount":interface {}(nil)}
Inside the nested chart, .Values is seen as the contents of .Values.nested (using whatever name for the dependency is in requirements.yaml or Chart.yaml), merged with the top-level global: key.
# requirements.yaml
- name: nested # <-- this name will be used in values.yaml
version: "^1"
repository: ...
You can provide specific settings for that dependency using its name as a key in values.yaml. This doesn't require global: or any changes to the dependency chart.
# outer chart values.yaml, or `helm install -f` YAML file
nested: # <-- matches name: in requirements.yaml above
replicaCount: 1
For a Deployment replica count in particular, I'd prefer this option over possible alternatives. It's likely that each Deployment and StatefulSet will need a different replica count, especially across multiple charts, and it doesn't make sense to globally say "absolutely everything will have exactly 4 replicas".
If you can edit the nested chart, and you want it to optionally accept a global: value, you could honor that if a local value isn't set
# charts/nested/deployment.yaml
replicas: {{ .Values.replicaCount | default .Values.global.replicaCount | default 1 }}
Generally what you want to achieve is not really supported. However, I'm thinking about simple workaround that may help you.
Before running helm install command or whatever you using you can replace values in the nested values.yaml from the outer values.yaml by using yq utility (assuming you are in the same directory as from where you ran tree command):
yq e -i ".replicaCount = $(yq e '.global.myreplicacount' ./outerChart/values.yaml)" ./outerChart/charts/innerChart/values.yaml
We are setting the replicaCount value in the inner values.yaml to the value set in global.myreplicacount from the outer values.yaml only by using yq utility, you don't have to edit manually.
You can insert multiple yq commands in the script and just run it before every helm deployment so you will have values in nested values.yaml always up-to-date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With