存放在 git 存放庫中的 chart
Helm chart 不一定要來自 chart 登錄庫。此範例取用存放在 git 存放庫中的 chart——Helm 專案自己的 hello-world 範例 chart——並把它建置成 Kubernetes 資訊清單。
開發者的 Aggregate 對這一切隻字未提。它只宣告一個 core/compute/server,而該伺服器的封裝來自何處,是在 Recipe 中決定並由 Manifest 繫結。
來源區塊
source: kind: git-repository url: https://github.com/helm/examples.git ref: hello-world-0.1.0 module_subdir: charts/hello-world三個座標,各自回答不同的問題:
url— 要複製的存放庫。https://、ssh://與git://都被接受。私有存放庫可透過 SSH agent 以 SSH 連線,或以 HTTPS 連線並在你的 netrc 檔案中有其主機的項目——machine github.com login <user> password <token>。寫在 URL 本身中的認證會被拒絕,如身分驗證所述。ref— 讀取存放庫時所用的參照:標籤、分支或提交。它是存放庫自己的作者所取的名稱,不一定是單純的版本——這裡標籤也帶有 chart 的名稱。module_subdir— 存放庫內作為 chart 的目錄。存放庫是在此路徑下持有 chart,而不是本身就是 chart,而且該目錄是讓成品有所區別的一部分:同一存放庫在同一提交上的兩個目錄是兩個成品,而不是一個。
來源種類指明的是傳輸方式與位置,而不是格式。同一種類可以取得 chart、Terraform 模組或任何其他檔案目錄;目錄裡放的是什麼,則是使用它的提供者的事。
鎖定檔記錄了什麼
只有提交才能指明不會在你不知情下改變的內容。標籤或分支是存放庫可以重新指向另一個提交的名稱,所以 aggrega pull 會解析該參照,並同時記錄兩者——你寫下的參照,以及它解析到的提交:
# release-chart.locksource_artifacts: - kind: git-repository url: https://github.com/helm/examples.git module_subdir: charts/hello-world pinned_version: f3dc419e558e6ccebce2ed447384e5d2e11de2f9 digest: sha256:2bee62cfc2efccc61d8e17efeb72f9f77ad7bd87f3ea4e50b1e4219c8d5a2c78 mediatype: application/gzip referenced_by: - entity: recipe uri: helm/hello-world-git:1.0.0 field: spec.source requested_ref: hello-world-0.1.0兩半都有其用處。提交是建置所取得的內容,所以建置可重現。參照是建置用來查詢該項目的依據,這讓你可以繼續在 Recipe 中寫 ref: hello-world-0.1.0,同時仍解析到鎖定檔寫入時該標籤所在的確切提交。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example chart-from-git-repository它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd chart-from-git-repository建置
aggrega build --release release-chart.yaml --auto-pull此範例帶有兩個 Releases,所以要指名建置哪一個。第一次建置需要鎖定檔,這就是這裡傳入 --auto-pull 的原因;事先執行一次 aggrega pull --release release-chart.yaml 也能達到同樣效果,並讓之後每次建置都不需要旗標。
渲染出的 Deployment 帶有 Release 所提供的值,並對應到 chart 自己的值名稱:
apiVersion: apps/v1kind: Deploymentmetadata: name: web-hello-world [… omitted: the chart's own labels, and the labels and annotations every rendered document carries]spec: replicas: 2 [… omitted: the selector and the pod template's own labels] spec: serviceAccountName: web-hello-world securityContext: {} containers: - name: hello-world securityContext: {} image: "nginx:1.27.3" imagePullPolicy: IfNotPresent [… omitted: the container port and the two probes]每次建置都會印出一則警告:
warning: node "web": .infra key "compute" is read by no template in recipe "hello-world-git" — correct the spelling, or remove the key這則警告是準確的,值得閱讀而不是消音。core/compute/server Component 提供 CPU 與記憶體的規模調整旋鈕;這個特定的 chart 沒有為它公開任何值,所以 Recipe 在那裡沒有對應任何東西。來源式 Recipe 只對應它所取用的 chart 實際接受的內容,編譯器會回報那個沒有到達任何地方的值,而不是假裝它已落地。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自己建立任何資料夾。若 Aggrega 工作區資料夾中還沒有 `chart-from-git-repository` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `chart-from-git-repository` 且不指定 `dir`:它會把範例寫入新的 `chart-from-git-repository` 資料夾。接著呼叫 `build` 工具,將 `workspace_root` 設為 `chart-from-git-repository`、`release` 設為 `release-chart.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `chart-from-git-repository/dist/` 之下。當參照移動時
標籤可以在存放庫中被重新指向。發生這種情況時,下一次一般的 aggrega pull 會發現參照解析到的位置不是它所記錄的提交,並中止而不採用它:
# aggrega pull --release release-chart.yaml## [E0130]# lockfile: out of date; it no longer matches what its Release resolves to# ---# source-artifact kind=git-repository url=https://github.com/helm/examples.git# module_subdir=charts/hello-world: reference "hello-world-0.1.0" moved — the# lockfile pins 03f4a18d3c2b77b7ecd91d0324a95910d4f04152, and it now resolves to# f3dc419e558e6ccebce2ed447384e5d2e11de2f9; adopt where it now points with# `aggrega pull --upgrade --release <release>`它會指出兩個提交,讓你在決定之前能確切看到移動了什麼。鎖定檔維持原狀——中止發生在任何內容被寫入之前——而採用新的提交是一個刻意的動作:
aggrega pull --upgrade --pin-sources --release release-chart.yaml參照被移動的情況,正是 --upgrade 的用途:一般的 pull 會保持鎖定檔所記錄的提交,只有升級才會採用參照現在指向的位置。單獨的 --upgrade 會捨棄所有已記錄的固定,所以會先要求確認。此處也需要 --pin-sources,因為這個 Recipe 也把其來源的提交記錄在它旁邊的 entity.lock 中:升級會把該固定改寫為新的提交,沒有該旗標時,pull 會在該鎖定上中止並指名 aggrega pin。請把改寫後的 entity.lock 與 Recipe 一起提交。
在此工作區中對 Aggregate 或 Release 所做的編輯,屬於另一種漂移:下一次建置會回報它,而一般的 aggrega pull --release release-chart.yaml 會清除它。
在 ref: 中固定提交而不是標籤,是取得同樣保證的另一種方式,代價是一個沒有人能讀懂的座標。
護欄 Release
release-negative.yaml 預期會失敗,它之所以在這裡,是因為只在順利路徑上演練過的取得路徑,無法告訴你它的防護機制如何。它透過 manifest-negative.yaml 部署同一個 Aggregate,後者繫結的 Recipe 與可運作的那個相同,只有參照不同——它指名了存放庫沒有的標籤:
aggrega build --release release-negative.yaml --auto-pull# exits non-zero# [E0137]# auto-pull: resolving the source of each Recipe and FeatureRecipe the Release# uses: resolve: resolve source for recipe# "helm/hello-world-git-missing-ref:1.0.0": sourceresolver: kind=git-repository# url=https://github.com/helm/examples.git version=: oci: artifact not found at# remote source (cause: reference not found)[… omitted: the rest of that line, a remediation naming the URL and version to check]存放庫可以連線,複製也成功了;只是其中沒有這個參照,因此沒有可固定的提交,也沒有可封存的內容。
此範例對網路的需求
與這裡大多數的工作區不同,這個工作區在第一次建置時會複製一個真實的存放庫,所以該次建置需要對 url 中所指名主機的對外存取。一旦鎖定檔與解析快取存在,之後的建置就從快取讀取,完全不需要網路——aggrega build --release release-chart.yaml --offline 會重建出相同的位元組。