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

一個開發者意圖,兩種認證傳遞形狀

認證如何到達執行中的容器,是你部署在哪裡的屬性,而不是應用程式的屬性。一個叢集沒有機密儲存區,所以部署自己把內容放在 Kubernetes Secret 中一併送出。另一個執行著機密儲存區 Operator,所以部署只送出一個指標,由叢集在協調時取得內容。

此工作區持有一個 Aggregate,並為兩個目標建置它。Aggregate 在兩者之間不變。資料庫對其密碼的參照也不變。改變的是 Manifest 中的一行。

什麼會移動,什麼保持不動

檔案本機目標儲存區目標
aggregate.yaml同一個檔案同一個檔案
Manifestmanifest-local.yamlmanifest-store.yaml
Releaserelease-local.yamlrelease-store.yaml

兩個 Manifests 的差異在於它們自己的名稱與一個對應項目:

Terminal window
$ diff manifest-local.yaml manifest-store.yaml
7c7
< name: credential-shape-local-manifest
---
> name: credential-shape-store-manifest
16c16
< core/referenced-secret:1.0.0: helm/k8s-secret-from-parameter:1.0.0
---
> core/referenced-secret:1.0.0: helm/k8s-external-secret:1.0.0

兩個 Releases 的差異在於它們自己的名稱,以及它們各指向哪個 Manifest:

Terminal window
$ diff release-local.yaml release-store.yaml
7c7
< name: credential-shape-local
---
> name: credential-shape-store
12c12
< manifest: credential-shape-local-manifest
---
> manifest: credential-shape-store-manifest

兩個 Releases 宣告相同的 environment。這是刻意的:環境名稱會作為標籤蓋印在每份渲染出的文件上,所以讓它保持不動,就恰好只剩一件事可以自由移動——Manifest 所繫結的 FeatureRecipe。真實的一對 Releases 當然會指名各自的環境,而認證形狀也會以完全相同的方式不同。

開發者這一側,一次

aggregate.yaml 以同一個可參照的認證 Feature 填入 PostgreSQL Component 的兩個認證槽位:

features:
password:
type: core/referenced-secret
values:
key: POSTGRES_PASSWORD
origin: postgres-superuser-password
app_user_password:
type: core/referenced-secret
values:
key: APP_USER_PASSWORD
origin: postgres-app-password

core/referenced-secret 指名一個機密資源,也就是它的 FeatureRecipe 輸出所建立的資源,或已存在的資源,並發布該資源的定位器——它的 name 以及其中的資料 key——透過它的 provides 契約。使用的 Recipe 讀取該定位器。它從不自己衍生資源名稱,也從不詢問是哪個 FeatureRecipe 產生了它。

每個目標發出什麼

每次建置寫出六個檔案。其中四個——StatefulSet、兩個 Services 與初始化 ConfigMap——兩個目標產生的結果逐位元組相同。另外兩個是認證資源,每個填入的槽位一個,而它們正是目標不同之處。

本機目標,來自 k8s-secret-from-parameter,位於 dist/helm/helm/db--password--db-secret-db-password.yaml:

apiVersion: v1
kind: Secret
metadata:
name: db-password
[… omitted: the labels and annotations naming the aggregate, component, environment, compiler and provider versions, boundary and Recipe — the same set every rendered document carries]
stringData:
POSTGRES_PASSWORD: "postgres-superuser-password"

儲存區目標,來自 k8s-external-secret,位於 dist/helm/helm/db--password--db-externalsecret-db-password.yaml:

apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: db-password
[… omitted: the same label and annotation set as above]
spec:
refreshInterval: "1h"
secretStoreRef:
name: platform-secret-store
kind: ClusterSecretStore
target:
name: db-password
creationPolicy: Owner
data:
- secretKey: POSTGRES_PASSWORD
remoteRef:
key: "postgres-superuser-password"

不同的 API 群組、不同的種類、不同的內容。在本機目標上,認證內容在渲染出的文件中,任何能讀取該部署的人都能讀到——這就是為什麼這種形狀適合本機與開發叢集,而不是正式環境的叢集。在儲存區目標上,渲染出的文件只帶有項目名稱;內容留在儲存區中,而 Operator 把它協調成名為 db-password 的 Secret。

比較兩棵輸出樹,會顯示差異,也顯示它在何處停止(下方的建置一節有產生 local/ 與 store/ 的命令):

Terminal window
$ diff -r local store
Only in store/helm/helm: db--app-zuuser-zupassword--db-externalsecret-db-app-user-password.yaml
Only in local/helm/helm: db--app-zuuser-zupassword--db-secret-db-app-user-password.yaml
Only in store/helm/helm: db--password--db-externalsecret-db-password.yaml
Only in local/helm/helm: db--password--db-secret-db-password.yaml

四行,全都在同一個目錄中,因為在此目標上,附加項目的檔案與元件自己的檔案並排,而不是放在子目錄中。檔名帶有兩個座標:db--password-- 是 password 附加項目,位於 db,而 db--app-zuuser-zupassword-- 是 app_user_password 那一個——底線編碼為 -zu,因為座標與檔案自己的名稱在這裡共用同一個字串。

四行,沒有別的:兩個認證資源只出現在本機目標的樹中,兩個只出現在儲存區目標的樹中。diff -r 在兩棵樹共有的任何檔案中都回報沒有內容差異,所以四份共同的文件是相同的,而不只是相似。

不會移動的參照

資料庫容器把兩個認證都讀作由 secretKeyRef 支援的環境變數。此區塊在兩個目標中逐位元組相同:

- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-password
key: POSTGRES_PASSWORD
- name: APP_USER_PASSWORD
valueFrom:
secretKeyRef:
name: db-app-user-password
key: APP_USER_PASSWORD

它之所以相同,是因為使用的 Recipe 不分支。它向槽位詢問其發布的 name 與 key 並把它們寫出。兩個 FeatureRecipes 發布同樣的兩個鍵,因為 Feature 宣告了它們,所以任一個都能滿足使用端——而在儲存區目標上,一旦 Operator 把 ExternalSecret 協調成該名稱下的 Secret,該參照就會解析。

這種不變性,正是讓兩種形狀可以替換的原因。 如果使用端自己衍生 Secret 名稱,那麼更換繫結後,它會指向一個在該名稱下已不存在的資源。

origin 的意義由 FeatureRecipe 決定

core/referenced-secret 把 origin 宣告為內容來自何處,並把它的解讀留給 FeatureRecipe:

  • k8s-secret-from-parameter 把它作為內容寫入 Secret。
  • k8s-external-secret 把它讀作要從儲存區取得的鍵。

編譯器原封不動地傳遞該字串;它與某個字串屬於其中哪一種無關。這使得一個附加項目能不經開發者這一側任何編輯,就服務兩種形狀——也是更換繫結之前要檢查的事,因為同一個撰寫出的字串,必須對讀取它的任何 FeatureRecipe 都有意義。

發布的名稱,與槽位名稱中的底線

Component 把它的第二個認證槽位命名為 app_user_password。Kubernetes 物件名稱不能含底線,所以發布的 provides.name 會把底線正規化為連字號,而資源最後落在 db-app-user-password:

- name: APP_USER_PASSWORD
valueFrom:
secretKeyRef:
name: db-app-user-password

兩個 FeatureRecipes 的正規化完全相同,所以更換繫結不會移動使用端的參照。名稱在生產端只發布一次,而發出的資源與使用端的參照,都從那一個已發布的值解析。

儲存區目標對叢集的期望

ExternalSecret 是自訂資源。把儲存區目標的輸出套用到叢集,需要該處執行著機密儲存區 Operator,並且存在一個名為 platform-secret-store 的 ClusterSecretStore。此工作區沒有安裝其中任何一個:範例渲染的是這樣的叢集所使用的內容,並說明形狀如何被選擇。

叢集執行哪個儲存區、以什麼名稱,是目標的屬性,而不是應用程式的屬性,所以 k8s-external-secret 沒有為它提供撰寫用的鍵。儲存區名稱不同的叢集,由指名該儲存區的 FeatureRecipe 來服務。

草擬範例

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

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

Terminal window
aggrega draft --example credential-shape-per-target

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

Terminal window
cd credential-shape-per-target

建置

Terminal window
aggrega build --release release-local.yaml --auto-pull
aggrega build --release release-store.yaml --auto-pull

--auto-pull 在第一次建置時,把目錄解析成 Release 旁的鎖定檔;同一個 Release 之後的建置不需要旗標。

兩個目標都寫入 dist/,而 aggrega build 不會清除它,所以請把每次執行的輸出另外保存,以便比較兩者:

Terminal window
out=$(mktemp -d)
rm -rf dist && aggrega build --release release-local.yaml --auto-pull && cp -r dist "$out/local"
rm -rf dist && aggrega build --release release-store.yaml --auto-pull && cp -r dist "$out/store"
( cd "$out" && diff -r local store )

要確認每個目標以確定性方式渲染,請加上 --verify-determinism:每個節點會被渲染兩次,若任何節點的兩次渲染不同,建置就會中止。

Terminal window
aggrega build --release release-local.yaml --verify-determinism
aggrega build --release release-store.yaml --verify-determinism

搭配 AI 助理

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

只使用 Aggrega 工具,且不要自己建立任何資料夾。若 Aggrega 工作區資料夾中還沒有 `credential-shape-per-target` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `credential-shape-per-target` 且不指定 `dir`:它會把範例寫入新的 `credential-shape-per-target` 資料夾。接著對每個 Release 各呼叫一次 `build` 工具,依此順序——`release-local.yaml`、`release-store.yaml`——每次都將 `workspace_root` 設為 `credential-shape-per-target`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `credential-shape-per-target/dist/` 之下。

檔案

  • aggregate.yaml — 開發者意圖:一個型別為 core/datastore/postgres 的 db 元件,兩個認證槽位都由 core/referenced-secret 填入。兩個 Releases 原封不動地使用。
  • manifest-local.yaml — 把 core/referenced-secret 繫結到 k8s-secret-from-parameter,後者從撰寫出的內容渲染 Kubernetes Secret。
  • manifest-store.yaml — 把同一個 Feature 繫結到 k8s-external-secret,後者渲染指名儲存項目的 ExternalSecret。與 manifest-local.yaml 相同,只差它自己的名稱與那一行。
  • release-local.yaml、release-store.yaml — 兩個 Releases。相同,只差它們自己的名稱與各自指向的 Manifest。
  • .aggrega-config.yaml——指名目錄實體所解析自的 hub 登錄庫。

這裡的 origin 值是預留位置。它們不是任何真實服務的認證,且在本機目標上,兩者都以明文渲染到輸出中。