configMap我有一個模板,可以從jmeter-test-data-file-configmap.yamlhelm 圖表中的檔案創建一個:
{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-testdata
data:
{{ .Values.env.datafile }}: |-
{{ .Files.Get .Values.env.datafile | indent 4}}
{{- end }}
對應的 yaml.configuration 是:
env:
testfile: sample
datafile: example.csv
當我這樣做時helm upgrade,一切都很好,如果 example.csv 是這樣的單行文本檔案:
1,1
但它是否至少有兩條或更多行
1,1
2,2
部署失敗并顯示不相關的錯誤訊息:
upgrade.go:123: [debug] preparing upgrade for jmeter
Error: UPGRADE FAILED: YAML parse error on jmeter/templates/jmeter-test-data-file-configmap.yaml: error converting YAML to JSON: yaml: line 7: did not find expected key
helm.go:81: [debug] error converting YAML to JSON: yaml: line 7: did not find expected key
YAML parse error on jmeter/templates/jmeter-test-data-file-configmap.yaml
除錯細節是:
helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort
/home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:146
helm.sh/helm/v3/pkg/releaseutil.SortManifests
/home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:106
helm.sh/helm/v3/pkg/action.(*Configuration).renderResources
/home/circleci/helm.sh/helm/pkg/action/action.go:165
helm.sh/helm/v3/pkg/action.(*Upgrade).prepareUpgrade
/home/circleci/helm.sh/helm/pkg/action/upgrade.go:215
helm.sh/helm/v3/pkg/action.(*Upgrade).Run
/home/circleci/helm.sh/helm/pkg/action/upgrade.go:124
main.newUpgradeCmd.func2
/home/circleci/helm.sh/helm/cmd/helm/upgrade.go:155
github.com/spf13/cobra.(*Command).execute
/go/pkg/mod/github.com/spf13/[email protected]/command.go:850
github.com/spf13/cobra.(*Command).ExecuteC
/go/pkg/mod/github.com/spf13/[email protected]/command.go:958
github.com/spf13/cobra.(*Command).Execute
/go/pkg/mod/github.com/spf13/[email protected]/command.go:895
main.main
/home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
/usr/local/go/src/runtime/proc.go:204
runtime.goexit
/usr/local/go/src/runtime/asm_amd64.s:1374
UPGRADE FAILED
main.newUpgradeCmd.func2
/home/circleci/helm.sh/helm/cmd/helm/upgrade.go:157
github.com/spf13/cobra.(*Command).execute
/go/pkg/mod/github.com/spf13/[email protected]/command.go:850
github.com/spf13/cobra.(*Command).ExecuteC
/go/pkg/mod/github.com/spf13/[email protected]/command.go:958
github.com/spf13/cobra.(*Command).Execute
/go/pkg/mod/github.com/spf13/[email protected]/command.go:895
main.main
/home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
/usr/local/go/src/runtime/proc.go:204
runtime.goexit
所以,錯誤指向這一行:
{{ .Values.env.datafile }}: |-
這里可能有什么問題,檔案本身如何因為它的新行而損害模板處理?我們累了 /r/n, /n, /r 沒有成功,都一樣
我們還嘗試像這樣對 line:7 進行 hadcode:
{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-testdata
data:
example.csv: |-
{{ .Files.Get .Values.env.datafile | indent 4}}
{{- end }}
仍然是同樣的錯誤。甚至用硬代碼替換模板值:
{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-testdata
data:
{{ (printf "example.csv" ) }}: |-
{{ .Files.Get (printf "example.csv" ) | indent 4}}
{{- end }}
但仍然是同樣的錯誤。
幾乎相同的配置不會發生這種情況,它將多行 xml 檔案以完全相同的方式帶到 configMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-test
data:
test.jmx: |-
{{ .Files.Get (printf "%s.jmx" .Values.env.testfile ) | indent 4}}
我想,由于某種原因,當它應用于 jmx 檔案時,可能并不適用于 csv 檔案的所有行。但是如何弄清楚該廣告如何解決?
升級版:
1, 1
2, 2
3, 3
像上面那樣手動“打算”檔案是成功的,它被部署了,但是這個檔案不能滿足當時的需要。如何避免這種情況?
uj5u.com熱心網友回復:
如果您有一行包含indent它,則可能需要從行首開始,即使它位于其他縮進的背景關系中。
{{- if .Values.env.datafile }}
...
data:
{{ .Values.env.datafile }}: |-
{{ .Files.Get .Values.env.datafile | indent 4}}
{{/*- previous line is not indented */}}
{{- end }}
在您的原始示例中,讓我們關注這兩行:
{{ .Values.env.datafile }}: |-
{{ .Files.Get .Values.env.datafile | indent 4}}
## (these two spaces are important)
現在,如果輸入行是您的第二個示例
1,1
2,2
現在:包含的行indent 4本身縮進了 2 個空格。因此,在該行中,行首有兩個空格,再加上 4 個空格indent 4,所以 6 個空格;然后是換行符和四個空格indent 4,但沒有行首空格(您仍然在輸出中indent),所以只有 4 個空格。
test.jmx: |-
1,1
2,2
如果您helm template --debug在原始圖表上運行,您仍然會收到 YAML 決議錯誤,但它也應該列印出此輸出。
在某些情況下,您可能會發現使用它更美觀一些nindent,它在第一行的縮進之前包含一個換行符,并與-花括號內的 a 組合以消耗前面的空格(包括空格和換行符)。這也應該有效:
data:
{{ .Values.env.datafile }}: |-
{{- .Files.Get .Values.env.datafile | nindent 4}}
{{/*- indented, starts with -, uses nindent */}}
但也例如:
metadata:
labels: {{- include "common.labels" . | nindent 4 }}
annotations: {{- include "common.annotations" . | nindent 4 }}
uj5u.com熱心網友回復:
{{ .Files.Get (printf "%s" .Values.env.datafile ) | nindent 4}}
代替
{{ .Files.Get (printf "%s" .Values.env.datafile ) | indent 4}}
已解決問題。
但對我來說,仍然不清楚為什么 csv 檔案會出現問題,而 XML 檔案卻沒有。都是因為空間嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483214.html
標籤:Kubernetes kubernetes-helm 去模板
