Helm _helpers.yaml 模板返回 %!S(Bool=True) 而不是字符串

Helm _helpers.yaml template with bool returning %!S(Bool=True) instead of string

我有以下 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 函数吗?不清楚这里发生了什么。

围棋 text/template printf template function passes through directly to fmt.Printf(), but the fmt package defines its format strings a little bit differently from C's printf(3) function. In particular, the %s format modifier is only defined for string-type arguments, and you've passed it a bool-type argument; the %!s(...) output means there was an error processing a %s argument (see 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"。)