是否可以在模板中不使用所需的.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
...
是的,有可能。让我们考虑下面的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.
可能最直接的方法是使用sprig的fail
函数。
fail
将{{- if not .Values.mongodb.mongodbPassword -}}
{{- fail "You must set a mongodb password" -}}
{{- end -}}
表达式分配给一个变量(您从未使用过)可能也会产生预期的效果。
required