kubernetes-helmgo-templatessprig-template-functions

Helm template condition check for string does not work as expected



  WARNING: jp-osa do not have regional COS endpoint support, it is recommended to use Cross-Regional Storageclass. 

And the code snippet I have in NOTES.txt for this helm chart is:

 {{- if not (contains  $.Values.region "storage-plugin.regionEP") }}
  WARNING: {{ $.Values.region }} do not have regional COS endpoint support, it is recommended to use Cross-Regional Storageclass.

  {{- end }


When I print storage-plugin.regionEP I see .au-syd.br-sao.ca-tor.eu-de.eu-fr2.eu-gb.jp-osa.jp-tok.us-east.us-south

and the region values is jp-osa.

If condition should ideally not get executed. Any idea why this is not working as expected.


Solution

  • The way you have it set up, it is looking for the string "jp-osa" inside the literal string "storage-plugin.regionEP", not a variable or template. Since it does not match, the if statement is always true.

    I'm guessing you have a template in _helpers.tpl called storage-plugin.regionEP, which you are trying to call. In this case, you need to use the following:

     {{- if not (contains  $.Values.region (include "storage-plugin.regionEP" $)) }}
      WARNING: {{ $.Values.region }} do not have regional COS endpoint support, it is recommended to use Cross-Regional Storageclass.
    
      {{- end }
    

    The (include "storage-plugin.regionEP" $) should return the string .au-syd.br-sao.ca-tor.eu-de.eu-fr2.eu-gb.jp-osa.jp-tok.us-east.us-south. The contains will then return true and the if statement will be rendered false.