英文:
Helm Template check boolean value
问题
我正在尝试检查一个值是否为真,但是无论我尝试哪种变化,都会显示下面的错误:
_helpers.tpl:96:19: 在类型为字符串的字段Values中无法评估字段Values
我的_helpers.tpl文件的一部分:
{{/*
获取mongodb连接字符串
*/}}
{{- define "mongo_databasename" -}}
{{- if eq (.Values.mongo.enabled | toString) "true" }}
{{- .Values.mongo.databaseName }}
{{- else }}
{{- .Values.environmentVars.mongo.databaseName }}
{{- end -}}
{{- end -}}
我的values.yaml文件的相应部分:
mongo:
enabled:
username: user
我尝试了很多种方式来检查if条件,像上面这样的方式:
{{- if .Values.mongo.enabled }}
英文:
I'm trying to check if a value is true or not, but for every variation that I try, the error below is always shown:
_helpers.tpl:96:19: executing "mongo_databasename" at <.Values.mongo.enabled>: can't evaluate field Values in type string
Part of my _helpers.tpl file:
{{/*
Get mongodb connection string
*/}}
{{- define "mongo_databasename" -}}
{{- if eq (.Values.mongo.enabled | toString) "true" }}
{{- .Values.mongo.databaseName }}
{{- else }}
{{- .Values.environmentVars.mongo.databaseName }}
{{- end -}}
{{- end -}}
The respective part of my values.yaml file:
mongo:
enabled:
username: user
I've tried many ways to check the if condition, like the one above:
{{- if .Values.mongo.enabled }}
答案1
得分: 3
可能是由于include的范围引起的。
模板看起来没问题。
我尝试了以下操作
_helper.tpl
{{/*
获取mongodb连接字符串
*/}}
{{- define "mongo_databasename" -}}
{{- if .Values.mongo.enabled }}
{{- .Values.mongo.databaseName }}
{{- else }}
{{- .Values.environmentVars.mongo.databaseName }}
{{- end -}}
{{- end -}}
values.yaml
mongo:
enabled:
username: user
databaseName: test
environmentVars:
mongo:
databaseName: envvar
templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
data:
test: {{- include "mongo_databasename" .}}
test2:
{{- with .Values.mongo }}
{{- include "mongo_databasename" $ }}
{{- end }}
输出
apiVersion: v1
kind: ConfigMap
metadata:
name: test-v7
data:
test:envvar
test2:envvar
请注意,在templates/configmap.yaml include
语句的末尾传入的范围(. | $
)。
英文:
It may be caused by the scope of include.
The template looks okay.
I tried as follows
_helper.tpl
{{/*
Get mongodb connection string
*/}}
{{- define "mongo_databasename" -}}
{{- if .Values.mongo.enabled }}
{{- .Values.mongo.databaseName }}
{{- else }}
{{- .Values.environmentVars.mongo.databaseName }}
{{- end -}}
{{- end -}}
values.yaml
mongo:
enabled:
username: user
databaseName: test
environmentVars:
mongo:
databaseName: envvar
templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
data:
test: {{- include "mongo_databasename" .}}
test2:
{{- with .Values.mongo }}
{{- include "mongo_databasename" $ }}
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test-v7
data:
test:envvar
test2:envvar
Pay attention to the scope (. | $
) passed in at the end of the templates/configmap.yaml include
statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论