Answers: <p>This wouldn't work. The second condition will be evaluated regardless of the first. So if the key is not defined there will be an error.
  Use <code>default</code> instead:</p>
  <pre><code>{{- if eq (default .Values.enableJsConfigMap false) true }}
  # Do something if enableJsConfigMap exists and its value is true
  {{- end }}
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-10-17T16:41:45.717Z
Answers: 2023-10-17T16:41:45.717Z
Answers: user42196
Answers: <p>Th best construction i use:</p>
  <pre><code>{{- if (.Values.enableJsConfigMap).enabled }}
  {{- /*do something*/ }}
  {{- end }}
  </code></pre>
  <p>Also you can make any number of childs like this:</p>
  <pre><code>{{- if (((((((.Values.a).b).c).d).e).f).g).k }}
  </code></pre>
  <hr />
  <p>I have no idea why is it working.<br />
  Looks like Helm will try to check brackets from inner to outer.<br />
  If some thing will be absent, it will return nill to &quot;if&quot;.
  I assume, it's memory consumption operation, because helm can create new objects instead of allocators here - i'm not sure.<br />
  But i never met a problem with this.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-12-25T13:51:44.453Z
Answers: 2024-12-25T13:52:11.780Z
Answers: 2024-12-25T13:52:11.780Z
Answers: Zerginwan
Answers: <p>Use <code>dig</code>. <code>dig</code> is the helm equivalent of <code>bar?.foo</code>, in that it doesn't error if bar doesn't have a foo. <a href="https://helm.sh/docs/chart_template_guide/function_list/#dig" rel="nofollow noreferrer">https://helm.sh/docs/chart_template_guide/function_list/#dig</a></p>
  <p>The least unreadable syntax of dig uses pipelining:
  <code>dictionary | dig &quot;key&quot; &quot;childDictKey&quot; &quot;defaultValue&quot;</code>. You can have any number of childDictKeys, including none.</p>
  <pre class="lang-none prettyprint-override"><code>{{- if eq (.Values | dig &quot;enableJsConfigMap&quot; &quot;enabled&quot; &quot;false&quot;) &quot;true&quot; }}
  {{- /*do something*/ }}
  {{- end }}
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-10-30T10:37:41.163Z
Answers: 2024-10-30T10:54:03.060Z
Answers: 2024-10-30T10:54:03.060Z
Answers: Syfer Polski
Body: <p>&quot;helm check if key exist and then evaluate if it's value is true on single line condition&quot;
  In Helm version 3.10 will the following condition below work as intended or I can achieve it in some other way?</p>
  <pre><code>{{- if and (hasKey .Values.enableJsConfigMap) (eq .Values.enableJsConfigMap.enabled true) }}
  </code></pre>
  
Tags: <helm><kubernetes-helm>
Title: helm check if key exist and then evaluate if it's value is true on single line condition
UserDisplayName: anVzdGFub3RoZXJodW1hbg
ThreadText: # helm check if key exist and then evaluate if it's value is true on single line condition
  
  (Asked by: username_0 on 2023-07-07)
  
  "helm check if key exist and then evaluate if it's value is true on single line condition"
  In Helm version 3.10 will the following condition below work as intended or I can achieve it in some other way?
  
  ```
  {{- if and (hasKey .Values.enableJsConfigMap) (eq .Values.enableJsConfigMap.enabled true) }}
  
  ```
  
  **Tags:** helm, kubernetes-helm
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2023-10-17. Score: 3
  
  This wouldn't work. The second condition will be evaluated regardless of the first. So if the key is not defined there will be an error.
  Use `default` instead:
  
  ```
  {{- if eq (default .Values.enableJsConfigMap false) true }}
  # Do something if enableJsConfigMap exists and its value is true
  {{- end }}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2024-12-25. Score: 0
  
  Th best construction i use:
  
  ```
  {{- if (.Values.enableJsConfigMap).enabled }}
  {{- /*do something*/ }}
  {{- end }}
  
  ```
  
  Also you can make any number of childs like this:
  
  ```
  {{- if (((((((.Values.a).b).c).d).e).f).g).k }}
  
  ```
  
  ---
  
  I have no idea why is it working.  
  Looks like Helm will try to check brackets from inner to outer.  
  If some thing will be absent, it will return nill to "if".
  I assume, it's memory consumption operation, because helm can create new objects instead of allocators here - i'm not sure.  
  But i never met a problem with this.
  
  
  ---
  
  ### Answer by username_3 on 2024-10-30. Score: 0
  
  Use `dig`. `dig` is the helm equivalent of `bar?.foo`, in that it doesn't error if bar doesn't have a foo. <https://helm.sh/docs/chart_template_guide/function_list/#dig>
  
  The least unreadable syntax of dig uses pipelining:
  `dictionary | dig "key" "childDictKey" "defaultValue"`. You can have any number of childDictKeys, including none.
  
  ```none
  {{- if eq (.Values | dig "enableJsConfigMap" "enabled" "false") "true" }}
  {{- /*do something*/ }}
  {{- end }}
  
  ```
  
  
  ---
  
  