12

I am trying to add if great than condition in Helm chart. it is throwing error.

I have defined value in values.yaml and using that value in deployment.yaml for condition.

values.yaml

replicaCount: 2

deployment.yaml

rollingUpdate:
  maxSurge: 1
  {{ if gt .Values.replicaCount 2}}
  maxUnavailable: 0
  {{ else }}
  maxUnavailable: 1
  {{ end }}

I am using helm dry run option to check result. getting error

Error: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:16:12: executing "hello-world/templates/deployment.yaml" at <gt .Values.replicaCo...>: error calling gt: incompatible types for comparison

how to fix this ?

Gnana
  • 2,130
  • 5
  • 26
  • 57

1 Answers1

24

Try using float number in comparison instead:

deployment.yaml

rollingUpdate:
  maxSurge: 1
  {{ if gt .Values.replicaCount 2.0}}
  maxUnavailable: 0
  {{ else }}
  maxUnavailable: 1
  {{ end }}

Helm (along with underlying Golang templates and Yaml) can be weird sometimes.


Also, note that sometimes you need to typecast values in your yaml configs (e.g. port numbers).

Example:

...
ports:
- containerPort: !!int {{ .Values.containers.app.port }}
...

More about Yaml type casting: https://github.com/yaml/YAML2/wiki/Type-casting

hypnoglow
  • 1,483
  • 14
  • 16
  • 1
    I am trying with type casting option . it is not working. it is throwing error.{{- if gt !!int {{ .Values.replicaCount }} 1}}. how to type cast ? – Gnana Sep 04 '17 at 19:23
  • 2
    You can also try typecasting not by yaml but by Go templates: `containerPort: {{ .Values.replicaCount | int }}` – hypnoglow Jul 05 '20 at 20:51
  • 1
    What is the "-" is {{- if .Values.enable}} .. .. {{- end }} referred as? – Pasha Mar 22 '21 at 15:40
  • 1
    @Pasha it is about controlling whitespace. Please see: https://helm.sh/docs/chart_template_guide/control_structures/#controlling-whitespace – hypnoglow Mar 23 '21 at 19:33