new to k8s, trying to remove stupid boilerplate and write better config-map.yaml generation. Expected format is:
...
data:
first.property: 1
second.property: 2
...
I don't want to refer there key by key to values.yaml, like:
...
data:
first.property: {{.Values.configuration.first.property}}
second.property: {{.Values.configuration.second.property}}
...
I want to include whole subtree into here, like:
{{ (toYaml .Values.configuration | indent 2) }}
That works, but (as expected) the yaml is inserted as is. I need to adapt it to property-like format. So the question is: is there a function/way in helm/go templates how to transform this yaml:
a:
b:
c: 1
d: 2
into following representation?
a.b.c: 1
a.b.d: 2
this based on answer How do you apply a recursive formatting with Go Templates (Helm)? in which I fixed some problems and the final solution could be:
{{- define "flattenYaml" -}}
{{- $dict := . -}}
{{- $prefix := $dict.prefix -}}
{{- $data := $dict.data -}}
{{- $knd := kindOf $data -}}
{{- if eq $knd "map" }}
{{- range (keys $data) }}
{{- $key := . }}
{{- $prefixedKey := (printf "%s.%s" $prefix $key) }}
{{- $value := get $data $key }}
{{- $valueKind := kindOf $value }}
{{- if eq $valueKind "map" }}
{{- include "flattenYaml" (dict "prefix" ($prefixedKey) "data" $value) }}
{{- else }}
{{- printf "%s=%s\n" $prefixedKey (toJson $value) }}
{{- end }}
{{- end }}
{{- else }}
{{ toJson . }}#k({{ $knd }})
{{- end }}
{{- end -}}
for more details see my answer there.
Related
My goal is to convert values in Values.yaml into the following:
CUSTOM_VARIABLE: "TEST_ENV_1=devil,TEST_ENV_2=god,TEST_ENV_3=angel"
### Values.yaml
env:
TEST_ENV_1: devil
TEST_ENV_2: god
TEST_ENV_3: angel
The below template almost does this but I'm getting comma at the end: TEST_ENV_1=devil,TEST_ENV_2=god,TEST_ENV_3=angel,.
### _envVars.tpl
{{ define "envVars" }}
...
- name: CUSTOM_VARIABLE
value: "
{{- range $key, $value := .Values.env -}}
{{- printf "%s=%s," $key $value -}}
{{- end -}}
"
...
{{- end }}
Is there a way to apply template function (e.g. trunc to remove last symbol) to a range result in my case?
try something like
{{range $i, $e := $}}
{{if $i}},{{end}}
{{$e}}{{end}}
If actually look for the index and if it's zero it's wont to add the , at last. here is if is not behave like normal it checks the index also.
{{- range $i, $e := . -}}
{{if $i}}, {{end}}prefix_{{$e}}
{{- end}}
above loop will give output like : prefix_one, prefix_two, prefix_three
https://play.golang.org/p/KuRh55BHna8
Read more at : https://groups.google.com/g/golang-nuts/c/XBScetK-guk/m/Bh7ZFz6R3wQJ
If you write the range call into a helper template, Helm has an include extension function that calls a template and captures its output as a string.
{{/* Render the map-type template parameter to a key=value,key=value,
list, ending with a trailing comma. */}}
{{- define "custom.variable.format" -}}
{{- range $key, $value := . -}}
{{ $key }}={{ $value }},
{{- end -}}
{{- end -}}
- name: CUSTOM_VARIABLE
value: {{ include "custom.variable.format" .Values.env | trimSuffix "," | quote }}
(It is probably cleaner to not generate the comma at all, as #HarshManvar's answer proposes.)
I have a parent chart with 2 subcharts. The parent chart has global.myflag while the subcharts have myflag fields, in their respective values.yaml. I want the flexibility, where the sub-charts could be deployed independently. So, I have added a template function in the sub-chart _helper.tpl where I want to check
- if global.myflag exists, use that value
- else use value of myflag from the subchart
The template will return true/false. Something like this -
{{- define "isFlagEnabled" -}}
{{- $flag := false -}}
{{- if .Values.myflag -}}
{{- $flag := .Values.myflag -}}
{{- end -}}
{{- if .Values.global.myflag -}}
{{- $flag := .Values.global.myflag -}}
{{- end -}}
{{- printf "%s" $flag -}}
{{- end -}}
And using this value (true/false), I want to set some values in my config.yaml.
{{- if eq (value from template) true -}}
I am having two questions here -
1. Can we do 'if' condition on the template values? How?
2. Is there a better way to do this?
Definition of isFlagEnabled template
Retouched and cleaned your function
{{- define "isFlagEnabled" -}}
{{- if .Values.global -}} {{/* <-- check parent exists to avoid nil pointer evaluating interface {}.myflag */}}
{{- if .Values.global.myflag -}}
{{- .Values.global.myflag -}}
{{- end -}}
{{- else if .Values.myflag -}} {{/* <-- make sure its else if so you wont override if both defined */}}
{{- .Values.myflag -}}
{{- else -}}
{{- printf "false" }}
{{- end -}}
{{- end -}}
Using the template
Inside another template
When using template inside golang template syntax, you will need to escape them with round brackets:
{{- define "flagUsage" -}}
{{- if eq (include "isFlagEnabled" .) "true" -}}
{{- printf "%s" (include "isFlagEnabled" .) -}}
{{- end -}}
{{- end -}}
another example used inside a resource
Pay attention the template is being used twice in the example, once as an operand for the if operator and one as text for the label
{{- if eq (include "isFlagEnabled" .) "true" -}} {{/* <--- operand used in spring function surrounded by `{{ }}` */}}
apiVersion: v1
kind: Service
metadata:
name: {{ include "my-chart.fullname" . }}
labels:
my-meta-label: {{ include "isFlagEnabled" . }} {{/* <---- plain text */}}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "my-chart.selectorLabels" . | nindent 4 }}
{{- end }}
To build on #Totem's excellent and helpful answer, you can make the "isFlagEnabled" template reusable across multiple flags.
{{- define "isFlagEnabled" -}}
{{- $root := index . "root" -}}
{{- $flag := index . "flag" -}}
{{- if (index $root.Values $root.Chart.Name) -}}
{{- if hasKey (index $root.Values $root.Chart.Name) $flag -}}
{{- (index (index $root.Values $root.Chart.Name) $flag).enabled -}}
{{- end -}}
{{- else if hasKey $root.Values $flag -}}
{{- (index $root.Values $flag).enabled -}}
{{- else -}}
{{- printf "false" }}
{{- end -}}
{{- end -}}
Use it in the yaml file like so:
{{- if eq (include "isFlagEnabled" (dict "root" . "flag" "myflag")) "true" }}
Local values file:
myflag:
enabled: false
Overriding values file:
mychart:
myflag:
enabled: true
This worked for me in the situation where I had a subchart that needed to be overridden by a global chart, and I wanted to reference the chart by name in the global chart.
I'm trying to pass a dictionary from one helm template to another but it's resolved to null inside the called template.
Calling template - deployment.yaml
Called template - storageNodeAffinity
I see myDict printed as map inside deployment.yaml but inside storageNodeAffinity it's printed as null.
Eventually I need to pass nodeAffn from the values file.
deployment.yaml
{{- $myDict := dict "cpu" "amd" }}
{{- include "storageNodeAffinity" $myDict | indent 6 }}
{{printf "%q" $myDict}}
storage-affinity.tpl
{{- define "storageNodeAffinity" }}
{{/* {{- $myDict := dict "cpu" "amd" }}*/}}
{{printf "%q" .myDict}}
{{- range $key, $val := .myDict }}
- key: {{ $key }}
operator: In
values:
- {{ $val }}
{{- end }}
{{- end }}
values.yaml
nodeAffn:
disktype: "ssd"
cpu: intel
When you call a template
{{- include "storageNodeAffinity" $myDict -}}
then within the template whatever you pass as the parameter becomes the special variable .. That is, . is the dictionary itself; you don't need to use a relative path to find its values.
{{- define "storageNodeAffinity" }}
{{/* ., not .myDict */}}
{{printf "%q" .}}
{{- range $key, $val := . }}...{{ end -}}
{{- end -}}
I figured it out. The trick is to pass context of the parent variable for the variable you want to use in the called template. So here I'm passing "csAffn" as context and then using "nodeAffn" inside this context, in the called template (_additionalNodeAffinity)
_additionalNodeAffinity.tpl
{{- define "additionalNodeAffinity" }}
{{- range $key, $val := .nodeAffn }}
- key: {{ $key }}
operator: In
values:
- {{ $val }}
{{- end }}
{{- end }}
deployment.yaml
{{- include "additionalNodeAffinity" ( .Values.csAffn )
values.yaml
csAffn:
nodeAffn:
disktype: "ssd"
cpu: "intel"
I am doing this now:
value: {{ required "A valid .Values.foo entry required!" .Values.foo }}
But to give this same message for all required values in the templates is cumbersome and clutters the templates in my opinion.
Is there a better way where we could define it outside the template \ or a cleaner way to do it within the template itself?
You could do something by taking advantage of range and the fact that null will fail the required check. So in your values.yaml you could have this section for required env vars:
reqEnv:
- name: "VAR1"
value: null
- name: "VAR2"
value: null
And in the env section of the Deployment you then have:
{{- range .Values.reqEnv }}
{{ .name }}: {{ required "A value must be entered for all reqEnv entries" .value }}
{{- end }}
Then the user gets an error unless they set all required values of the reqEnv section in their values file or as paramters. Unfortunately what you lose by doing this is the detail of which var is missing. This could be why the official helm charts seem to prefer using required in the way that you already are.
Define the required values on top of your manifest as variables utilizing the required function.
E.g. deployment.yaml:
{{- $name := .Values.name | required ".Values.name is required." -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $name }}
....
You can use helm lint with --strict flag to check undefined values
$ helm lint --strict .
==> Linting .
[INFO] Chart.yaml: icon is recommended
[ERROR] templates/: render error in "mychart/templates/service.yaml": template: mychart/templates/service.yaml:10:19: executing "mychart/templates/service.yaml" at <.Values.foo>: map has no entry for key "foo"
Error: 1 chart(s) linted, 1 chart(s) failed
To expose name of missing item to required text you can do something like this:
{{- range $field, $my_key := $data }}
{{- if hasKey $dic1 $my_key }}
{{ $field }}: {{ index $dic1 $my_key | b64enc}}
{{- else if hasKey $dic2 $my_key }}
{{ $field }}: {{ index $dic2 $my_key | b64enc}}
{{- else }}
{{ $field }}: {{ required (printf "key %s is missing" $my_key) nil }}
{{- end }}
{{- end }}
I've come up with an alternative hack trying to solve this problem. It's questionable whether this is actually any better that the built in solution, but I thought it might be worth noting down here as an option.
Add a function to your _helpers.tpl (or wherever):
{{/*
Require and include a value
*/}}
{{- define "require" -}}
{{- $scope := index . 0 -}}
{{- $name := index . 1 -}}
{{required (print "Missing required value: " $name) (index $scope "Values" $name)}}
{{- end}}
Then in your template call it with:
value: {{ include "require" (list . "foo") }}
If a value is missing, it errors with message:
Missing required value: foo
Otherwise, it inserts the value.
Edit: To make this work for nested values, you need a slightly more complex helper:
{{/*
Index a nested component
*/}}
{{- define "indexNested" -}}
{{- $message := index . 0 -}}
{{- $object := index . 1 -}}
{{- $path := (mustRegexSplit "\\." (index . 2) -1) -}}
{{- range $path -}}
{{- if not $object -}}
{{ fail $message }}
{{- end -}}
{{- $object = index $object . -}}
{{- end -}}
{{ required $message $object }}
{{- end}}
{{/*
Require and include a value
*/}}
{{- define "require" -}}
{{- $scope := index . 0 -}}
{{- $name := index . 1 -}}
{{ include "indexNested" (list (print "Missing required value: " $name) $scope.Values $name) }}
{{- end}}
Now you can access the foo.bar value with:
{{ include "require" (list . "foo.bar") }}
I’m trying to change the value of a variable if another variable it set by combining the two with a dash in the middle, I’m not sure of the syntax to do this, I’m thinking of somethings like:
{{- $serviceNamespace := .Values.serviceNamespace -}}
{{- $serviceTag := .Values.serviceTag -}}
{{- if $serviceTag}}
{{- $serviceNamespace := .Values.serviceNamespace "-" .Values.serviceTag -}}
{{- end}}
Is this correct? if serviceNamespace was hello and serviceTag was 1.0.0 would I end up with serviceNamespace being hello-1.0.0?
For concatenation just use printf:
{{- $serviceNamespace := printf "%s-%s" .Values.serviceNamespace .Values.serviceTag -}}
You can simply do it like this , with string ":" in middle
"{{ $values.image.repository }}:{{ $values.image.tag }}"
Update
It is now possible in the 1.11 version of golang, see commit:
{{- $serviceNamespace := .Values.serviceNamespace -}}
{{- $serviceTag := .Values.serviceTag -}}
{{- if $serviceTag}}
{{- $serviceNamespace = print .Values.serviceNamespace "-" .Values.serviceTag -}}
{{- end}}
Notice the new = operator in $serviceNamespace = print .Values.serviceNamespace "-" .Values.serviceTag
Older golang versions
You cannot currently (in golang 1.9, but available in 1.11, see update above) reassign template variables because if introduces a new scope. Until this is fixed (see issue and proposed fix), you can work around this by writing a function:
{{ define "makeServiceNamespace" }}
{{- if .Values.serviceTag }}
{{- printf "%s-%s" .Values.serviceNamespace .Values.serviceTag -}}
{{- else }}
{{- print .Values.serviceNamespace }}
{{- end }}
{{- end }}
Then use it like so:
serviceNamespace: {{ template makeServiceNamespace . }}