Home > Mobile >  Unable to add if condition to the helm yaml values.yam;
Unable to add if condition to the helm yaml values.yam;

Time:05-05

Hi Helm and Yaml Experts,

I am a beginner with Helm/YAML. Is it possible to add if-else conditions in the values.yaml file in helm charts?

I need to check a variable and set some attribute values accordingly. Here is a snippet example YAML which I am trying to work on the www.yamllint.com currently.

---
cp-kafka: 
  brokers: 3
  enabled: true
  heapOptions: "-Xms512M -Xmx512M"
  image: confluentinc/cp-enterprise-kafka
  imagePullSecrets: ~
  imageTag: "6.1.0"
  persistence: 
    disksPerBroker: 1
    enabled: true
    size: 5Gi
  resources: {}
  securityContext: 
    runAsUser: 0
  configurationOverrides: 
    {{- if .Values.prometheus.jmx.enabled }}
    - 
      containerPort: 10
      name: jmx
    {{- else }}
      containerPort: 11
      name: jmx
    {{- end }}

I am getting the following error on line 17 - if condition. Tried various variants like

{{ if .Values.prometheus.jmx.enabled }}, only if and end, indentations. Nothing helped so far.

(): did not find expected node content while parsing a flow node at line 17 column 7

Can someone please help here? Please let me know if you need any further information from my end to answer this.

Thanks !

CodePudding user response:

Is it possible to add if-else conditions in the values.yaml file in helm charts?

No.

It's possible the chart could support using the template language in limited contexts: if the chart code uses the tpl function to process a specific value, then the value's text can include template syntax, but it must be properly quoted. Most values in most contexts in most charts don't allow this.

Since you as the operator control the content of the values.yaml file, you can just change this setting in the file you're passing to helm install -f. It's fine to have a separate values file per environment.

If you're actually authoring the chart, I'd suggest having a higher-level values file, rather than writing out a list of individual Kubernetes object settings. If you could write within the chart:

containerPorts:
{{- with .Values.prometheus.jmx | default dict }}
{{- if .enabled }}
  - containerPort: {{ .port | default 9875 }}
    name: imx
{{- end }}
{{- end }}

and then configure

prometheus:
  jmx:
    enabled: true
    port: 11
  • Related