Helm需要值而不使用它

问题描述 投票:0回答:2

是否可以在模板中不使用所需的.Value

例如,在我的情况下,我想要求为mongodb的一个子图写一个密码,但是我不会在模板上使用它,所以我可以在模板中使用类似于波纹管的东西吗:

{{- required 'You must set a mongodb password' .Values.mongodb.mongodbPassword | noPrint -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cloud.fullname" . }}
  labels:
    {{- include "cloud.labels" . | nindent 4 }}
    app.kubernetes.io/component: cloud
spec:
  replicas: {{ .Values.cloud.minReplicaCount }}
  selector:
....

结果将是这样的:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blablablabla
...
mongodb templates kubernetes-helm
2个回答
0
投票

是的,有可能。让我们考虑下面的Values.yaml文件:

Values.yaml:

mongodb:
  mongodbPassword: "AbDEX***"

因此,仅在设置密码后,您才想生成部署文件。您可以使用模板if-block进行操作。如果密码字段的长度大于零,那么将生成部署yaml,否则不会生成。

{{- if  .Values.mongodb.mongodbPassword}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cloud.fullname" . }}
  labels:
    {{- include "cloud.labels" . | nindent 4 }}
    app.kubernetes.io/component: cloud
spec:
  replicas: {{ .Values.cloud.minReplicaCount }}
  selector:
....
{{- end }}

参考:

{{if pipeline}} T1 {{end}}
    If the value of the pipeline is empty, no output is generated;
    otherwise, T1 is executed. The empty values are false, 0, any nil pointer or
    interface value, and any array, slice, map, or string of length zero.
    Dot is unaffected.

0
投票

可能最直接的方法是使用sprig的fail函数。

fail

{{- if not .Values.mongodb.mongodbPassword -}} {{- fail "You must set a mongodb password" -}} {{- end -}} 表达式分配给一个变量(您从未使用过)可能也会产生预期的效果。

required
© www.soinside.com 2019 - 2024. All rights reserved.