Helm iterate over string with comma separated values - for-loop

I'd like to iterate over string which contain comma separated values using range. For example:
DNS_NAMES: example.com, example2.com, example3.com
and then iterate using range over values: example.com, example2.com, example3.com

use split
values.yaml
DNS_NAMES: example.com, example2.com, example3.com
templates/cm.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
config.yaml: |-
args:
{{- range ( split ", " $.Values.DNS_NAMES ) }}
- {{ . }}
{{- end }}
output.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
config.yaml: |-
args:
- example.com
- example2.com
- example3.com

Related

helm to iterate a list of lists

Given the follow values.yaml
configurations:
endpoints:
- firstEndpoint
- firstPath
- secondPath
- secondEndpoint
- thirdPath
- fourthPath
I need to generate different resources from those values in the following way:
- name: firstEndpoint
paths:
- firstPath
- secondPath
- name: secondEndpoint
paths:
- thirdPath
- fourthPath
I can do this if "endpoints" were to be a map, instead of a list/array, but in this case, I need "endpoints" to be a list of endpoints, and "paths" to be a list of paths for each endpoint.
How could this be achieved?
As David Maze said. Your proposed endpoints: isn't valid.
You may try this:
values.yaml
configurations:
endpoints:
- firstEndpoint:
- firstPath
- secondPath
- secondEndpoint:
- thirdPath
- fourthPath
(Note the : after firstEndpoint and secondEndpoint)
template/cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
test: |-
{{- range $_, $item := .Values.configurations.endpoints }}
{{- range $k, $v := . }}
- name: {{ $k }}
path:
{{- range $_, $path := $v }}
- {{ $path }}
{{- end }}
{{- end }}
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
test: |-
- name: firstEndpoint
path:
- firstPath
- secondPath
- name: secondEndpoint
path:
- thirdPath
- fourthPath

Helm Chart Error when using functions: " invalid value; expected string"

I defined the following ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-application-yaml
data:
application.yaml: |-
testOne: {{ .Values.testOne | nindent 6 }}
testTwo: {{ .Values.testTwo | nindent 6 }}
The values.yaml looks as follows:
test:
replicas: 1
testOne: |
testOne: "test1"
testTwo: "test2"
testTwo: |
testThree: "test3"
testFour: "test4"
When executing helm lint -f $v helm/charts/applications on Azure, the following error message is reported:
[ERROR] ... at <6>: invalid value; expected string
When trying it on my machine via helm template -f .\values.yaml . however, i get the configMap correctly indented:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-application-yaml
data:
application.yaml: |-
testOne:
testOne: "test1"
testTwo: "test2"
testTwo:
testThree: "test3"
testFour: "test4"
What am I missing here?

Convert helm yaml values to cli executable

how do I convert this yaml values in a way such that this become executable with helm install command?
hostAliases:
- ip: "12.20.30.40"
hostnames:
- "001.stg.local"
i want to execute from terminal like this
helm install my-app -n my-namespace app/app1 \
--set hostAliases=[]
values.yaml
hostAliases:
- ip: "12.20.30.40"
hostnames:
- "001.stg.local"
- "002.stg.remote"
- ip: "0.0.0.0"
hostnames:
- "003.stg.local"
- "004.stg.remote"
template/configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
{{- range $idx, $alias := $.Values.hostAliases }}
ip-{{ $idx }}: {{ $alias.ip }}
{{- range $i, $n := $alias.hostnames }}
host-{{ $idx }}-{{ $i }}: {{ $n }}
{{- end }}
{{- end }}
cmd
helm template --debug test .
output
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
ip-0: 12.20.30.40
host-0-0: 001.stg.local
host-0-1: 002.stg.remote
ip-1: 0.0.0.0
host-1-0: 003.stg.local
host-1-1: 004.stg.remote
cmd
helm template --debug test . --set "hostAliases[0].ip=1.2.3.4" --set "hostAliases[0].hostnames={001,002}"
output
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
ip-0: 1.2.3.4
host-0-0: 001
host-0-1: 002

Is it possible to use a template inside a template with go template

Using https://golang.org/pkg/text/template/, I sometimes need to use variables in the accessed path (for kubernetes deployments).
I end up writing something like :
{{ if (eq .Values.cluster "aws" }}{{ .Values.redis.aws.masterHost | quote }}{{else}}{{ .Values.redis.gcp.masterHost | quote }}{{end}}
What I'd really like to write is pretty much {{ .Values.redis.{{.Values.cluster}}.masterHost | quote }} , which doesn't compile.
Is there a way to write something similar ? (so having a kind of variable in the accessed path).
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"

Helm yaml keys from values.yaml

I want to make a yaml KEY (not the value) dynamically.
In my values.yaml
failoverip1: 0.0.0.0` (<- this is only a demo IP)
In my templates/configmap.yaml I have this:
apiVersion: v1
kind: ConfigMap
metadata:
name: vip-configmap
data:
{{- .Values.failoverip1 -}}: {{ .Release.Namespace -}}/{{- .Values.target -}}
^^^^^^^^^^^^^^^^^^^^^----> here should be an IP address from values.yaml
{{ .Release.Namespace -}}/{{- .Values.target -}} renders successfully.
But if I add {{- .Values.failoverip1 -}} to the key part, it renders nothing.
(Nothing means, the whole data: block, does not get rendered.
This is the error message when I run helm install --name hetzner-failover .
Error: YAML parse error on hetzner-failover/templates/configmap-ip.yaml: error converting YAML to JSON: yaml: line 4: mapping values are not allowed in this context
Is it not allowed to make a
key dynamic?
If not, how to drive around that?
Here is the repo I am talking of:
https://github.com/exocode/helm-charts/blob/master/hetzner-failover/templates/configmap-ip.yaml
The error seems to be, that the leading - got cut.
So the correct way is to remove that minus:
Before:
{{- .Values.failoverip1 | indent 2 -}}
After:
{{ .Values.failoverip1 | indent 2 -}}
The yaml is now:
apiVersion: v1
kind: ConfigMap
metadata:
name: vip-configmap
data:
{{ .Values.failoverip1 | indent 2 -}}: {{ .Release.Namespace -}}/{{- .Values.target -}} # add your config map here. must map the base64 encoded IP in secrets.yaml
And the rendered result is:
kubectl get configmap -o yaml
apiVersion: v1
items:
- apiVersion: v1
data:
0.0.0.0: default/nginx# add your config map here. must map the base64 encoded
IP in secrets.yaml
kind: ConfigMap

Resources