How to use if condition in helm chart

16,359

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.

Share:
16,359
Neelam Sharma
Author by

Neelam Sharma

Updated on June 30, 2022

Comments

  • Neelam Sharma
    Neelam Sharma almost 2 years

    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.

    • Grigoriy Mikhalkin
      Grigoriy Mikhalkin almost 4 years
      What you mean by "hostssl line exists"? Do you want to check if line contains hostssl?
    • Neelam Sharma
      Neelam Sharma almost 4 years
      Hi Grigoriy, ii want to check if the "hostssl all all 0.0.0.0/0 md5" contains /exits in if condition Please note the values after hostall may chnage as per the requirement.
  • Neelam Sharma
    Neelam Sharma almost 4 years
    Below is the content in values.yaml , spec: patroni: pg_hba: - hostssl all all 0.0.0.0/0 md5 - host all all 0.0.0.0/0 md5 i tried as below , {{ if hasPrefix "hostssl" .Values.spec.patroni.pg_hba } But i see below error: template: postgres-operator-cluster/templates/pkicertificate.yaml:1:33‌​: executing "postgres-operator-cluster/templates/pkicertificate.yaml" at <.Values.spec.patroni.pg_hba>: wrong type for value; expected string; got []interface {}
  • Grigoriy Mikhalkin
    Grigoriy Mikhalkin almost 4 years
    @NeelamSharma .Values.spec.patroni.pg_hba is array. You need to iterate over it and apply hasPrefix to each entry. Also, you need to convert interface to string(or quote values in values.yaml). There's plenty of answers on how you can do that on SO. Use search
  • Neelam Sharma
    Neelam Sharma almost 4 years
    I have defined below function to iterate over the range and use if condition: {{- define "sslmode" }} {{- range $k, $v := $.Values.spec.patroni }} {{ index $k | trim | -}}: {{ if hasPrefix "hostssl" $k }} {{- $v | toYaml | trimSuffix "\n"| nindent 2 -}} {{- end }} {{- end }} {{- end }} But the O/P returned is as : pg_hba: Please let me know what I'm missing
  • Grigoriy Mikhalkin
    Grigoriy Mikhalkin almost 4 years
    You iterate over Values.spec.patroni, but need to iterate over Values.spec.patroni.pg_hba
  • Neelam Sharma
    Neelam Sharma almost 4 years
    i tried tht option too but it gives the same error as: executing "postgres-operator-cluster/templates/deployment.yaml" at <include "sslmode" .>: error calling include: template: postgres-operator-cluster/templates/_helpers.tpl:61:14: executing "sslmode" at <trim>: wrong type for value; expected string; got int
  • Grigoriy Mikhalkin
    Grigoriy Mikhalkin almost 4 years
    @NeelamSharma {{ index $k | trim | -}} -- you're trimming index, not value. Instead try {{ index $v | trim | -}}. But still, it probably won't work, because you have interface values, so, first you need to convert them to string.