我想检查tolerations在global中是否可用,然后对所有的豆荚(带有子图表的父图表)使用它,如果每个子模块/子图表都有自己的tolerations,那么使用它而不是全局的。所以我就是这么定义它的:
{{/* Set tolerations from global if available and if not set it from each module */}}
{{- define "helper.tolerations" }}
{{- if .globalTolerations.tolerations }}
tolerations:
{{toYaml .globalTolerations.tolerations | indent 2}}
{{- else if (.localTolerations.tolerations) }}
tolerations:
{{toYaml .localTolerations.tolerations | indent 2}}
{{- end }}
{{- end }}在每个子图中,我都有:
spec:
template:
spec:
containers:
- name: my-container-name
{{toYaml (include "helper.tolerations" (dict "globalTolerations" .Values.global "localTolerations" (index .Values "ui-log-collector"))) | indent 6}}values.yaml中的默认值定义如下:
global:
tolerations: []
ui-log-collector:
image: a.b.c
tolerations: []
some-other-sub-charts:
image: x.y.z
tolerations: []现在,当我想使用helm部署堆栈时,我传递一个values.yaml来覆盖tolerations,如下所示:
global:
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoSchedule"
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
ui-log-collector:
tolerations:
- key: "key2"
operator: "Equal"
value: "value2"
effect: "NoSchedule"
- key: "key2"
operator: "Equal"
value: "value2"
effect: "NoExecute"有了这个设置,我现在得到了这个错误:
error converting YAML to JSON: yaml: line 34: could not find expected ':'我尝试了不同的东西,但我没有toYaml,我得到:
Error: UPGRADE FAILED: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.tolerations): invalid type for io.k8s.api.core.v1.PodSpec.tolerations: got "string", expected "array"发布于 2021-05-27 10:11:56
我在代码中看到了两个问题。Go模板生成文本输出,因此不需要对它们调用toYaml。另外,当您调用indent时,它不知道当前行的缩进,所以如果要使用indent,{{ ... }}模板表达式通常需要在未缩进的行上。
例如,调用本身应该如下所示:
spec:
template:
spec:
containers:
- name: my-container-name
{{ include "helper.tolerations" (dict "globalTolerations" .Values.global "localTolerations" .Values.ui-log-collector) | indent 6}}
{{-/* at the first column; without toYaml */}}在helper函数中,使用toYaml是合适的(.Values是一个复合对象,而不是字符串),但是indent行再次需要从第一列开始。
tolerations:
{{toYaml .globalTolerations.tolerations | indent 2}}您可能会发现使用helm template来调试这样的问题很有用;它将写出生成的YAML文件,而不是将其发送到集群。问题中的toYaml表单可能会将tolerations:块转换为YAML字符串,这在输出中非常明显。
https://stackoverflow.com/questions/67716373
复制相似问题