英文:
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 "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 ?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论