I am trying to build configmap data out of values I have in values.yaml.
CASE 1:
values.yaml:
dns_domains: abc xyz
dns_servers: IP1 IP2 IP3
I want configmap data something as below for the above values.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: Corefile
data:
abc:53 {
log
errors
cache 30
forward . IP1 IP2 IP3
}
xyz:53 {
log
errors
cache 30
forward . IP1 IP2 IP3
}
CASE 2:
values.yaml:
dns_domains: abc xyz
dns_servers:
or
dns_domains: abc xyz
I want configmap data something as below for the above values.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: Corefile
data:
abc:53 {
log
errors
cache 30
}
xyz:53 {
log
errors
cache 30
}
I tried something as below and got stucked how to make "forward" line to have all values in the range
{{- range $domain := splitList " " .Values.dns_int_domains }}
$domain:53 {
log
errors
cache 30
{{- range $dns_int_server := splitList " " .Values.dns_int_servers }}
{{- if $dns_int_server }}
forward . $dns_int_server # how to make this line to have all values in dns_int_server list
{{- end }}
}
{{- end }}
Note: we should have only one forward plugin inside a server block. i.e, below is not allowed
abc:53 {
forward . IP1
forward . IP2
}
With the format you currently have, .Values.dns_servers
is already a string with space-separated values, which is the format you want out. You don't need to split it to a list and write it out again.
{{- if .Values.dns_servers }}
forward . {{ .Values.dns_servers }}
{{- end }}
Helm contains (almost all of) the extension functions in the Sprig library, not all of which are in the Helm documentation proper. If you do have this as a list, there is a join
template function that can combine them together.
{{- $dns_servers := splitList " " .Values.dns_servers }}
{{- if $dns_servers }}
forward . {{ join " " $dns_servers }}
{{- end }}
Rather than a space-separated string, you might find it easier to manipulate these values if you use native YAML lists in your values.yaml
file. Any valid YAML list syntax will work here, including formats that put the entire list on one line.
# values.yaml, reformatted to use YAML lists and snakeCase names
dnsDomains: [abc,xyz]
dnsServers:
- 10.10.10.10
- 10.10.10.11
- 10.10.20.20
As one final option, if you're very careful about the whitespace handling, you can put the templating anywhere you want, even in the middle of a line.
{{- with .Values.dnsServers }}
forward .
{{- range . }} {{ . }}{{ end }}
{{- end }}
The important trick with this last example is that the -
whitespace control before range
also consumes the newline at the end of the previous line. Then inside the range
block, repeated once for each element and with no whitespace control, there is a space and a list element. Finally after the very last end
there is a newline.
You can double-check this with helm template
, which will validate the YAML syntax and print out what got rendered (with --debug
it will print it out even if it's invalid YAML).