塑造標籤的原則:每個環境一個支援層級,以及讀取它的判定
治理外掛程式為此工作區部署的每個元件加上標籤:其擁有者、其環境所對應的支援層級、其備份排程與一個簡短的標記,使任何開發者都不必輸入其中任何一項。這些標籤是在建置期間,由四個治理附加項目推導出來的:
- 三個位於
label-transform角色,塑造建置節點上的標籤:一個讀取 Release 環境的 Rego 原則,以及編譯進 CLI 的兩個標記外掛程式,label-injector與selector-tagger; - 一個位於
ir-verdict角色,是針對建置圖的 Rego 原則,會中止其中有節點沒有支援層級的正式環境建置。
同一個 Aggregate 的兩個 Releases 在環境上不同,staging 與 production,而它們的節點最後帶有不同的標籤。第三次建置由 check.sh 從此工作區的副本進行,該副本的原則少涵蓋一個類別,它會中止並指名被遺漏的節點。
它建置什麼
| 實例 | 目錄 Component | 它在這裡是什麼 |
|---|---|---|
db | core/datastore/mariadb | 商店的資料庫 |
cache | core/datastore/redis | 它的工作階段快取 |
web | core/devtool/adminer | 連接到 db 的資料庫管理 UI |
三者都編譯成 Docker Compose,輸出到 dist/compose/docker-compose.yaml。Aggregate 與兩個 Releases 都沒有指名自己的標籤:下面每個標籤都是由附加項目寫入的。
四個附加項目
它們附加在此工作區的 .aggrega-config.yaml 中、plugins.governance 之下,因此在這裡執行的每次建置都會套用它們:
| 外掛程式 | 角色 | 它寫入什麼 | |
|---|---|---|---|
| 1 | governance/rego:0.9.0,搭配 policies/support-tier.rego | label-transform | 每個節點上的 ops.example.com/environment;其設定所列類別的每個節點上的 ops.example.com/support-tier |
| 2 | governance/label-injector:0.9.0 | label-transform | 每個節點上的 ops.example.com/owner;datastore 類別中每個節點上的 backup.example.com/schedule |
| 3 | governance/selector-tagger:0.9.0 | label-transform | tag,來自 policies/tags.yaml 中的對應表 |
| 4 | governance/rego:0.9.0,搭配 policies/production-support.rego | ir-verdict | 什麼都不寫:它使建置中止,或讓建置完成 |
0.9.0 是標準程式庫目錄中三個 Plugin 實體的版本;外掛程式本身已編譯進 CLI,建置時由 CLI 提供。aggrega build --release release-production.yaml --auto-pull --detail 會為這三者各列出這兩項:
[… omitted: the line counting what the pull resolved, and the rows for the Components, the Recipes, the Feature and the FeatureRecipes, each read from the registry] in-binary Plugin governance/label-injector ← governance/label-injector:0.9.0 in-binary Plugin governance/label-injector:0.9.0 ← .aggrega-config.yaml.plugins.governance in-binary Plugin governance/rego ← governance/rego:0.9.0 in-binary Plugin governance/rego:0.9.0 ← .aggrega-config.yaml.plugins.governance in-binary Plugin governance/selector-tagger ← governance/selector-tagger:0.9.0 in-binary Plugin governance/selector-tagger:0.9.0 ← .aggrega-config.yaml.plugins.governance[… omitted: the rows for this workspace's Aggregate and Manifest, the pull's summary, a lint notice about core/secret-value, and the build's summary]同一角色上的附加項目依列出的順序執行,而每一個都會讀取它之前那些所寫的標籤:標籤注入器收到 Rego 原則所寫的兩個標籤,而選擇器標記器收到那兩個以及標籤注入器所寫的。判定在三者之後,針對它們留下的圖執行。
label-transform 原則讀取什麼
位於 label-transform 的原則讀取 input.graph.nodes 的節點,以節點 id 為鍵,每個節點帶有:
component— 該節點所實例化的 Component,例如core/datastore/mariadb;resolved_version— 該 Component 解析到的版本;labels— 原則執行時節點所帶的標籤:Component 自己的,以及任何較早的轉換所寫的;values— 節點的實例在 Aggregate 中宣告的值,沒有宣告時則不存在。這裡只有db宣告了一個,database: appdb。
它在 input.compilation_context 讀取 Release:release_name 與 environment。這就是讓一個原則能針對每個環境作出不同決定的原因。
位於 ir-verdict 的原則改為讀取整個建置圖,而它的節點深了一層,位於 input.graph.spec.nodes。從一個角色搬到另一個角色的規則是要編輯的,而不是複製的:路徑錯誤時,它什麼都讀不到,也永遠不會觸發。ir-verdict 酬載不帶 compilation_context,因此判定無法從 Release 讀取環境。這就是此處第一個原則也在每個節點上寫入 ops.example.com/environment 的原因:判定從該標籤讀取環境。
要看到酬載而不只是讀到關於它們的說明,先建置一次讓鎖定檔存在,然後:
aggrega build --release release-production.yaml --dry-run --policy-input payloads會把每個附加項目所收到的酬載寫入 payloads/,在同一角色上有多個附加項目收到不同酬載時,每個附加項目一個檔案——label-transform.1.rego.json、label-transform.2.label-injector.json、label-transform.3.selector-tagger.json——並為其他每個角色各一個檔案,且不寫入其他任何東西。
Rego 原則
policies/support-tier.rego 在 labels 之下收集它的決定,每個標籤一項,指名節點、標籤鍵與值:
env := input.compilation_context.environment
labels contains {"node": id, "key": "ops.example.com/environment", "value": env} if { some id, _ in input.graph.nodes}
labels contains {"node": id, "key": "ops.example.com/support-tier", "value": data.support_tiers[env]} if { some id, node in input.graph.nodes node.labels["component.aggrega.ai/category"] in data.supported_categories}它的設定是原則的 data 文件,其中存放企業環境的詞彙:
settings: # The categories whose components get a support tier. supported_categories: [datastore, devtool] # The tier each environment gives them. support_tiers: production: on-call staging: office-hours每個內建項目寫入什麼
label-injector 套用一份規則清單。節點的標籤若包含某規則 match 之下的每一組鍵值對,就會收到該規則 set 之下的每一組鍵值對,這些鍵值對會被加入,或取代節點已帶有的同名標籤的值。沒有任何鍵值對的 match 會符合每個節點。它從 rules 設定讀取規則,如此處所示;若設定中沒有規則,則從 sources 之下第一個帶有規則的檔案讀取。這裡的兩條規則讓每個節點都得到 ops.example.com/owner: shop-team,並讓 db 和 cache(屬於 datastore 類別的兩個節點)得到 backup.example.com/schedule: nightly。web 屬於 devtool 類別,不會得到備份排程。
selector-tagger 寫入一個標籤,永遠在鍵 tag 之下。它的值來自 Component 名稱到標記的對應表,從 sources 之下的檔案讀取。對應表沒有列出其 Component 的節點,會採用 default 設定,而沒有 default 時,它就不帶 tag。policies/tags.yaml 對應資料庫與快取:
core/datastore/mariadb: orderscore/datastore/redis: sessions而附加項目的 default 是 untagged,因此 web 被標記為 untagged。
兩者都只憑節點的標籤與其 Component 來決定。兩者都不讀取環境,因此它們在兩個 Releases 中寫入相同的標籤。
兩者都只服務 label-transform。附加在 ir-verdict 時,任一個都會在執行前被拒絕;selector-tagger 附加在那裡會使建置中止,並顯示:
[E0085] ir-verdict: plugin "governance/selector-tagger:0.9.0": call "verdict_ir": wasm: plugin "selector-tagger" does not declare export "verdict_ir" in describe(): wasm: plugin rejected by the sandbox草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example policy-label-transform它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd policy-label-transform建置
aggrega build --release release-staging.yaml --auto-pullaggrega build --release release-production.yaml --auto-pull標準程式庫目錄在此提供每個 Component、Recipe 與 Feature,以及三個 Plugin 實體。 它們從 hub.aggrega.ai 解析,也就是 .aggrega-config.yaml 所指名的預設登錄庫。以 --catalog 指名目錄的簽出副本(取得方式)時,不會從登錄庫擷取任何東西,因此建置可離線執行。--auto-pull 會在建置期間填入每個 Release 的鎖定檔;鎖定檔與渲染出的輸出是產生出來的,不隨附發布。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自己建立任何資料夾。如果工作區資料夾中還沒有 `policy-label-transform` 資料夾,就呼叫一次 `draft` 工具,將 `example` 設為 `policy-label-transform` 且不給 `dir`:它會把範例寫入新的 `policy-label-transform` 資料夾。接著為每個 Release 各呼叫一次 `build` 工具,依此順序——`release-staging.yaml` 與 `release-production.yaml`——每次都將 `workspace_root` 設為 `policy-label-transform`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `policy-label-transform/dist/` 之下。每個節點最後帶有的標籤
--ir-graph 把建置圖印在標準輸出上,位於建置概要之前,而該圖帶有每個節點被轉換留下的標籤。在 aggrega build --release release-production.yaml --auto-pull --ir-graph 之後,web 的內容如下:
"web": { "labels": { "component.aggrega.ai/type": "server", "component.aggrega.ai/category": "devtool", "component.aggrega.ai/upstream-licensing": "osi-approved", "ops.example.com/environment": "production", "ops.example.com/support-tier": "on-call", "ops.example.com/owner": "shop-team", "tag": "untagged" }, [… omitted: the node's Component, its version, where it is placed, its catalog metadata, the file it was declared in, and its connection to db]}前三個標籤是 Component 自己的;後四個是由三個轉換所寫。check.sh 讀取每次建置的圖,並列出附加項目所寫的標籤:
labels the attachments wrote, release-staging.yaml: db ops.example.com/environment: staging ops.example.com/support-tier: office-hours ops.example.com/owner: shop-team backup.example.com/schedule: nightly tag: orders cache ops.example.com/environment: staging ops.example.com/support-tier: office-hours ops.example.com/owner: shop-team backup.example.com/schedule: nightly tag: sessions web ops.example.com/environment: staging ops.example.com/support-tier: office-hours ops.example.com/owner: shop-team tag: untagged
labels the attachments wrote, release-production.yaml: db ops.example.com/environment: production ops.example.com/support-tier: on-call ops.example.com/owner: shop-team backup.example.com/schedule: nightly tag: orders cache ops.example.com/environment: production ops.example.com/support-tier: on-call ops.example.com/owner: shop-team backup.example.com/schedule: nightly tag: sessions web ops.example.com/environment: production ops.example.com/support-tier: on-call ops.example.com/owner: shop-team tag: untagged兩個 Releases 之間只有 Rego 原則的兩個標籤不同。
建置填好鎖定檔之後,aggrega inspect release-production.yaml --ir-graph 不需建置就能印出同一張圖,且排在 Release 的可讀檢視之前,因為轉換會從鎖定檔讀取它們的檔案。沒有鎖定檔時,它印出的圖不帶有那些檔案所決定的任何標籤:沒有 ops.example.com/environment 或 ops.example.com/support-tier,且每個節點都是 tag: untagged。
轉換只改變標籤,不改變其他任何東西
label-transform 上的決定指名一個節點、一個標籤鍵與它的值,或要求移除一個標籤。沒有任何決定能新增節點、變更節點的 Component 或版本,或觸及它的值,因此轉換無法表達其中任何一項。指名了建置中不存在之節點的決定不會被忽略:建置會以 E0069 中止,並指名它無法設定的節點與標籤。
release-production.yaml 的兩次建置,一次有這些附加項目、一次沒有,顯示了這一點。它們的建置圖只在上面列出的標籤上不同,沒有其他差異。它們渲染出的輸出完全沒有差異:這裡的 Compose Recipes 不渲染這些標籤中的任何一個,所以 dist/compose/docker-compose.yaml 不論有沒有附加項目,都是逐位元組相同的檔案,而 check.sh 每次執行都會比較兩個目錄樹。標籤存在於建置圖中,判定就在那裡讀取它們。
判定,以及它所中止的建置
policies/production-support.rego 附加在 ir-verdict,位於轉換之後,並讀取它們所寫的標籤:
deny contains msg if { some id, node in input.graph.spec.nodes node.labels["ops.example.com/environment"] == "production" not node.labels["ops.example.com/support-tier"] msg := sprintf("%v is built for production and carries no ops.example.com/support-tier label", [id])}兩個 Releases 都能建置:在預備環境中判定不要求任何東西,而在正式環境中每個節點都帶有層級。縮小原則的涵蓋範圍,判定就會抓到被遺漏的節點。當 supported_categories 在 .aggrega-config.yaml 中寫成 [datastore] 時,web 得不到支援層級,而 aggrega build --release release-production.yaml --auto-pull 會中止:
[… omitted: the pull's lines, which carry the refusal below once more, and a lint notice about core/secret-value][POLICY_ERROR E0005] policy plugin "rego" rejected the build: web is built for production and carries no ops.example.com/support-tier label caused by: policy violation建置以 1 結束,且不寫出 dist/。release-staging.yaml 在縮小的原則下仍然能建置:它的節點被標上 staging,而判定對它們沒有任何要求。check.sh 在工作區的副本中進行這次建置,因此這裡不需要編輯任何東西就能看到它。
判定只接受或拒絕,不做其他任何事:它無法加上缺少的標籤來讓建置通過。正式環境的節點必須帶有哪個標籤,是企業環境的規則,寫在它的原則中;編譯器把標籤以字串形式帶入圖中,不賦予它們任何意義。
鎖定檔記錄了什麼
鎖定檔以不同方式記錄這兩種附加項目。在一次正式環境建置之後,release-production.lock 在 mutators: 之下帶有每個轉換附加項目——它的外掛程式、角色、設定,以及它每個檔案的內容與摘要——並在 plugins: 之下固定每個轉換外掛程式:
plugins: - uri: governance/label-injector [… omitted: the artifact digest, which depends on the aggrega binary that built it, recorded twice] referenced_from: - governance/label-injector:0.9.0 - uri: governance/rego [… omitted: the artifact digest, recorded twice] referenced_from: - governance/rego:0.9.0 - uri: governance/selector-tagger [… omitted: the artifact digest, recorded twice] referenced_from: - governance/selector-tagger:0.9.0判定附加項目只出現在 verdict_inputs: 之下,以其檔案的 sha256 表示:
verdict_inputs: - plugin: governance/rego:0.9.0 role: ir-verdict sources: - digest: sha256:5e01fc76f791ef00eba02f02ffb383437c87531f3927ee4a91cec9bf9c6e8cd8 path: production-support.rego判定附加項目不會被固定。只附加判定時——三個轉換從 .aggrega-config.yaml 中移除——鎖定檔帶有 mutators: [] 與 plugins: [],以及 verdict_inputs: 之下判定的檔案。轉換會改變管線其餘部分所依據的建置,所以鎖定檔保存做出該變更的外掛程式;判定只決定建置是否完成。
檢查每次建置
這個自我檢查在文件存放庫的檢出中執行,其中有它所使用的共用建置輔助程式;aggrega draft --example 所寫出的副本不帶該輔助程式。
./check.shcheck.sh 透過 build-release 輔助程式進行四次建置,並將每個結果與本頁所述的結果比對:
- 在沒有治理附加項目的此工作區副本中的
release-production.yaml,作為參考基準; - 此處的
release-staging.yaml與release-production.yaml,兩者都必須完成,且每個節點帶有上面列出的標籤,正式環境的目錄樹與參考基準逐位元組相同; - 在原則只涵蓋
datastore類別的副本中的release-production.yaml,它必須以E0005與上述句子中止,旁邊沒有其他拒絕。
它每次建置印出一行,接著是標籤,若有任何結果不同則以 1 結束,並印出該次建置的輸出:
RELEASE GOVERNANCE EXIT EXPECTED VERDICT----------------------------------------------------------------------------------------------------release-production.yaml none 0 reference OKrelease-staging.yaml as attached 0 pass OK: the expected labelsrelease-production.yaml as attached 0 pass OK: the expected labels; byte-identical to the build with nonerelease-production.yaml narrowed 1 halt OK: E0005, the expected sentence[… omitted: the two label listings shown above]check: every build ended as expected.兩個副本建立於暫存目錄,其 .aggrega-config.yaml 中的目錄路徑被改成絕對路徑,使它在那裡仍能解析。此目錄中沒有任何檔案被編輯;在這裡進行的兩次建置會填入它的鎖定檔,就像任何建置一樣。它使用 ./build-release.sh 所解析的二進位檔,因此由 AGGREGA_BIN 選定,而 -- 之後的引數會傳給每次建置。只要此目錄中有 dist/,它就拒絕啟動,因為它要比較各次建置所寫出的目錄樹,而且它不會留下任何東西。它的結束狀態就是整個判定。
編譯器不解讀的地方
編譯器把節點與 Release 的環境交給每個轉換,套用它所決定的標籤,並把結果交給判定。它不知道支援層級是什麼、on-call 的等級高於 office-hours,或 devtool 需要一個支援層級:那些意義是原則的設定與規則。關於角色、酬載,以及原則如何指名標籤,請見治理插槽,而關於原則在產品中的位置,請見治理與合規。
檔案
aggregate.yaml— 資料庫、快取,以及連接到資料庫的管理 UI;沒有標籤。manifest.yaml— 將每個元件型別與機密 Feature 繫結到其 Docker Compose Recipe。由兩個 Releases 共用。release-staging.yaml、release-production.yaml— 兩個環境。.aggrega-config.yaml— 預設登錄庫與四個治理附加項目。policies/support-tier.rego— label-transform 原則。policies/production-support.rego— 判定。policies/tags.yaml— 選擇器標記器的元件到標記對應表。build-release.sh— 建置一個 Release,並回顯等效的aggrega build命令。check.sh— 進行四次建置並檢查每個結果。