Helm - comma-separated list of dynamic strings - kubernetes

Is it possible, within a helm chart to create a single string which is a comma-separated representation (similar to using the ",".join() command in Python) of strings with a common prefix and a variable suffix?
For example, I have a CLI application that requires an argument like so via the extraArgs parameter in a kubernetes pod definition:
extraArgs: >-
-M {{ $.Values.global.hostname }}/100
I now have to modify this value to be over a range (i.e. from {{$.Values.global.minval}} to {{$.Values.global.maxval}}, inclusive). So, for a minval=100 and maxval=105, my chart needs to now become (note the lack of a trailing comma, and no spaces other than the space after -M):
extraArgs: >-
-M {{ $.Values.global.hostname }}/100,{{ $.Values.global.hostname }}/101,{{ $.Values.global.hostname }}/102,{{ $.Values.global.hostname }}/103,{{ $.Values.global.hostname }}/104,{{ $.Values.global.hostname }}/105
Is there some way I can execute this in a range/loop in my chart? I have several instances of this chart that will use different min/max values, and I'd like to automate this tedious task as much as I can (additionally, I do not have access to the app's source, so I can't change the CLI interface to the application).
In Python, I could accomplish this roughly by:
minval = 100
minval = 105
s = "-M "
L = []
for i in range(minval, maxval+1):
L.append("{{{{ $.Values.global.hostname }}}}/{}".format(i))
s = s + ",".join(L)
# print(s)
I'm not sure where to begin doing this in a Helm template beyond starting with the range() function.

Helm includes the sprig library of template functions which contains untilStep and join.
There is no concept of a map or each operator in sprig yet so you can construct the list in a range loop to be joined later (from here)
{{- $minval := int .Values.minval -}}
{{- $maxval := int .Values.maxval | add1 | int -}}
{{- $args := list -}}
{{- range untilStep $minval $maxval 1 -}}
{{- $args = printf "%s/%d" $hostname . | append $args -}}
{{- end }}
extraArgs: '-M {{ $args | join "," }}'

Related

Helm equals to one of multiple values

I want to have a condition that will be considered as "true" if .Values.envName is "dev" + the release namespace name is one of a closed list. otherwise, it should be "false".
Tried the following, but seems like it gets unexpected "false" when I ran it with .Values.envName = dev & .Values.envName = ns-1:
env:
- name: MY_ENV
{{- if and (eq .Values.envName "dev") (regexMatch "^(?!^ns-1$)(?!^ns-2$).*$" .Release.Namespace)}}
value: 'true'
{{- else }}
value: 'false'
{{- end }}
A general note - if there is a better way to use eq with multiple possible values please let me know.
You can use has to test if some item is in a list; and then you can use list to construct a list of known items.
So for your example, if the test is that envName is exactly dev, and the namespace is either ns-1 or ns-2, then you can say
env:
{{- $isDev := eq .Values.envName "dev" }}
{{- $isNs := list "ns-1" "ns-2" | has .Release.Namespace }}
- name: MY_ENV
value: {{ and $isDev $isNs | toString | quote }}
You could do this case with a regular expression too. The start-of-string test ^ and the end-of-string test $ generally need to be outside parentheses; I wouldn't worry about "non-greedy matches" if you're just trying to determine whether a string matches without extracting things. Here I might write either of
{{- $isNs := regexMatch "^ns-[12]$" .Release.Namespace }}
{{- $isNs := regexMatch "^ns-1|ns-2$" .Release.Namespace }}
If you've got an enumerated list of values that don't neatly fall into a regex, using has and list is probably clearer.

Go template: is there any item in list of objects with a specific attribute value?

I'm using helm (sprig, go templates). I'm trying to build guards to selectively include stuff in my helm chart, but only if one of the components needs them.
So, I have a list:
- name: foo
flag1: true
flag2: false
flag3: false
- name: bar
flag1: false
flag2: true
flag3: false
I want to do something akin to a (pseudocode) list.any(flag), where over a variable length list, if I passed in flag1 or flag2 I'd get back true, but flag3 would get me false. If possible, I'd like to be able to ask about a different flag without repeating myself each time.
Is there a concise way to accomplish this? Can it be done?
The set of things that are and aren't possible in Go templates can be a little mysterious. A named template always returns a string, but an empty string is logically "false", so it should be possible to write a template call like
{{- if (include "list.any" (list .Values.options "flag2")) }}
...
{{- end }}
A template only takes a single parameter, so in the call we've packed the multiple inputs we need into a list. We've also used the Helm-specific include function to invoke a template and get its output as a string.
How can the template work? Template range loops don't have break or return actions or any other way to stop early. If we only want to output the "success" value once, this means we need to manually iterate through the list. For reasonably short lists, a recursive template call works here.
(For this specific thing, outputting yes or yesyesyes would both be non-empty and therefore logically "true", so you could use a range loop here successfully. This would not work for an equivalent list.all, though.)
In the template definition
{{- define "list.any" -}}
...
{{- end -}}
we need to start by unpacking the parameter list
{{- $list := index . 0 -}}
{{- $search := index . 1 -}}
We only do something if the list is non-empty.
{{- if $list -}}
...
{{- end -}}
If it is non-empty, we can split out its first element. We expect that to be a map, so we can look up the requested key in that with the standard index function. This will return nil if the key is absent and false if it's false, both of which are logically false; if it's true then the if test will pass.
{{- if index (first $list) $search -}}
...
{{- else -}}
...
{{- end -}}
If we do find the item, we can write out a success value and not do anything else
yes
If we don't, then we can recursively call ourselves with the remainder of the list.
{{- include "list.any" (list (rest $list) $search) -}}
Combining that all together gives this template (indented for clarity, the - markers will consume all of the whitespace):
{{- define "list.any" -}}
{{- $list := index . 0 -}}
{{- $search := index . 1 -}}
{{- if index (first $list) $search -}}
yes
{{- else -}}
{{- include "list.any" (list (rest $list) $search) -}}
{{- end -}}
{{- end -}}

How to use printf in this Helm template function?

I created this Helm template function in my templates/_helpers.yaml file. It simply gets the gets the value of an array element (the index .Values... part), based on the passed-in environment. It works fine.
{{/*
Function to get min CPU units
*/}}
{{- define "microserviceChart.minCpuUnits" -}}
{{ index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) | quote }}
{{- end }}
For example, in my values.yaml file
environments:
sandbox: 0
staging: 1
production: 2
valuesPerEnvironment:
cpuUnits: [512, 512, 1024]
so my template function returns "512", "512", "1024" based on my passed-in environment. However, can I use printf to it adds m to these values? In other words, I want it to return "1024m" for production. I tried the following but I get a syntax error
{{/*
Function to get min CPU units
*/}}
{{- define "microserviceChart.minCpuUnits" -}}
{{- printf "%dm" index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) | quote }}
{{- end }}
Instead of trying to get the arguments in the right order for printf, you could include the m in the template body. You'll also need to make the "..." explicit, instead of using the quote function, to get the m inside the quotes.
Expanded out (note that {{- and -}} will delete whitespace before and after, including newlines):
{{- define "microserviceChart.minCpuUnits" -}}
"
{{- index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) -}}
m"
{{- end }}
The heck with it. I just made my values.yaml like this, and I get the same result.
environments:
sandbox: 0
staging: 1
production: 2
valuesPerEnvironment:
cpuUnits: [512m, 512m, 1024m]
It'd still be cool to know if I can do this in the function, so if someone answers the actual question, I'll accept that as the answer.

Add suffix to each list member in helm template

I'm trying to iterate over a list in a helm template, and add a suffix to each member.
I currently have this block of code that does exactly that:
{{- range $host := .Values.ingress.hosts }}
{{- $subdomain := initial (initial (splitList "." $host)) | join "." }}
{{- $topLevelDomain := last (splitList "." $host) }}
{{- $secondLevelDomain := last (initial (splitList "." $host)) }}
- host: {{- printf " %s-%s.%s.%s" $subdomain $environment $secondLevelDomain $topLevelDomain | trimSuffix "-" }}
{{- end }}
Since I need to do the exact same manipulation twice in the same file, I want to create a new list, called $host-with-env, that will contain the suffix I'm looking for. That way I can only perform this operation once.
Problem is - I've no idea how to create an empty list in helm - so I can't append items from the existing list into the new one.
Any idea how can I achieve this?
I'm also fine with altering the existing list but every manipulation I apply to the list seems to apply to the scope of the foreach I apply to it.
Any ideas how to go about this?
It might not be quite clear what result are you trying to achieve, it will be helpful to add your input, like your values.yaml and the desired output. However, I added an example that answers your question.
Inspired by this answer, you can use dictionary.
This code will add suffix to all .Values.ingress.hosts and put them into $hostsWithEnv dictionary into a list, which can be accessed by myhosts key
values.yaml
ingress:
hosts:
- one
- two
configmap.yaml
{{- $hostsWithEnv := dict "myhosts" (list) -}}
{{- range $host := .Values.ingress.hosts -}}
{{- $var := printf "%s.domain.com" $host | append $hostsWithEnv.myhosts | set $hostsWithEnv "myhosts" -}}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
{{- range $hostsWithEnv.myhosts}}
- host: {{- printf " %s" . | trimSuffix "-" }}
{{- end }}
output
$ helm install --debug --dry-run .
[debug] Created tunnel using local port: '62742'
...
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
- host: one.domain.com
- host: two.domain.com

Caculating the global index for two ranges in Kubernetes Sprig/helm templates?

In a Helm Chart I have to following values
dataCenters:
- name: a
replicas: 3
- name: b
replicas: 2
When generating the template I would like my output to be like the following
server.1 = a-1
server.2 = a-2
server.3 = a-3
server.4 = b-1
server.5 = b-2
I tried this code
{{- $index := 0 -}}
{{ range $dc := .Values.cluster.dataCenters -}}
{{ range $seq := (int $dc.replicas | until) -}}
{{- $index := (add $index 1) -}}
server.{{ $index }}={{ $dc.name }}-{{ $seq }}
{{ end -}}
{{ end -}}
however in helm templates I don't thing you can reassign the value of the index as my 4th line is attempting and because of that I get out
server.1 = a-1
...
server.1 = b-2
How does one calculates the global index 0 to 4 (1 to 5 in my situation) using the Sprig/Helm templating language?
I have a way to do it that involves some trickery, heavily inspired by functional programming experience.
A Go/Helm template takes a single parameter, but the sprig library gives you the ability to create lists, and the text/template index function lets you pick things out of a list. That lets you write a "function" template that takes multiple parameters, packed into a list.
Say we want to write out a single line of this output. We need to keep track of which server number we're at (globally), which replica number we're at (within the current data center), the current data center record, and the records we haven't emitted yet. If we're past the end of the current list, then print the records for the rest of the data centers; otherwise print a single line for the current replica and repeat for the next server/replica index.
{{ define "emit-dc" -}}
{{ $server := index . 0 -}}
{{ $n := index . 1 -}}
{{ $dc := index . 2 -}}
{{ $dcs := index . 3 -}}
{{ if gt $n (int64 $dc.replicas) -}}
{{ template "emit-dcs" (list $server $dcs) -}}
{{ else -}}
server.{{ $server }}: {{ $dc.name }}-{{ $n }}
{{ template "emit-dc" (list (add1 $server) (add1 $n) $dc $dcs) -}}
{{ end -}}
{{ end -}}
At the top level, we know the index of the next server number, plus the list of data centers. If that list is empty, we're done. Otherwise we can start emitting rows from the first data center in the list.
{{ define "emit-dcs" -}}
{{ $server := index . 0 -}}
{{ $dcs := index . 1 -}}
{{ if ne 0 (len $dcs) -}}
{{ template "emit-dc" (list $server 1 (first $dcs) (rest $dcs)) -}}
{{ end -}}
{{ end -}}
Then in your actual resource definition (say, your ConfigMap definition) you can invoke this template with the first server number:
{{ template "emit-dcs" (list 1 .Values.dataCenters) -}}
Copy this all into a dummy Helm chart and you can verify the output:
% helm template .
---
# Source: x/templates/test.yaml
server.1: a-1
server.2: a-2
server.3: a-3
server.4: b-1
server.5: b-2
I suspect this trick won't work well if the number of servers goes much above the hundreds (the Go templating engine almost certainly isn't tail recursive), and this is somewhat trying to impose standard programming language methods on a templating language that isn't quite designed for it. But...it works.