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

附加無人撰寫之 Features 的原則:在 `annotation-transform` 注入的註記

治理原則為商店的每個元件提供平台的監控與一個 AWS IAM 角色。它的做法是新增註記。Manifest 把兩個註記鍵繫結到兩個 Features,而繫結鍵之下的註記會展開成 Feature 附加項目。因此原則所新增的註記,替每個元件帶來 Prometheus ServiceMonitor 與 Kubernetes ServiceAccount,而 Aggregate 與 Release 兩者都不寫它們。

原則在 annotation-transform 角色上附加,編譯進 CLI 的每個原則引擎各一次:Rego 與 CEL。核心證明是一個手動寫入相同註記的對照 Release。在沒有原則的情況下建置,它渲染出的檔案與被原則加註的 Release 逐位元組相同。

它建置什麼

實例目錄 Component它在 release.yaml 中攜帶什麼
apiexample/compute/service無
workerexample/compute/service無
gatewayexample/compute/service它自己的監控註記,間隔為 10 秒

三者都透過 Helm 編譯到 Kubernetes,輸出到 dist/shop/helm/。example/compute/service 是標準程式庫目錄中的示範 Component。它的每個實例都必須帶有一個 core/prometheus-exporter 附加項目,這是 Component 所聲明、並由建置檢查的要求。

兩個註記繫結

manifest.yaml 賦予兩個註記鍵意義:

features:
type:
core/prometheus-exporter: helm/prometheus-exporter:1.0.0
core/aws-irsa: helm/aws-irsa:1.0.0
annotations:
feature.aggrega.ai/monitoring:
feature: core/prometheus-exporter
maps:
infra:
- interval
- port
- path
aws-role:
feature: core/aws-irsa
maps:
infra:
- role_arn

繫結若路由多個子鍵,就以一個命名每個子鍵的物件作為其值,所以 feature.aggrega.ai/monitoring 接受 {interval: …, port: …, path: …}。路由單一子鍵的繫結則接受字串,所以 aws-role 接受角色的 ARN。註記展開成的附加項目以它的鍵命名。core/aws-irsa 以附加項目命名它的 ServiceAccount,這就是該鍵不帶點或斜線的原因。

附加原則

.aggrega-config.yaml 將 hub 指名為其登錄庫,不指名任何目錄資料夾,也沒有治理附加項目。原則是由建置以 --config 指名的檔案附加的:Rego 引擎用 governance-rego.yaml,CEL 引擎用 governance-cel.yaml。Rego 那一個內容如下:

plugins:
governance:
- uri: governance/rego:0.9.0
role: annotation-transform
with:
sources:
- policies/platform-defaults.rego

--config 為該次執行指名全域設定檔,取代機器原本會讀取的那一個,而此工作區的 .aggrega-config.yaml 仍然疊在其上。

0.9.0 是 hub 發布各引擎之 Plugin 實體的一個版本,因此附加項目會從工作區所讀取的登錄庫解析,或從該發行版本的 aggrega 二進位檔解析,後者隨附了它。實際執行的引擎,是編譯進執行該建置之 aggrega 二進位檔的那一個,不論附加項目指名哪個版本。監控註記所接受的物件,需要能注入子鍵物件的引擎,而 0.8.7 之前發行版本的引擎做不到。

annotation-transform 原則讀取什麼

位於 annotation-transform 的原則讀取兩樣東西:

  • input.targets 列出它可以註記的每個元件。每個目標都有 boundary_path、component_name,以及 Release 寫在該元件上的 annotations,每個值都照所寫的樣子。
  • input.binding_keys 列出 Manifest 所繫結的註記鍵。

aggrega build --release <release> --dry-run --policy-input <dir> 會把每個角色所收到的酬載寫入 <dir>,並且不寫入其他任何東西。在對 release.yaml 的建置填好其鎖定檔之後:

Terminal window
aggrega build --release release.yaml --config governance-rego.yaml --dry-run --policy-input payloads
jq -c '.binding_keys' payloads/annotation-transform.json
# ["feature.aggrega.ai/monitoring","aws-role"]
jq -c '.targets[]' payloads/annotation-transform.json
# {"boundary_path":"shop","component_name":"api","annotations":{}}
# {"boundary_path":"shop","component_name":"worker","annotations":{}}
# {"boundary_path":"shop","component_name":"gateway","annotations":{"feature.aggrega.ai/monitoring":{"interval":"10s","port":"metrics","path":"/metrics"}}}

目標的 annotations 存放 Release 寫在元件本身上的內容。邊界的 aggrega.ai/namespace 不在其中。

原則

policies/platform-defaults.rego 在 annotations 之下,為每個註記收集一項決定。每項決定以讀取目標時所用的兩個欄位指名該目標,接著是鍵與值:

annotations contains {
"boundary_path": t.boundary_path,
"component_name": t.component_name,
"annotation_key": monitoring,
"annotation_value": {"interval": "30s", "port": "metrics", "path": "/metrics"},
} if {
monitoring in input.binding_keys
some t in input.targets
not monitoring in object.keys(t.annotations)
}
annotations contains {
"boundary_path": t.boundary_path,
"component_name": t.component_name,
"annotation_key": role,
"annotation_value": sprintf("arn:aws:iam::123456789012:role/shop-%v", [t.component_name]),
} if {
role in input.binding_keys
some t in input.targets
not role in object.keys(t.annotations)
}

每條規則只對 input.binding_keys 中的鍵觸發,因此原則不會加入任何 Manifest 沒有賦予意義的東西。每條規則也會略過已帶有該鍵的目標。對元件已帶有的鍵所作的決定,會取代 Release 所寫的值。沒有那項測試的話,gateway 的 10 秒間隔會輸給原則的 30 秒。

policies/platform-defaults.cel 把同樣兩項決定寫成一個運算式,以 + 連接兩個清單。對應表的鍵用 in 而不是 object.keys 測試,而角色的 ARN 用 + 連接,因為該運算式語言沒有 sprintf。運算式中屬於角色的那一半內容如下:

+ input.targets
.filter(t, "aws-role" in input.binding_keys
&& !("aws-role" in t.annotations))
.map(t, {
"boundary_path": t.boundary_path,
"component_name": t.component_name,
"annotation_key": "aws-role",
"annotation_value": "arn:aws:iam::123456789012:role/shop-" + t.component_name,
}),

建置渲染出什麼

Terminal window
aggrega build --release release.yaml --config governance-rego.yaml --auto-pull

在 dist/shop/helm/ 之下寫出十二個檔案:每個元件的 Deployment 與 Service、其監控註記所展開成的 ServiceMonitor,以及其 aws-role 註記所展開成的 ServiceAccount。api 的 ServiceMonitor 之 spec,來自原則所注入的註記:

spec:
selector:
matchLabels:
app.kubernetes.io/name: api
endpoints:
- port: metrics
interval: 30s
path: /metrics

gateway 的帶有 interval: 10s,也就是其 Release 項目所寫的值。api 所使用的 ServiceAccount,來自注入的 aws-role:

apiVersion: v1
kind: ServiceAccount
metadata:
name: api-aws-role
namespace: shop
annotations:
eks.amazonaws.com/role-arn: "arn:aws:iam::123456789012:role/shop-api"
aggrega.ai/boundary-path: shop
aggrega.ai/recipe-uri: "aws-irsa:1.0.0"
[… omitted: the labels, which carry the version of the aggrega binary that built the file]

注入或手寫:同樣的檔案

release-by-hand.yaml 就是 release.yaml,只是原則所注入的註記改為手動寫在每個元件上。在沒有附加任何原則的情況下建置時,

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

它寫出的十二個檔案,與附加原則(於任一引擎)的 release.yaml 逐位元組相同。該比較不排除任何東西:註記是否為注入,並未記錄在任何渲染出的檔案中。check.sh 每次執行都會比較這些目錄樹。

建置讀取注入的值,與讀取寫在 Release 中的同一個值完全一樣,因此該值會展開成同一個附加項目。

注入記錄在哪裡

建置圖會記錄這項注入,--ir-graph 會印出該圖。每個收到注入註記的元件,其 Release 註記中都帶有 aggrega.ai/injected-by,指出是哪個外掛程式。執行 aggrega build --release release.yaml --config governance-rego.yaml --auto-pull --ir-graph 之後,api 的內容如下:

"release_annotations": {
"aggrega.ai/namespace": "shop",
"aws-role": "arn:aws:iam::123456789012:role/shop-api",
"aggrega.ai/injected-by": "governance/rego"
},

記錄是以元件為單位,而不是以鍵為單位:gateway 也帶有它,是為了它的 aws-role。註記所展開成的每個附加項目,在圖中都有 "origin": "annotation",就像手寫的註記會有的那樣。

Helm 目標不渲染 aggrega.ai/injected-by 註記,而且它會明說。在這次建置中,每個收到注入的元件都會為輸出增加三則警告。api 的第一則內容如下:

warning: node "api" (provider "helm"): target "helm" does not support annotation aggrega.ai/injected-by on synthesized input and ignored it

--no-unsupported-field-warnings 會使這類警告不再顯示,並且不改變建置所寫出的任何東西。

該角色只新增註記,不新增其他東西

一項決定指名一個元件、一個註記鍵與它的值。決定中沒有任何東西能新增元件、變更標籤,或直接附加 Feature。原則只能透過 Manifest 所繫結的鍵觸及 Feature,而哪一個 Feature 則由 Manifest 決定。

決定也不能移除註記。它的值是字串或子鍵物件,而 null 會被拒絕並指名該注入,因為 null 是作者讓元件退出其本會繼承之註記的方式。

繫結分送三個子鍵之處卻給了字串

governance-monitoring-as-string.yaml 附加 policies/monitoring-as-string.rego,它在監控鍵之下注入字串 "30s"。該繫結分送三個子鍵,而字串一個也沒有指名,因此

Terminal window
aggrega build --release release.yaml --config governance-monitoring-as-string.yaml --auto-pull

會中止、以 1 結束,且不寫出 dist/。原則在 api 與 worker 上都注入了該字串,而兩者都被拒絕。每次拒絕都指名注入該值的外掛程式以及它所附加的角色,因此該值可追溯到決定它的原則,而不是追溯到 Release 中一行沒有人寫過的內容:

[… omitted: a warning that the attachment is in the global configuration named with --config]
[OPS_ERROR E0367] .spec.boundaries.shop.components.api.annotations
a scalar annotation value does not route to exactly one path of its binding (Release shop-production, at .spec.boundaries.shop.components.api.annotations, "feature.aggrega.ai/monitoring", carrying the bare value "30s", whose binding routes "interval, port, path", whose value plugin "governance/rego" injected at governance role "annotation-transform")
---
[OPS_ERROR E0367] .spec.boundaries.shop.components.worker.annotations
a scalar annotation value does not route to exactly one path of its binding (Release shop-production, at .spec.boundaries.shop.components.worker.annotations, "feature.aggrega.ai/monitoring", carrying the bare value "30s", whose binding routes "interval, port, path", whose value plugin "governance/rego" injected at governance role "annotation-transform")

沒有原則時

在沒有治理附加項目的情況下建置 release.yaml,

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

會中止。它的 Component 需要一個 core/prometheus-exporter 附加項目,而沒有任何東西給 api 或 worker 一個:

[DEVELOPER_ERROR E0162] aggregate.yaml at .spec.components.api
missing required feature: attach a Feature of the required type to the component ("core/prometheus-exporter", required by component type "example/compute/service", under the requirement label "monitoring")
---
[DEVELOPER_ERROR E0162] aggregate.yaml at .spec.components.worker
missing required feature: attach a Feature of the required type to the component ("core/prometheus-exporter", required by component type "example/compute/service", under the requirement label "monitoring")

gateway 沒有被指名,因為它的 Release 項目自己寫了監控。附加原則之後,同一個 Release 就能建置:注入的註記所展開成的附加項目滿足該需求,就像手寫的那一個一樣。

鎖定檔記錄了什麼

annotation-transform 附加項目會改變建置所渲染的內容,因此鎖定檔會固定它。在 Rego 建置之後,release.lock 在 mutators: 之下帶有該附加項目,包含其外掛程式、其角色,以及其原則檔的內容與摘要:

mutators:
- plugin: governance/rego
role: annotation-transform
sources:
- content: |
[… omitted: the policy file, as written]
digest: sha256:9779faf366fc131b1a56ea68ccbae811173e3642943909f8d4ca63c00df8781b
path: platform-defaults.rego

它也在 plugins: 之下固定該外掛程式:

plugins:
- uri: governance/rego
[… omitted: the artifact digest, which depends on the aggrega binary that built it, recorded twice]
referenced_from:
- governance/rego:0.9.0

附加項目指名的是 hub 所發布的版本,因此該固定項目會透過工作區所讀取的登錄庫解析,或從該發行版本的 aggrega 二進位檔解析。

草擬範例

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

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

Terminal window
aggrega draft --example policy-annotation-transform

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

Terminal window
cd policy-annotation-transform

建置

Terminal window
aggrega build --release release.yaml --config governance-rego.yaml --auto-pull
aggrega build --release release.yaml --config governance-cel.yaml --auto-pull
aggrega build --release release-by-hand.yaml --auto-pull

標準程式庫目錄在此提供每個 Component、Feature、Recipe 與 FeatureRecipe,以及兩個引擎的 Plugin 實體。 它們從 hub.aggrega.ai 解析,也就是 .aggrega-config.yaml 所指名的預設登錄庫。以 --catalog 指名目錄的簽出副本(取得方式)時,不會從登錄庫擷取任何東西,因此建置可離線執行。--auto-pull 會在建置期間填入每個 Release 的鎖定檔。鎖定檔與渲染出的輸出是產生出來的,不隨附發布。

搭配 AI 助理

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

只使用 Aggrega 工具,且不要自己建立任何資料夾。如果工作區資料夾中還沒有 `policy-annotation-transform` 資料夾,就呼叫一次 `draft` 工具,將 `example` 設為 `policy-annotation-transform` 且不給 `dir`:它會把範例寫入新的 `policy-annotation-transform` 資料夾。接著呼叫 `cli-build` 工具,它會執行 `aggrega build` 並接受其 `--config` 旗標,將 `workspace_root` 設為 `policy-annotation-transform`、`release` 設為 `release.yaml`、`auto_pull` 設為 true、`config` 設為 `governance-rego.yaml`;然後以同樣方式再呼叫一次,將 `config` 設為 `governance-cel.yaml`。最後呼叫 `build` 工具,將 `workspace_root` 設為 `policy-annotation-transform`、`release` 設為 `release-by-hand.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `policy-annotation-transform/dist/` 之下。

檢查每次建置

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

Terminal window
./check.sh

check.sh 透過 build-release 輔助程式進行五次建置,並將每個結果與本頁所述的結果比對:

  • 未附加原則的 release-by-hand.yaml 是參考基準。它必須渲染出註記所展開成的六個檔案。
  • 未附加原則的 release.yaml 必須以 E0162 中止,針對 api 與 worker,且不針對其他任何元件。
  • 附加原則的 release.yaml 在每個引擎上各建置一次。每次建置都必須逐位元組寫出參考基準的檔案、在其鎖定檔中固定該外掛程式,並記錄三個元件都是由該外掛程式注入。
  • 帶有刻意植入字串的 release.yaml 必須以上述拒絕中止。

它每次建置印出一行,接著是兩個繫結所渲染的檔案,若有任何結果不同則以 1 結束,並印出該次建置的輸出:

POLICY RELEASE EXIT EXPECTED VERDICT
----------------------------------------------------------------------------------------------------
none release-by-hand.yaml 0 reference OK: the six attachments' files
none release.yaml 1 halt OK: E0162 for api and worker
rego release.yaml 0 pass OK: byte-identical to the build by hand; pinned; injected
cel release.yaml 0 pass OK: byte-identical to the build by hand; pinned; injected
string release.yaml 1 halt OK: refused, naming the plugin and its role
files the two annotation bindings rendered, release.yaml with governance-rego.yaml:
shop/helm/api--aws-role--api-serviceaccount-shop-api-aws-role.yaml
shop/helm/api--feature-zdaggrega-zdai-zsmonitoring--api-servicemonitor-shop-api-scrape.yaml
shop/helm/gateway--aws-role--gateway-serviceaccount-shop-gateway-aws-role.yaml
shop/helm/gateway--feature-zdaggrega-zdai-zsmonitoring--gateway-servicemonitor-shop-gateway-scrape.yaml
shop/helm/worker--aws-role--worker-serviceaccount-shop-worker-aws-role.yaml
shop/helm/worker--feature-zdaggrega-zdai-zsmonitoring--worker-servicemonitor-shop-worker-scrape.yaml
check: every build ended as expected.

每次建置都在此工作區自己的副本中執行,副本建立於暫存目錄,其 .aggrega-config.yaml 中的目錄路徑被改成絕對路徑,使它在那裡仍能解析。此目錄中沒有任何東西被寫入。它使用 build-release 輔助程式所解析的二進位檔,因此由 AGGREGA_BIN 選定,而 -- 之後的引數會傳給每次建置。它的結束狀態就是整個判定。

編譯器不解讀的地方

編譯器把元件與已繫結的鍵交給原則。它加入原則所決定的註記,並透過 Manifest 的繫結展開它們,就像展開任何註記一樣。它不知道服務應該被監控、抓取間隔是什麼,也不知道元件應該以哪個 IAM 角色執行:那些意義屬於原則與 Manifest。關於角色、酬載與決定鍵,請見治理插槽。關於沒有原則的註記展開,請見 annotation-driven-monitoring。

檔案

  • aggregate.yaml — 三個服務,沒有註記。
  • manifest.yaml — 將元件型別與兩個 Features 繫結到其 Helm Recipes,並將兩個註記鍵繫結到兩個 Features。
  • release.yaml — 原則所註記的 Release;只有 gateway 寫了註記。
  • release-by-hand.yaml — 對照組:每個註記都是手寫的。
  • .aggrega-config.yaml — 預設登錄庫;沒有治理附加項目。
  • governance-rego.yaml、governance-cel.yaml — 透過各引擎在 annotation-transform 附加的原則,各以 --config 指名。
  • governance-monitoring-as-string.yaml — 刻意植入的錯誤,以 --config 指名。
  • policies/platform-defaults.rego、policies/platform-defaults.cel — 原則,每個引擎各一份。
  • policies/monitoring-as-string.rego — 刻意植入的錯誤。
  • build-release.sh — 建置一個 Release,並回顯等效的 aggrega build 命令。
  • check.sh — 進行五次建置並檢查每個結果。