Search code examples
kuberneteskubernetes-helm

Helm template check if boolean value is actually empty not false


How to tell if a value, more specifically a boolean typed value is empty in a gotpl alias helm template?

How to tell if a simple value is empty?


Solution

  • You have multiple options, how to tell if a normal (not boolean) value is empty.

    A very brute force and quite long way is to put an if statement around your node such as

    {{ if .Values.simpleText }}
    isEmpty: false
    {{ end }}
    

    This will evaulate isEmpty as false only if there is any value inside .Values.simpleText

    A better way to do this is to use the inbuilt empty function:

    isEmpty2: {{ empty .Values.simpleText }}
    

    This does the same as the first example just shorter and arguably more readable

    The issue with theese methods is that if you have a node which is a bool theese methods wont work.

    #values.yaml
    myBool: false
    #-----------------------------------------
    #template.yaml
    isBoolEmpty: {{ empty .Values.myBool }}
    #-----------------------------------------
    #output
    isBoolEmpty: true
    

    This will claim that .Values.myBool is empty even tho it clearly has a value which is false.

    Now there is an option to check if a value is nil

    isNil: {{ eq nil .Values.nilValue }}
    

    This will result as true if the value is indeed nil ( nilValue: ) but as soon as you have a value inside nilValue you will get a type exception.

    The solution that I found and seemed the easiest to understand and to use is:

    {{ if (quote .Values.myBool | empty)  }}
    isActuallyEmpty: ".Values.myBool is empty"
    {{ else }}
    isActuallyEmpty: ".Values.myBool has a false or true value"
    {{ end }}
    

    I am sorry if this is very trivial but I know that I was struggleing with this question for some time.