error when installing helm chart whith conditions

3/3/2020

I need to deploy three different certificates on different namespaces using helm chart. I create a template per certificate in the same file and add if conditions on each one in order to deloy only the needed certificate that i pass as a paramater in my helm install command, My secret.yaml look like this :

{{- if eq .Values.val "paris_turf_support" }}
{{- range .Values.namespaces.paris_turf_support }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
name: "tls-paris-turf.support"
namespace: {{ $ns }}
data:
    tls.crt: {{ $.Files.Get "tls-paris-turf.support.crt" | b64enc }}
    tls.key: {{ $.Files.Get "tls-paris-turf.support.key" | b64enc }}
{{- end }}

{{ else if eq .Values.val "geny_sports" }}
{{- range .Values.namespaces.geny_sports }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
    name: "tls-geny-sports.com"
    namespace: {{ $ns }}
data:
    tls.crt: {{ $.Files.Get "tls-geny-sports.com.crt" | b64enc }}
    tls.key: {{ $.Files.Get "tls-geny-sports.com.key" | b64enc }}
{{- end }}

{{ else if eq .Values.val "paris_turf_com" }}
{{- range .Values.namespaces.paris_turf_com }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
    name: "tls-paris-turf.com"
    namespace: {{ $ns }}
data:
    tls.crt: {{ $.Files.Get "tls-paris-turf.com.crt" | b64enc }}
    tls.key: {{ $.Files.Get "tls-paris-turf.com.key" | b64enc }}
{{- end }}
{{- end }}

when i run this command to install the helm chart : helm install secret-rel ./secret --values=./secret/values/dev.yaml --namespace=secret --set val="paris_turf_com"

I get this error : Error: YAML parse error on secret/templates/secret.yaml: error converting YAML to JSON: yaml: line 9: mapping values are not allowed in this context

Need your help please

-- ahmed khalil jerbi
kubernetes
kubernetes-helm

2 Answers

3/6/2020

mapping values are not allowed in this context means that there is an error in .yaml which makes it invalid.

There are plenty of online tools that can be used to validate yaml's syntax such as YAML Lint.

In your particular use case the error says that there is an issue with line 9. Looking at your config we can see that you rae missing indentations in lines 9 and 10. It should look like this instead:

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
  name: tls-paris-turf.support
  namespace: {{ $ns }}

Also you don't need to use double quotes (" ") for naming your Secrets. And as you already noticed, you should use --- a line before {{- end }}

I hope it helps.

-- OhHiMark
Source: StackOverflow

3/3/2020

finally i fix the problem, this is my secret.yaml :

{{- if eq .Values.val "paris_turf_support" }}
{{- range .Values.namespaces.paris_turf_support }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
  name: tls-paris-turf.support
  namespace: {{ $ns }}
data:
  tls.crt: {{ $.Files.Get "tls-paris-turf.support.crt" | b64enc }}
  tls.key: {{ $.Files.Get "tls-paris-turf.support.key" | b64enc }}
---
{{- end }}
{{ else if eq .Values.val "geny_sports" }}
{{- range .Values.namespaces.geny_sports }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
  name: tls-geny-sports.com
  namespace: {{ $ns }}
data:
  tls.crt: {{ $.Files.Get "tls-geny-sports.com.crt" | b64enc }}
  tls.key: {{ $.Files.Get "tls-geny-sports.com.key" | b64enc }}
---
{{- end }}
{{ else if eq .Values.val "paris_turf_com" }}
{{- range .Values.namespaces.paris_turf_com }}
{{- $ns := . -}}

apiVersion: v1
kind: Secret
type: kubernetes.io/tls
metadata:
  name: tls-paris-turf.com
  namespace: {{ $ns }}
data:
  tls.crt: {{ $.Files.Get "tls-paris-turf.com.crt" | b64enc }}
  tls.key: {{ $.Files.Get "tls-paris-turf.com.key" | b64enc }}
---
{{- end }}
{{- end }}
-- ahmed khalil jerbi
Source: StackOverflow