How to use if condition in helm chart

5/13/2020

I have below values in values.yaml

  pg_hba:
    - hostssl all all 0.0.0.0/0 md5
    - host    all all 0.0.0.0/0 md5

The reqiurement is to check the hostssl line exists , if yes it should go into the if loop and do something.

i tried to use {{ if has "hostssl" .Values.pg_hba }} but it seraches only for the exact string "hostssll" and not the entire line.

Please help on how i can check for the entrire line in if condition.

-- Neelam Sharma
go-templates
kubernetes-helm

1 Answer

5/13/2020

I have not fully understood your question, so here are 3 options.

To check if two string are equal, Go has built in template function eq, here is use example:

{{ if eq "line" "line" }}
> true

If you want to check if line contains hostssl string. Helm has sprig as it's dependency -- it's module that provides additional template functions. One of these functions is contains, that checks if string is contained inside another:

{{ if contains "cat" "catch" }}
> true

If you want to check, if string has hostssl precisely at it's start you can use another function, provided by sprig -- hasPrefix:

{{ if hasPrefix "cat" "catch" }}
> true

Here is a list of all string functions that sprig offers. If none of above options does satisfy your requirement, you can use regex function for matching.

-- Grigoriy Mikhalkin
Source: StackOverflow