Helm模板检查布尔值

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

Helm Template check boolean value

问题

我正在尝试检查一个值是否为真,但是无论我尝试哪种变化,都会显示下面的错误:

  1. _helpers.tpl:96:19: 在类型为字符串的字段Values中无法评估字段Values

我的_helpers.tpl文件的一部分:

  1. {{/*
  2. 获取mongodb连接字符串
  3. */}}
  4. {{- define "mongo_databasename" -}}
  5. {{- if eq (.Values.mongo.enabled | toString) "true" }}
  6. {{- .Values.mongo.databaseName }}
  7. {{- else }}
  8. {{- .Values.environmentVars.mongo.databaseName }}
  9. {{- end -}}
  10. {{- end -}}

我的values.yaml文件的相应部分:

  1. mongo:
  2. enabled:
  3. username: user

我尝试了很多种方式来检查if条件,像上面这样的方式:

  1. {{- 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:

  1. _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:

  1. {{/*
  2. Get mongodb connection string
  3. */}}
  4. {{- define "mongo_databasename" -}}
  5. {{- if eq (.Values.mongo.enabled | toString) "true" }}
  6. {{- .Values.mongo.databaseName }}
  7. {{- else }}
  8. {{- .Values.environmentVars.mongo.databaseName }}
  9. {{- end -}}
  10. {{- end -}}

The respective part of my values.yaml file:

  1. mongo:
  2. enabled:
  3. username: user

I've tried many ways to check the if condition, like the one above:

  1. {{- if .Values.mongo.enabled }}

答案1

得分: 3

可能是由于include的范围引起的。

模板看起来没问题。

我尝试了以下操作

_helper.tpl

  1. {{/*
  2. 获取mongodb连接字符串
  3. */}}
  4. {{- define "mongo_databasename" -}}
  5. {{- if .Values.mongo.enabled }}
  6. {{- .Values.mongo.databaseName }}
  7. {{- else }}
  8. {{- .Values.environmentVars.mongo.databaseName }}
  9. {{- end -}}
  10. {{- end -}}

values.yaml

  1. mongo:
  2. enabled:
  3. username: user
  4. databaseName: test
  5. environmentVars:
  6. mongo:
  7. databaseName: envvar

templates/configmap.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: {{ include "test.fullname" . }}
  5. data:
  6. test: {{- include "mongo_databasename" .}}
  7. test2:
  8. {{- with .Values.mongo }}
  9. {{- include "mongo_databasename" $ }}
  10. {{- end }}

输出

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: test-v7
  5. data:
  6. test:envvar
  7. 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

  1. {{/*
  2. Get mongodb connection string
  3. */}}
  4. {{- define "mongo_databasename" -}}
  5. {{- if .Values.mongo.enabled }}
  6. {{- .Values.mongo.databaseName }}
  7. {{- else }}
  8. {{- .Values.environmentVars.mongo.databaseName }}
  9. {{- end -}}
  10. {{- end -}}

values.yaml

  1. mongo:
  2. enabled:
  3. username: user
  4. databaseName: test
  5. environmentVars:
  6. mongo:
  7. databaseName: envvar

templates/configmap.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: {{ include "test.fullname" . }}
  5. data:
  6. test: {{- include "mongo_databasename" .}}
  7. test2:
  8. {{- with .Values.mongo }}
  9. {{- include "mongo_databasename" $ }}
  10. {{- end }}

output

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: test-v7
  5. data:
  6. test:envvar
  7. 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:

确定