【问题标题】:How to define a template function to set tolerations either from global or from local tolerations in Helm?如何定义模板函数以在 Helm 中设置全局或局部容差?
【发布时间】:2021-05-27 18:11:56
【问题描述】:

我想检查tolerations 是否在global 中可用,然后将其用于所有 pod(带有子图表的父图表),如果每个子模块/子图表都有自己的 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"

【问题讨论】:

    标签: kubernetes-helm


    【解决方案1】:

    我在编写的代码中看到两个问题。 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 */}}
    

    在辅助函数中,使用toYaml 是合适的(.Values 是复合对象而不是字符串)但indent 行再次需要从第一列开始。

    tolerations:
    {{toYaml .globalTolerations.tolerations | indent 2}}
    

    您可能会发现使用helm template 来调试此类问题很有用;它将写出生成的 YAML 文件,而不是将其发送到集群。问题中的 toYaml 表单可能会将 tolerations: 块转换为 YAML 字符串,这在输出中非常明显。

    【讨论】:

    • 谢谢。通过此设置,我收到此错误: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"
    • 干得好!我只需要将 not empty 添加到 if 语句中:if not (empty .globalTolerations.tolerations) 否则它会返回 nil 作为字符串。谢谢
    猜你喜欢
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2017-09-16
    相关资源
    最近更新 更多