Helm yaml keys from values.yaml

5/26/2018

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
go-templates
kubernetes-helm
yaml

1 Answer

5/26/2018

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
Source: StackOverflow