How to add counter to helm range - kubernetes

Here is my values.yaml file:
options:
collection: "myCollection"
ttl: 100800
autoReconnect: true
reconnectTries: 3
reconnectInterval: 5
Now I'm trying to convert it into JSON in my configMap like this:
options: {
{{- range $key, $val := .Values.options }}
{{ $key }}: {{ $val | quote }},
{{- end }}
}
But I need to eliminate last comma in JSON so I'm trying to add a counter:
options: {
{{ $c := 0 | int }}
{{- range $key, $val := .Values.options }}
{{ if ne $c 0 }},{{ end }}
{{- $key }}: {{ $val | quote }}
{{ $c := $c add 1 }}
{{- end }}
}
But I'm getting following error for helm template ... command:
at <$c>: can't give argument to non-function $c
So what am I doing wrong?

Helm has an undocumented toJson template function, so if you can get your data in the right format you can just ask it to serialize it.
Managing the quoting for the embedded JSON file will be tricky. The two good options are to use a YAML block scalar, where indentation at the start of a line delimits the content, or treat it as binary data.
apiVersion: v1
kind: ConfigMap
metadata:
name: x
data:
optionsAsBlockScalar: >-
{{ .Values.options | toJson | indent 4 }}
binaryData:
optionsAsBase64: {{ .Values.options | toJson | b64enc }}
Note that this approach will preserve the native types of the objects in the JSON content; your example forces everything to strings. If you need everything to be a string then the Sprig support library contains functions to convert arbitrary objects to strings and to mutate a dictionary-type object in place, though this starts to get into the unfortunate land of writing real code in your templating language.

The simplest way to increment the counter in your case would be to replace
{{ $c := $c add 1 }}
with
{{ $c = add1 $c }}

Related

Helm - only create if nested values are set

I am wondering if there is a more efficient way to exclude any yaml keys which do not have a value set.
My current approach is to wrap each key in an if statement...
container:
spec:
{{- if values.spec.x }}
x: {{ values.spec.x }}
{{- end}}
{{- if values.spec.y }}
y: {{ values.spec.y }}
{{- end}}
{{- if values.spec.z }}
z: {{ values.spec.z }}
{{- end}}
e.g.
for each child of container.spec:
if the value != null:
include as child of spec
else:
exclude from spec
I thought about wrapping the above in a _helper.tpl function to try to keep the main template tidy, but it would still include writing multiple if statements.
Is there a better way of doing the above?
Thanks!
You can directly translate that pseudocode into Helm chart logic. The trick is that a Go template range loop is basically equivalent to a "for" loop in most languages. So:
container:
spec:
{{- range $key, $value := .Values.spec }}
{{- if ne $value nil }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
If you can just omit the unused keys from the values, then this becomes simpler and safer. Helm includes a lightly-documented toYaml function that will render an arbitrary structure as YAML, but you can't really do any filtering or other preprocessing before writing it out.
container:
spec:
{{ .Values.spec | toYaml | indent 4 }}

How can I apply template function to a range result in Helm?

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.)

helm - how to iterate over map with complex values

In a helm chart want to iterate over a map that contains structured values.
I do know how to iterate over a map with simple string values. I also can iterate over an array that contains structured values (not shown here). But I did not manage to iterate over a map that contains structured values.
This is my directory structure containing 3 files:
templates/test.yaml
Chart.yaml
values.yaml
A simple file Chart.yaml (just for completing the showcase):
---
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: foochart
version: 0.1.0
The file values.yaml with a map that contains simple string values (label) and one that contains structured values (label1):
---
label:
fook: foo
bark: bar
label1:
fook:
name: foo
value: foo1
bark:
name: bar
value: bar2
This template test.yaml works:
---
env:
{{- range $k, $v := .Values.label }}
- name: {{ $k }}
value: {{ $v }}
{{- end }}
But when I substitute .Values.label by .Values.label1, it produces no output.
This is my command for testing:
helm template foochart
Question: Is it possible to process a map with structured values? I would like to use something like $v.name. If yes, how can I do that?
You can in fact use syntax like $v.name, if you know that $v is a variable holding an object.
env:
{{- range $k, $v := .Values.label1 }}
- name: {{ $k }}_{{ $v.name }}
value: {{ $v.value }}
{{- end }}
If you know that it has exactly the syntax you want, there is an underdocumented toYaml function that takes an arbitrary object and returns it as unindented YAML. In your example, each of the values has the form of an env: item, and if you know (or specify) that, you can write out literally:
env:
{{- range .Values.label1 }}
- {{ . | toYaml | indent 4 | trim }}
{{- else }}
[]
{{- end }}
(In this last example: I'm not assigning a variable, so . is temporarily reassigned to each value in the map, and the keys are lost; for each item, I convert it to YAML, indent it by 4 spaces, but then trim out leading and trailing whitespace; and if there are no values, I explicitly write out an empty list.)
It's usually easier to specify a format you want your values to be in, and work with that. If you for some reason can't be sure which form you have, the template language includes functions to test on a value's type, so in principle you can test:
env:
{{- range $k, $v := .Values.labelN }}
{{- if kindIs "string" $v }}
- name: {{ $k }}
value: {{ $v }}
{{- else }}
- name: {{ $v.name }}
value: {{ $v.value }}
{{- end }}
{{- else }}
[]
{{- end }}

Adding news lines when defining collection

I am trying to define a collection (dict), and I would like to add a new line on each definition (for readability), Eg:
{{ $deployment := dict
"Release" .Release
"Chart" .Chart
"Values" .Values }}
But when I do this, helm respond a parse error :
Error: parse error in "XXX": template: XXX:2: unclosed action
Error: UPGRADE FAILED: parse error in "XXX": template: XXX:2: unclosed action
Is there a way in HELM to do this?
I achieved this by defining the dict first and then setting one key per line.
{{- $myDict := dict "" "" -}}
{{- $_ := set $myDict "myKey1" "myValue1" -}}
{{- $_ := set $myDict "myKey2" "myValue2" -}}
{{- $_ := set $myDict "myKey3" "myValue3" -}}
{{- $_ := set $myDict "myKey4" "myValue4" -}}
Bonus Tip: Since dict get function is available seemingly in only helm3 and later, you can use this hack to get a value from a dict to a string.
{{/* Hack needed until helm 3 which has 'get' for 'dict' */}}
{{- $myValue3Var := pluck "myKey3" $myDict | first -}}
TLDR;
It's impossible to declare dict in multiline way, like with Perl fat comma operator.
Please check the reference of "Sprig: Template functions for Go templates."
Instead you could use this sort of hacky way to achieve similar result:
Keep each key value pair in separate line, in Global Values file for readability:
# values.yaml
--
global:
someMap:
coffee: robusta
origin: Angola
crema: yes
Define helper template in _helpers.tpl:
{{- define "mychart.labels.standard"}}
{{- $global := default (dict) .Values.global.someMap -}}
Release: {{ .Release.Name | quote }}
Chart: {{ .Chart.Name }}
Values:
{{- $global := default (dict) .Values.global.someMap -}}
{{- range $key, $value := $global }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end -}}
Include it in another template:
helm_data:
{{- $global := default (dict) .Values.global -}}
{{- range $key, $value := $global }}
{{ $key }}: {{ $value }}
{{- end }}
{{ include "mychart.labels.standard" . | nindent 0 -}}
Render it to verify the result (helm template --name dict-chart .)
---
# Source: mychart/templates/data_type.yaml
helm_data:
someMap: map[crema:true origin:Angola coffee:robusta]
Release: "dict-chart"
Chart: mychart
Values:
coffee: robusta
crema: true
origin: Angol
It seems it's impossible to do so. The Helm templating system is basically the Go templating system. As stated in the Go templating docs:
Except for raw strings, actions may not span newlines, although comments can.
For people coming across this question, this functionality works in recent versions of HELM. For me, OPs example works as-is (Helm v3.8.2).
(I came across this question myself due to a mismatched ) in my template.)

How to use .Values in other variable loops

Below is my case:
{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.
spec:
{{- range $key, $value := $v.containers }}
containers:
- name: {{ $value.name }}
image: {{.Values.productpage_image}}:latest
Here when reaching .Values.productpage_image, it reports: can't evaluate field productpage_image in type interface {}.
Is there any usage error here? Why can I not use .Values.xxx in this loop? If I move the .Values to the first line, there is no error.
You can simply use $ to get to the root scope
Without defining what $root is, you can references .Values as $.Values from within a loop, or any other scope.
Source: https://github.com/kubeapps/kubeapps/pull/1057
It's because range changes a scope (see detailed description here https://github.com/helm/helm/blob/master/docs/chart_template_guide/control_structures.md#looping-with-the-range-action).
You can assign .Values.productpage_image to the variable outside the range and use inside.
As #abinet explained properly about the reason, I'll share my solution for that( which helped me a lot, and I hope that will save you time):
First, I saved the scope:
{{- $root := . -}}
and after that , I called the .Value inside the loop context like this:
{{ $root.Values.data }}
so basically , you code should be look like:
{{- $root := . -}}
{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.
spec:
{{- range $key, $value := $v.containers }}
containers:
- name: {{ $value.name }}
image: {{$root.Values.productpage_image}}:latest