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

把工作負載繫結到附加項目所發布的內容

一個單一的無狀態應用程式,由 helm 提供者的 Workload 合成渲染在 Kubernetes 上——沒有外部 chart。應用程式在其 Component 為它宣告槽位、並由 Release 填入的雲端身分下執行:填入槽位的附加項目會發出一個 Kubernetes ServiceAccount 並發布它的名稱,而應用程式的 Recipe 把 pod 繫結到那個已發布的名稱。

教學重點在於流動的方向。生產端只陳述一次名稱;使用端讀取它。使用端不會重述生產端已擁有的值,因此兩者不會彼此偏離。

草擬範例

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

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

Terminal window
aggrega draft --example workload-identity-binding

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

Terminal window
cd workload-identity-binding

建置

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

目錄從 .aggrega-config.yaml 中指名的兩個來源解析:範例自己的 ./catalog(local/compute/identity-app Component 與它的 helm/identity-app 合成 Recipe),以及 hub 登錄庫(core/gcp-workload-identity Feature 與它的 FeatureRecipe)。--auto-pull 會在建置期間寫出全新複製的存放庫所缺少的鎖定檔。三個資訊清單位於 dist/helm/helm/ 底下。

搭配 AI 助理

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

只使用 Aggrega 工具,且不要自行建立任何資料夾。如果工作區資料夾中還沒有 `workload-identity-binding` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `workload-identity-binding`,且不指定 `dir`:它會把範例寫入新的 `workload-identity-binding` 資料夾。接著呼叫 `build` 工具,將 `workspace_root` 設為 `workload-identity-binding`,將 `release` 設為 `release.yaml`,將 `auto_pull` 設為 true,並將 `write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `workload-identity-binding/dist/` 底下。

Component 宣告需求;Release 填入它

應用程式需要一個身分來執行,因此它的 Component 把它宣告為必要的 identity 槽位:

catalog/component/local/compute/identity-app/1.0.0/component.yaml
featuresSchema:
identity:
required: true
selector: "feature.aggrega.ai/kind!=secret"
inject: []

Aggregate 不為它寫任何東西。應用程式在哪個雲端、以哪個身分執行,是操作員為每個目標所做的選擇,因此 Release 以身分 Feature 以及該 Feature 需要的每個欄位填入槽位:

release.yaml
features:
identity:
type: core/gcp-workload-identity
infra:
service_account_name: app-workload-identity
gcp_service_account: app-dev@example-project.iam.gserviceaccount.com

讓槽位未填入的 Release 會中止,並指名該槽位。EKS 叢集的 Release 則改以 core/aws-irsa 及其 role_arn 填入同一個槽位,而它的 Manifest 把該 Feature 對應到它的 FeatureRecipe。選擇器讓認證不會進入槽位:填入它的機密種類 Feature 會中止。

附加項目發布

Feature 在它的 schema.provides 中宣告它發布一個 name,而它的 FeatureRecipe 發布它——來自發出的 chart 用來命名 ServiceAccount 的同一個 local,當 Release 寫入時,那就是由操作員設定的 service_account_name:

# FeatureRecipe gcp-workload-identity
locals:
sa_name: '{{ default (printf "%s-%s" .context.rendered_name (.attachment_name | replace "_" "-")) .infra.service_account_name }}'
provides:
name: "{{ .locals.sa_name }}"

Recipe 使用

使用端的 Recipe 在 .features.<instance-name>.provides.<key> 讀取已發布的名稱:

# helm/identity-app recipe workload
serviceAccount: "{{ .features.identity.provides.name }}"
containers:
main:
variables:
IDENTITY_SERVICE_ACCOUNT: {{ .features.identity.provides.name }}

兩次讀取都解析為附加項目實際發出的 ServiceAccount:

app--identity--app-serviceaccount-app-workload-identity.yaml
[… omitted: the apiVersion]
kind: ServiceAccount
metadata:
name: app-workload-identity
annotations:
iam.gke.io/gcp-service-account: "app-dev@example-project.iam.gserviceaccount.com"
[… omitted: the boundary and recipe annotations, and the labels, that every rendered document carries]
app-deployment.yaml
[… omitted: the Deployment's metadata, selector and pod template up to the containers]
containers:
- name: main
[… omitted: the image and the port]
env:
[… omitted: the GREETING variable]
- name: IDENTITY_SERVICE_ACCOUNT
value: app-workload-identity
serviceAccountName: app-workload-identity

為什麼要讀取它,而不是重複它

另一種做法是在使用端把 app-workload-identity 再寫一次。這種做法在有人改變操作員的 service_account_name 而只更新兩處之一之前都行得通——到那時,pod 請求的是一個不存在的 ServiceAccount,而編譯中沒有任何東西會察覺,因為兩種拼法都是有效的字串。

讀取 .features.identity.provides.name 就移除了第二種拼法。同樣的形狀適用於每個附加項目:認證在 .features.<slot>.provides.name 發布它的 Secret 的定位器,sidecar 發布它的連接埠,身分發布它的名稱。完整的逐欄位附加項目形狀,位於 templates/template-context-contract.md。

操作員的大小設定

release.yaml 為每個環境填入操作員規模設定支柱:容器映像檔標籤來自 infra.image_tag(myapp:1.2),而身分——它的 Feature 與該 Feature 的欄位——來自槽位的填入。開發者的 Aggregate 兩者都不帶。