Kubernetes Helm Chart 条件检查

huangapple go评论85阅读模式
英文:

Kubernetes Helm Chart If Condition Check

问题

我正在尝试在Helm chart中添加大于条件,但是出现错误。

我在values.yaml中定义了一个值,并在deployment.yaml中使用该值作为条件。

values.yaml

replicaCount: 2

deployment.yaml

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

我正在使用helm dry run选项来检查结果,但是出现错误:

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

如何修复这个问题?

英文:

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 &quot;hello-world/templates/deployment.yaml&quot;: template: hello-world/templates/deployment.yaml:16:12: executing &quot;hello-world/templates/deployment.yaml&quot; at &lt;gt .Values.replicaCo...&gt;: error calling gt: incompatible types for comparison

how to fix this ?

答案1

得分: 24

尝试在比较中使用浮点数:

deployment.yaml

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

Helm(以及底层的Golang模板和Yaml)有时可能会很奇怪。


此外,请注意有时需要在yaml配置中对值进行类型转换(例如端口号)。

示例:

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

有关Yaml类型转换的更多信息:https://github.com/yaml/YAML2/wiki/Type-casting

英文:

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

huangapple
  • 本文由 发表于 2017年9月2日 10:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/46009933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定