跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

你自己的 webhook 憑證:略過 chart 產生的 Secret

這是進階範例。它教授 Recipe 欄位 omits:,大多數 Recipes 永遠用不到:它適用於你原樣採用的來源——這裡是從登錄庫擷取的 chart——它渲染出你的 Release 不得發布的文件。遇到那種情況時再使用它。

這個工作區透過標準程式庫 Recipe helm/envoy-ai-gateway-operator,以其 webhook 憑證可採用的兩種模式,建置 Envoy AI Gateway 控制平面,也就是 Envoy Gateway 以擴充伺服器呼叫的控制器。在其中一種模式下,專案的 chart 會渲染一個 Secret,持有它每次渲染都重新產生的金鑰與憑證,而 Recipe 的 omits: 項目會將該 Secret 從建置中略去。另外兩個 Releases 展示你可能在該欄位周邊遇到的兩種中止:該項目存在就是為了防止的那一種,以及不再符合任何內容的項目所產生的那一種。

它建置什麼

core/gateway/envoy-ai-gateway-operator 的一個元件 ai-controller,編譯為 Kubernetes 資訊清單。Aggregate 對每個 Release 都相同。每個 Release 將控制器置於 envoy-ai-gateway-system 並選擇憑證模式;每個 Manifest 選擇 Recipe:

Release其 Manifest 繫結的憑證模式結果
release-cert-manager.yamlhelm/envoy-ai-gateway-operator,來自標準程式庫cert-manager建置成功
release-own-certificate.yamlhelm/envoy-ai-gateway-operator,來自標準程式庫Secret ai-gateway-webhook-tls建置成功,每次建置位元組完全相同
release-negative-without-omits.yamlhelm/envoy-ai-gateway-operator-without-omits,來自此工作區Secret ai-gateway-webhook-tls以 --verify-determinism 建置時以 E0058 中止
release-negative-stale-omits.yamlhelm/envoy-ai-gateway-operator-stale-omits,來自此工作區Secret ai-gateway-webhook-tls以 E0334 中止

此工作區 catalog/ 中的兩個 Recipes 是標準程式庫 Recipe 的教學變體。各自從同一個 release 擷取同一個 chart,並以同一個對應選擇憑證模式:一個沒有 omits: 項目,另一個的項目指名 chart 不渲染的 Secret。它們各有自己的名稱,所以與標準程式庫 Recipe 並列,而不是取代它,而 Release 所指名的 Manifest 決定它用哪一個建置。

給 webhook 憑證的兩種方式

控制器執行一個變更型 webhook,叢集的 API 伺服器透過 TLS 呼叫它,所以 webhook 提供憑證。Component 提供兩種供應憑證的方式,由一個 infra 鍵 webhook_tls_secret 選擇:

  • 未設定,預設。 chart 會為控制器的 Service 名稱渲染一個 cert-manager Certificate,以及簽署它的自簽 Issuer,所以叢集中必須已經執行 cert-manager。
  • 已設定。 webhook 提供該鍵所指名的 Secret 中的憑證,這個 Secret 由部署者事先在控制器的命名空間中建立,持有 tls.crt、tls.key 與 ca.crt。不會渲染 Certificate 與 Issuer,也不需要 cert-manager:控制器啟動時,會將該 Secret 的 ca.crt 寫入 webhook 設定,作為 API 伺服器用來檢查 webhook 的 CA 套組。

release-own-certificate.yaml 設定它:

components:
ai-controller:
annotations:
aggrega.ai/namespace: envoy-ai-gateway-system
infra:
webhook_tls_secret: ai-gateway-webhook-tls

在自有憑證模式下 chart 渲染什麼

設定該鍵後,專案的 chart 會將該名稱的 Secret 掛載到控制器 pod 的 /certs——同時它也以同一個名稱渲染自己的 Secret,持有它在渲染時產生的 CA 憑證、憑證與私密金鑰。兩次建置會產生兩把不同的金鑰,所以輸出不再於每次建置之間位元組完全相同;而套用它會用 chart 產生的 Secret 取代操作員所建立的 Secret。

chart 是從專案的登錄庫擷取並原樣使用的;Recipe 不編輯擷取到的 chart。標準程式庫 Recipe 改為略去該 Secret:

# helm/envoy-ai-gateway-operator:1.0.0, its omits: field
omits:
- kind: Secret
name: "{{ .infra.webhook_tls_secret }}"
when: '{{ ne (optional .infra "webhook_tls_secret" "") "" }}'
  • kind 與 name 指名 chart 所渲染的文件:一個 Secret,其 metadata.name 是 Release 所設定的名稱。name 範本讀取給 chart 的同一個 infra 鍵,所以不論 Release 如何稱呼它,項目都指名所產生的 Secret。
  • when 讓項目只在這個模式下適用。 在 cert-manager 模式下,chart 不渲染這樣的 Secret;條件在那裡為假,而 when: 為假的項目會在檢查任何東西之前被捨棄。一個 Recipe 同時服務兩種模式。

chart 的其餘部分仍完整渲染,只有符合的文件會從建置寫入的內容中捨棄。在這裡略去它是正確的,因為控制器的 pod 掛載的 Secret 是操作員自己另外建立的。該欄位略去一份文件;它不檢查輸出其餘部分所依賴的東西是否存在於別處。

草擬範例

你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example ai-gateway-own-webhook-certificate

它接著會印出要接著執行的命令。第一個會切換到新資料夾:

Terminal window
cd ai-gateway-own-webhook-certificate

建置

Terminal window
aggrega build --release release-cert-manager.yaml --auto-pull
aggrega build --release release-own-certificate.yaml --auto-pull
aggrega build --release release-negative-without-omits.yaml --auto-pull --verify-determinism # halts; exits 1
aggrega build --release release-negative-stale-omits.yaml --auto-pull # halts; exits 1

.aggrega-config.yaml 列出工作區自己的 catalog/,其中有兩個教學 Recipes;Component 與標準程式庫 Recipe 則從 hub.aggrega.ai,也就是它所指名的預設登錄庫解析。若有標準程式庫目錄的檢出(取得方式),以 --catalog 指名它的 aggrega-catalog 目錄,每個實體就從這兩個目錄解析。

--auto-pull 在建置期間填入每個 Release 的鎖定檔;鎖定檔與渲染輸出都是產生出來的,不隨附發布。解析鎖定檔會從 Docker Hub 擷取控制器的 chart,所以每個 Release 的第一次建置需要網路。

搭配 AI 助理

以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。

只使用 Aggrega 工具,且不要自己建立資料夾。若工作區資料夾中還沒有 `ai-gateway-own-webhook-certificate` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `ai-gateway-own-webhook-certificate` 且不指定 `dir`:它會將範例寫入新的 `ai-gateway-own-webhook-certificate` 資料夾。然後針對每個 Release 各呼叫一次 `build` 工具,依此順序——`release-cert-manager.yaml`、`release-own-certificate.yaml`——每次都將 `workspace_root` 設為 `ai-gateway-own-webhook-certificate`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。接著呼叫 `cli-build` 工具,它會執行 `aggrega build` 並接受其 `--verify-determinism` 旗標,將 `workspace_root` 設為 `ai-gateway-own-webhook-certificate`、`release` 設為 `release-negative-without-omits.yaml`、`auto_pull` 設為 true、`verify_determinism` 設為 true:它預期會被拒絕,請把拒絕內容念給我聽。然後以與前兩次建置相同的方式呼叫 `build` 工具,將 `release` 設為 `release-negative-stale-omits.yaml`:它也預期會被拒絕,所以也請把那次拒絕內容念給我聽。告訴我每次建置寫入了哪些檔案;它們位於 `ai-gateway-own-webhook-certificate/dist/` 之下。

兩種憑證模式,如渲染結果所示

release-cert-manager.yaml 以 --auto-pull 建置,會印出,其中 <workspace> 代表此範例的資料夾:

Pull complete: release-cert-manager.lock (entities=5, source_artifacts=1, external_inputs=0)
[… omitted: the total build time, which differs per run]
Build completed: 16 files written to <workspace>/dist

然後 ls dist/helm 列出:

ai-controller
ai-controller-certificate-envoy-ai-gateway-system-self-signed-cert-for-mutating-webhook.yaml
ai-controller-clusterrole-ai-controller-envoy-ai-gateway-system.yaml
ai-controller-clusterrole-envoy-ai-gateway-inference-pool-reader-envoy-ai-gateway-system.yaml
ai-controller-clusterrolebinding-ai-controller-envoy-ai-gateway-system.yaml
ai-controller-clusterrolebinding-envoy-ai-gateway-inference-pool-reader-binding-envoy-ai-gateway-system.yaml
ai-controller-deployment-envoy-ai-gateway-system-ai-controller.yaml
ai-controller-issuer-envoy-ai-gateway-system-self-signed-issuer-for-mutating-webhook.yaml
ai-controller-mutatingwebhookconfiguration-envoy-ai-gateway-gateway-pod-mutator-envoy-ai-gateway-system.yaml
ai-controller-service-envoy-ai-gateway-system-ai-controller.yaml
ai-controller-serviceaccount-envoy-ai-gateway-system-ai-controller.yaml

ai-controller 目錄持有 Recipe 在 chart 旁發出的六個 AI Gateway 資源定義。以同樣方式建置的 release-own-certificate.yaml 會印出:

Pull complete: release-own-certificate.lock (entities=5, source_artifacts=1, external_inputs=0)
[… omitted: the total build time, which differs per run]
Build completed: 14 files written to <workspace>/dist

而 ls dist/helm 列出:

ai-controller
ai-controller-clusterrole-ai-controller-envoy-ai-gateway-system.yaml
ai-controller-clusterrole-envoy-ai-gateway-inference-pool-reader-envoy-ai-gateway-system.yaml
ai-controller-clusterrolebinding-ai-controller-envoy-ai-gateway-system.yaml
ai-controller-clusterrolebinding-envoy-ai-gateway-inference-pool-reader-binding-envoy-ai-gateway-system.yaml
ai-controller-deployment-envoy-ai-gateway-system-ai-controller.yaml
ai-controller-mutatingwebhookconfiguration-envoy-ai-gateway-gateway-pod-mutator-envoy-ai-gateway-system.yaml
ai-controller-service-envoy-ai-gateway-system-ai-controller.yaml
ai-controller-serviceaccount-envoy-ai-gateway-system-ai-controller.yaml

cert-manager 模式的樹包含一個 Certificate 與一個 Issuer,沒有 Secret。自有憑證模式的樹三者皆無:沒有 Certificate 與 Issuer,因為 chart 在該模式下兩者都不渲染;也沒有 Secret,因為 Recipe 的項目略去了 chart 所渲染的那一個。控制器改為掛載的是 Release 所指名的 Secret:

dist/helm/ai-controller-deployment-envoy-ai-gateway-system-ai-controller.yaml
[… omitted: the chart's licence header, four comment lines]
apiVersion: apps/v1
kind: Deployment
metadata:
name: ai-controller
namespace: envoy-ai-gateway-system
[… omitted: the labels and annotations every rendered document carries]
spec:
[… omitted: the replica count, the selector, and the pod template's labels, service account, host-network setting and security settings]
containers:
- name: ai-gateway-helm
[… omitted: the container's security settings, its image and pull policy, and its ports]
args:
[… omitted: the controller's logging, external-processor, gateway-namespace and endpoint arguments]
- --tlsCertDir=/certs
- --tlsCertName=tls.crt
- --tlsKeyName=tls.key
[… omitted: the controller's webhook-port, leader-election, prefix, cache, namespace, quota and session-encryption arguments]
[… omitted: the probes, the environment and the resources]
volumeMounts:
- mountPath: /certs
name: certs
readOnly: true
volumes:
- name: certs
secret:
secretName: ai-gateway-webhook-tls

控制器從 /certs 讀取它的憑證,也就是 certs 磁碟區,它就是 Secret ai-gateway-webhook-tls——建置原樣傳遞這個名稱,叢集在 pod 啟動時解析它。

從一次建置到下一次的相同位元組

第二次以 --verify-determinism 建置時,release-own-certificate.yaml 會印出:

[… omitted: the total build time, which differs per run]
Build completed: 14 files written to <workspace>/dist

--verify-determinism 以相同的輸入將每個元件渲染兩次,若兩次渲染不同就中止。它在這裡通過,而它寫出的樹與第一次建置寫出的樹位元組完全相同。兩者成立都是因為 chart 每次重新產生的那一份文件從不進入輸出。

沒有該項目:檢查確定性的建置會中止

release-negative-without-omits.yaml 是透過 manifest-without-omits.yaml 部署的自有憑證 Release,該 Manifest 繫結沒有 omits: 項目的教學 Recipe。以 --verify-determinism 建置:

[… omitted: the pull's lines]
[TRANSLATION_ERROR E0058] instance ai-controller
rendering node "ai-controller"
caused by: wasmengine: node "ai-controller" (provider "helm"): two renders of identical input produced different output: provider render is not deterministic

建置以 1 結束。控制器的兩次渲染不同,因為 chart 每次都產生不同的金鑰與憑證。pull 也會渲染 Release,以找出它需要固定的外部輸入,並在那裡遇到同樣的中止;這就是省略的 pull 行第二次帶有該句子的原因。鎖定檔仍會寫入。

不帶旗標建置,同一個 Release 會完成。它的樹包含自有憑證 Release 所含的 chart 文件——但少了資源定義,那是教學 Recipe 不發出的——以及多出的一份,也就是 chart 產生的 Secret:

dist/helm/ai-controller-secret-envoy-ai-gateway-system-ai-gateway-webhook-tls.yaml
apiVersion: v1
kind: Secret
metadata:
name: ai-gateway-webhook-tls
namespace: envoy-ai-gateway-system
[… omitted: the labels and annotations every rendered document carries]
data:
[… omitted: the three base64-encoded entries ca.crt, tls.crt and tls.key]

建置兩次,兩棵樹恰好在這三個值上不同。一般建置只將每個元件渲染一次,所以它寫入它所產生的任何金鑰,而不會注意到;是這項檢查讓差異可見。套用後,這個檔案會取代操作員所建立的 Secret。

這項中止在 A provider rendered the same input twice and produced different output(提供者對同一份輸入渲染了兩次,卻產生不同的輸出) 下說明,其修正方式指出當差異是你所取用來源的 chart 所產生的值時有兩條出路:透過 chart 提供的設定供應該值,或者在產生的文件是 Release 不得發布的文件時,以 omits: 項目略去它——標準程式庫 Recipe 採用的做法。

不符合任何內容的項目

release-negative-stale-omits.yaml 是透過 manifest-stale-omits.yaml 部署的自有憑證 Release,該 Manifest 繫結的教學 Recipe,其項目是依控制器渲染的名稱,而不是依鍵來指名 Secret:

# catalog/recipe/helm/envoy-ai-gateway-operator-stale-omits/1.0.0/recipe.yaml, its omits: field
omits:
- kind: Secret
name: "{{ .context.rendered_name }}-webhook-tls"
when: '{{ ne (optional .infra "webhook_tls_secret" "") "" }}'

建置:

[… omitted: the pull's lines]
[TRANSLATION_ERROR E0334] instance ai-controller
rendering node "ai-controller"
caused by: [CATALOG_ERROR] instance ai-controller
an omits entry left out nothing the source rendered (node "ai-controller", provider "provider/helm:0.8.8-75-g9e6ef4e0", "envoy-ai-gateway-operator-stale-omits" omits[0], kind "Secret", name "ai-controller-webhook-tls"). correct the entry so it names something the source renders, or remove it; a source upgrade that renamed or removed it is the usual cause

提供者以執行該建置的二進位檔的版本命名,所以你自己的執行會在那裡顯示你的二進位檔版本。

建置以 1 結束,pull 的行因同樣的原因帶有同樣的句子。訊息由左至右讀作要看哪裡:元件、渲染它的提供者、宣告該項目的 Recipe 與該項目在其 omits: 清單中的位置(從 0 起算),然後是該項目所解析成的種類與名稱。when: 條件在這個模式下為真,所以項目適用;它的名稱渲染為 ai-controller-webhook-tls,chart 將它的 Secret 命名為 ai-gateway-webhook-tls,而項目什麼也沒略去。

每個適用的項目都必須略去某些東西,而建置會在不符合的項目上停止,因為不符合任何內容的項目,是關於來源的一項不再為真的陳述。通常的原因是來源升級重新命名了文件。重新命名的文件接著以新名稱出現在輸出中——正是該項目原本要排除的文件——而中止正是防止它悄悄發布的機制。在這次建置中,chart 產生的 Secret 就是這樣一份文件:項目是為了排除它而寫的,卻指名了別的東西。要修正項目,將它與來源現在渲染的內容比較:移除該項目後建置,如 release-negative-without-omits.yaml 在沒有檢查的情況下所做的,並從 chart 寫出的檔案讀出名稱。

E0334 在診斷參考中列為 omit-matched-nothing,並在 An omits entry left out nothing the source rendered(omits 項目沒有排除來源所渲染的任何東西) 下說明。欄位本身——每個項目欄位、範本情境、Release 能對項目決定什麼,以及哪些提供者遵循它——見 Recipe → omits: 欄位。

檢查每次建置

這個自我檢查在文件存放庫的檢出中執行,其中有它所使用的共用建置輔助程式;aggrega draft --example 所寫出的副本不帶該輔助程式。

Terminal window
./check.sh

check.sh 透過 ./build-release.sh 建置四個 Releases,並將每個結果與本頁所述的結果比對。它將每個通過的 Release 建置兩次,第二次帶 --verify-determinism:兩次建置都必須以 0 結束,並寫入相同的檔案,位元組完全相同;而樹必須持有——依每份渲染文件的種類計算——在 cert-manager 模式下有一個 Certificate 與一個 Issuer、沒有 Secret,在自有憑證模式下三者皆無,且控制器 Deployment 的 certs 磁碟區指名 ai-gateway-webhook-tls。護欄 Release 必須以非零結束,且恰有一個中止,帶有本頁為它展示的簡短代碼與句子,而過時項目的中止必須指名該項目。它為每個 Release 印出一行,只要有任何結果不同就以 1 結束,並印出該建置的輸出:

RELEASE EXIT EXPECTED VERDICT
------------------------------------------------------------------------------------------------
release-cert-manager.yaml 0/0 pass OK: byte-identical twice; Certificate=1 Issuer=1 Secret=0
release-own-certificate.yaml 0/0 pass OK: byte-identical twice; Certificate=0 Issuer=0 Secret=0; certs volume: ai-gateway-webhook-tls
release-negative-without-omits.yaml 1 halt OK: E0058, the expected sentence
release-negative-stale-omits.yaml 1 halt OK: E0334, the expected sentence, naming "envoy-ai-gateway-operator-stale-omits" omits[0]
check: every build ended as expected.

它需要單一建置所需要的:標準程式庫目錄,以及擷取 chart 用的網路。它使用 ./build-release.sh 所解析的二進位檔,所以由 AGGREGA_BIN 選擇它;-- 之後的引數會傳給每次建置。當此目錄中有 dist/ 時它拒絕啟動,因為它比較建置所寫的樹,而且它不會留下任何東西。它的結束狀態就是全部判定,所以管線可以用它取代逐一建置每個 Release。

編譯器不解讀的地方

編譯器不知道 Secret 是什麼、控制器拿它做什麼,或 chart 為什麼會產生一個。它像評估任何 Recipe 範本一樣評估項目的範本,將它們解析出的種類與名稱連同元件的輸入交給 helm 提供者,而提供者會捨棄符合的渲染文件,並回報每個項目捨棄了多少。它從不在叢集中尋找 ai-gateway-webhook-tls,從不建立它,也從不檢查它是否持有控制器讀取的三個項目:該名稱是 Release 所供應的字串,傳遞進 chart 的值與該項目中。Secret 在控制器啟動之前存在,是部署端要安排的事。

檔案

  • aggregate.yaml — AI 閘道控制器,唯一的元件;每個 Release 都相同。
  • manifest.yaml — 將控制器繫結到標準程式庫 Recipe;由兩個通過的 Releases 共用。
  • manifest-without-omits.yaml、manifest-stale-omits.yaml — 將它繫結到兩個教學 Recipes 之一。
  • release-cert-manager.yaml、release-own-certificate.yaml — 兩種憑證模式。
  • release-negative-without-omits.yaml、release-negative-stale-omits.yaml — 自有憑證模式經由各個教學 Recipe;兩者都中止。
  • catalog/recipe/helm/envoy-ai-gateway-operator-without-omits/1.0.0/ — 沒有 omits: 項目的教學 Recipe,以及固定其 chart 的 entity.lock。
  • catalog/recipe/helm/envoy-ai-gateway-operator-stale-omits/1.0.0/ — 項目不符合任何內容的教學 Recipe,以及它的 entity.lock。
  • .aggrega-config.yaml — 目錄搜尋堆疊與登錄庫。
  • build-release.sh — 建置一個 Release,並回顯等效的 aggrega build 命令。
  • check.sh — 建置全部四個並檢查每個結果。