我正在撰寫 Helm _helpers.tpl 檔案。這個助手需要
- 從不在圖表的 yaml/值中的檔案中讀取 JSON 值。
- 使用charts/values/yaml中的變數來決定讀取外部JSON的哪個欄位
- 將從 JSON 中提取的值存盤到本地 Go 變數中
- 將 Go 變數和圖表變數的值組合成最終值。
我的外部 JSON 檔案如下所示:
{
"java": {
"8": {
"version": "0.1.8"
},
"11": {
"version": "0.1.11"
}
},
"node": {
"14": {
"version": "14.5.0"
},
"16": {
"version": "16.4.0"
}
}
}
我的值/圖表中有以下變數可供我使用
- .Values.type
- .Values.typeVersion
我的 _helpers.tpl 看起來像這樣:
{{- $imageversions := (.Files.Get "../../../../common/versions.json" | toJson | jq ".".Values.type".".Values.typeVersion"."version) -}}
{{- printf "artifactory.myco.com/docker/%s/ubuntu20-slim-%s%s.0f:%s" .Values.type .Values.type .Values.typeVersion $imageversions }}
這段代碼的第一行(上面)是我需要幫助的地方。目前,我
- 用于
.Files.Get提取檔案內容 - 通過使用確保將其解釋為 JSON
toJson - 嘗試閱讀我感興趣的特定領域
jq - 將區域變數
$imageversions(最左邊)分配給 JSON 中找到的值
我想我一切都好,除了我沒有jq在這臺電腦上。如何決議 JSON 并在這個 Helm Go 模板助手中獲取我需要的值?
uj5u.com熱心網友回復:
一旦您呼叫.Files.Get檢索檔案然后呼叫fromJson決議它,您就可以使用普通的 Helm 模板語法在其中導航。您可能會發現標準index函式在這里很有用:它接受一個物件和任意數量的下標,并依次對每個下標進行映射或陣列查找。
{{-/* Get the contents of the file */-}}
{{- $versions := .Files.Get "common/versions.json" | fromJson -}}
{{-/* Extract the specific image tag from it */-}}
{{- $tag := index $versions .Values.type .Values.typeVersion "version" -}}
{{-/* Construct the complete image name */-}}
{{- printf "artifactory.myco.com/docker/%s/ubuntu20-slim-%s%s.0f:%s" .Values.type .Values.type .Values.typeVersion $tag }}
不過,這可能無法完全解決您的問題。特別是,.Files.Get只能檢索圖表目錄中的檔案,而不能檢索templates. (該實作實際上將所有非模板檔案預加載到記憶體中,并在您呼叫時只回傳其中一個.Files.Get。)
一種更簡單的方法可能是讓您的部署系統提供完整的映像名稱,也許將存盤庫、名稱和標簽作為單獨的 Helm 值傳遞。您在此處顯示的內容看起來像是您正在嘗試在沒有連接應用程式的情況下部署本地構建的語言運行時,并且它也可能是使影像字串位于應用程式影像的 DockerfileFROM行中的更好方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437372.html
