Search code examples
gotemplateskuberneteskubernetes-helmhelm3

Helm Template check boolean value


I'm trying to check if a value is true or not, but for every variation that I try, the error below is always shown:

_helpers.tpl:96:19: executing "mongo_databasename" at <.Values.mongo.enabled>: can't evaluate field Values in type string

Part of my _helpers.tpl file:

{{/*
Get mongodb connection string
*/}}
{{- define "mongo_databasename" -}}
{{-  if eq (.Values.mongo.enabled | toString) "true" }}
{{-  .Values.mongo.databaseName }}
{{- else }}
{{-  .Values.environmentVars.mongo.databaseName }}
{{- end -}}
{{- end -}}

The respective part of my values.yaml file:

mongo:
  enabled:
  username: user

I've tried many ways to check the if condition, like the one above:

{{-  if .Values.mongo.enabled  }}

Solution

  • It may be caused by the scope of include.

    The template looks okay.

    I tried as follows

    _helper.tpl

    {{/*
    Get mongodb connection string
    */}}
    {{- define "mongo_databasename" -}}
    {{- if .Values.mongo.enabled }}
    {{- .Values.mongo.databaseName }}
    {{- else }}
    {{- .Values.environmentVars.mongo.databaseName }}
    {{- end -}}
    {{- end -}}
    

    values.yaml

    mongo:
      enabled:
      username: user
      databaseName: test
    
    environmentVars:
      mongo:
        databaseName: envvar
    

    templates/configmap.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ include "test.fullname" . }}
    data:
      test: {{- include "mongo_databasename" .}}
      test2:
        {{- with .Values.mongo }}
        {{- include "mongo_databasename" $ }}
        {{- end }}
    

    output

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: test-v7
    data:
      test:envvar
      test2:envvar
    

    Pay attention to the scope (. | $) passed in at the end of the templates/configmap.yaml include statement.