【问题标题】:Helm _helpers.yaml template with bool returning %!S(Bool=True) instead of stringHelm _helpers.yaml 模板,bool 返回 %!S(Bool=True) 而不是字符串
【发布时间】:2021-10-04 18:12:50
【问题描述】:

我有以下 Helm 模板定义:

{{- define "api.test-drive" -}}
{{- if not .Values.global.testDrive }}
{{- printf "%s" .Values.default.TEST_DRIVE | quote -}}
{{- else -}}
{{- printf "%s" .Values.global.testDrive | title | quote -}}
{{- end -}}
{{- end -}}

在 configmap 模板中包含以下内容:

TEST_DRIVE: {{ include "api.test-drive" . }}

还有一个全局值global.testDrive: true。然而,当 Helm 执行这个插入到 configmap 中时,它会将其存储为:

TEST_DRIVE:
----
%!S(Bool=True)

不应该 printf 将 global.testDrive 从 bool true 转换为字符串,然后应用 titlequote 函数吗?目前尚不清楚这里发生了什么。

【问题讨论】:

    标签: kubernetes-helm go-templates


    【解决方案1】:

    Go text/template printf 模板函数直接传递给fmt.Printf(),但fmt 包定义其格式字符串与C 的printf(3) 函数略有不同。特别是,%s 格式修饰符仅为字符串类型的参数定义,并且您已将其传递给布尔类型的参数; %!s(...) 输出表示处理 %s 参数时出错(请参阅 Format errors)。

    如果您想在此处使用printf%v 会将任意值转换为具有默认语法的字符串

    {{- printf "%v" .Values.global.testDrive | title | quote -}}
    {{/*        ^^                                          */}}
    

    Helm 包含a generic toString helper,这在这里可能更方便。

    {{- .Values.global.testDrive | toString | title | quote -}}
    

    (...但是under the hood 在大多数情况下toString 相当于printf "%v"。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 2020-09-11
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多