I have a simple values.yml file for my Helm chart:
DbMigration:
Resources:
requests:
memory: 256Mi
limits:
memory: 512Mi
In a definition for my database migration job, I have this:
spec:
activeDeadlineSeconds: 120
template:
spec:
restartPolicy: Never
containers:
- name: myMigrate
image: myRepo/myService:0.0.1
imagePullPolicy: Always
resources:
requests:
{{- range $key, $value := $.Values.DbMigration.Resources.requests }}
{{ $key }}: {{ $value }}
{{- end }}
limits:
{{- range $key, $value := $.Values.DbMigration.Resources.limits }}
{{ $key }}: {{ $value }}
{{- end }}
Is is there any way to simplify the resources area so that I can just include all the data from $.Values.DbMigration.Resources? What I have works, but there must be a more concise way. I tried using the toYaml function in a fashion similar to this:
{{- toYaml $.Values.DbMigration.Resources }}
However, that results in:
Error: UPGRADE FAILED: YAML parse error on myTemplate.yaml: error converting YAML to JSON: yaml: line 30: mapping values are not allowed in this context
If you want to render the block from values.yaml "as is" then toYaml is pretty much all you are going to need.
spec:
activeDeadlineSeconds: 120
template:
spec:
restartPolicy: Never
containers:
- name: myMigrate
image: myRepo/myService:0.0.1
imagePullPolicy: Always
resources:
{{- toYaml $.Values.DbMigration.Resources | nindent 12 }}
If you are still having convert to JSON error try to play with indentation a bit, it's all there is.
There are multiple ways to achieve this. One of them is, instead of using range you can simply change the values.yaml into
DbMigration: |
resources:
requests:
memory: 256Mi
limits:
memory: 512Mi
and then make a change in migration template as
resources:
{{- .Values.DbMigration | indent 12 }}
Please change the indentation accordingly.
Another method is to use range and toYaml
DbMigration:
Resources:
requests:
memory: 256Mi
limits:
memory: 512M
migration template
resources:
{{- range $key, $value := $.Values.DbMigration.Resources }}
{{ $key }}:
{{ toYaml $value }}
{{- end }}
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