Search code examples
gokuberneteskubernetes-helmsprig-template-functions

Helm sprig template pipe and/or negatate


I'm trying to create my config map in helm using the following code:

 api: "{{ .Values.global.api }}",
 demo: {{ .Values.global.api | contains "demo" }},
 other: ....,

I would like to set other as true if Values.global.api NOT contains "demo" and "prod". Something like

other: {{ .Values.global.api | NOT (contains "demo" AND contains "prod") }},

Solution

  • In the Go text/template language, pipelines and function calls are equivalent and you can freely substitute in either direction

    {{ x | f a b }}
    {{ f a b (x) }}
    

    You can't distribute a parameter across multiple functions as you're suggesting here; you need to repeat it (or put it into a local variable). Looking through the catalog of Sprig functions, I'm not sure there's any specific function that matches your needs that would make this simpler. You have to spell it out:

    demo: {{ not (and (contains "demo" .Values.global.api) (contains "prod" .Values.global.api)) }}
    

    As I parenthetically suggested, you can use a local variable to shorten this a little:

    {{- $api := .Values.global.api }}
    api: {{ quote $api }}
    demo: {{ not (and (contains "demo" $api) (contains "prod" $api)) }}