Helm模板检查布尔值

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

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.

huangapple
  • 本文由 发表于 2021年12月30日 09:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/70526582.html
匿名

发表评论

匿名网友

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

确定