-1

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

Jan
  • 12,992
  • 9
  • 53
  • 89
  • What happens when you try? – Jonathan Hall May 26 '18 at 17:54
  • But to answer your question: The template is just treated as standard text. Keys and values are not special, to the templating engine, so anything can be dynamic. – Jonathan Hall May 26 '18 at 17:54
  • @Flimzy. So it should no problem to parse any text? I added now the error in the question: `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` – Jan May 26 '18 at 18:13
  • It's no problem for the templating engine to parse any text. Helm still expects you to produce valid YAML, though (that error says you are not doing so). – Jonathan Hall May 26 '18 at 18:14
  • I found the solution: it was the leading `-` which truncated the intedation :-) `{{ .Values.failoverip1 | indent 2 -}}` – Jan May 26 '18 at 18:20

1 Answers1

1

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
Jan
  • 12,992
  • 9
  • 53
  • 89