Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Helm Chart If Condition Check

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 ?

like image 654
Gnana Avatar asked Sep 02 '17 02:09

Gnana


People also ask

What does {{ }} mean in Helm?

The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

What is $_ in Helm?

The $_ is used to suppress undesired output as "set" returns the new dictionary.

Is Helm being deprecated?

helm/charts has been deprecated and will be obsolete by Nov 13 2020. For this reason, the datawire team as retaken ownership of this chart. The Ambassador Chart is now hosted at datawire/ambassador-chart.


1 Answers

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

like image 63
hypnoglow Avatar answered Sep 19 '22 21:09

hypnoglow