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

內建的標記外掛程式

兩個編譯進 CLI 的治理外掛程式,無須撰寫原則即可在建置的節點上寫入標籤:各自讀取什麼、各自寫入什麼,以及各自的附加範例與其所寫入的標籤。

CLI 隨附兩個僅憑設定就能塑造標籤的治理外掛程式,不需要撰寫任何原則語言:

Plugin寫入內容
governance/label-injector規則清單在每個標籤符合該規則的節點上設定的標籤
governance/selector-tagger一個標籤 tag,其值由 Component 名稱的對應表提供給每個節點

兩者都像任何治理外掛程式一樣,透過工作區設定中的 plugins.governance 附加(請參閱附加原則),而且兩者都列在內嵌外掛程式之中。本頁的附加範例指名 0.9.0,這是各外掛程式的 Plugin 實體在標準程式庫目錄中的版本;建置所提供的是編譯進 CLI 的外掛程式。

兩者的共通點

它們只服務 label-transform,不服務其他角色。 兩者都在標籤塑形角色回報標籤變更,時機在建置圖建立之後、被判定之前(請參閱轉換角色)。附加到任何其他角色時,兩者都會在執行之前遭到拒絕,因為它們沒有宣告該角色的匯出。在 ir-verdict,建置會中止並顯示:

[E0085]
ir-verdict: plugin "governance/label-injector:0.9.0": call "verdict_ir": wasm: plugin "label-injector" does not declare export "verdict_ir" in describe(): wasm: plugin rejected by the sandbox
[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

在 render-verdict 和 annotation-transform,拒絕的是同一個 E0085,只是改為指名 verdict_render 和 transform_annotations。

它們只讀取節點的標籤及其 Component,不讀取其他東西。 節點的標籤是外掛程式執行時它所帶有的那些:它的 Component 自己的標籤,以及排在它之前的任何轉換所寫入的標籤。它的 Component 是 Aggregate 的 type: 賦予它的名稱,例如 core/datastore/mariadb。兩個外掛程式都不讀取節點的值、Release 或其環境:兩者在節點上寫入的內容,僅取決於該節點的標籤和 Component。

它們寫入標籤,而且只寫入標籤。 兩個外掛程式回報的每項變更,都是為建置中某個節點指名一個標籤,並附上字串值。寫入節點已經帶有的標籤會取代其值——包括 Component 自己的標籤。兩者都不會移除標籤,也都無法變更值、節點的 Component 或版本,或節點的集合:標籤塑形角色不提供這類變更(請參閱塑造標籤)。

它們的設定在執行之前不會經過檢查。 兩者都沒有為其 settings 宣告結構描述,所以建置會照原樣把設定交給外掛程式。外掛程式不讀取的鍵會被忽略——拼錯的 rules 什麼也不會設定,建置也會完成——而它所讀取的鍵若持有其他型別的值,則會使建置中止,並顯示一則訊息,依序指名外掛程式、decode settings: 和原因。

它們以 YAML 讀取 sources 之下的檔案,依檔名順序。 外掛程式收到的檔案會依檔名排序,不論附加項目以什麼順序列出它們。不是有效 YAML 的檔案會使建置中止,並顯示一則訊息,依序指名外掛程式、parse source "<file>": 和剖析器給出的原因;內容不是外掛程式所讀取形狀的檔案,則以 decode source "<file>": 和原因使其中止。

鎖定檔保存建置所套用的輸入。 解析 Release 鎖定檔的 pull——aggrega pull,或帶有 --auto-pull 的建置——會記錄每個附加項目的設定及其各檔案的內容,而建置會套用所記錄的內容。編輯附加項目的設定或其中一個檔案之後,要再次解析鎖定檔;沒有這麼做的建置會持續寫入所記錄輸入給出的標籤。

附加時沒有 with: 區塊,兩者都不寫入任何東西,而且建置會完成。

在 label-transform 的多個附加項目會依列出的順序執行,每一個都讀取排在它之前的項目所寫入的標籤,所以規則可以比對較早的附加項目所設定的標籤。examples/policy-label-transform/ 在一個 Rego 原則之後附加這兩個外掛程式,以及一個讀取三者所寫內容的判定。

governance/label-injector

標籤注入器會把標籤加到每個標籤符合規則的節點上。

標籤注入器的設定

設定型別預設效果
rules規則清單無套用到每個節點的規則;沒有規則時,外掛程式不寫入任何東西

每條規則帶有兩個從標籤鍵對應到字串值的對應表:

鍵預設效果
match沒有配對節點帶有列出的每一組配對且各自符合該值時即相符;沒有配對的規則會符合每個節點
set沒有配對每一組配對都會寫入規則所符合的每個節點

節點沒有帶有的標籤,比較時視為空字串,所以值為 "" 的 match 配對也會符合沒有帶有該鍵的節點。

規則會依列出的順序套用到每個節點,而之後的規則為同一個鍵所寫入的標籤,會取代較早的規則所寫入的值。

規則從何而來

當設定中至少有一條規則時,外掛程式從其設定讀取 rules。當設定中沒有任何規則時,它會依檔名順序讀取 sources 之下的檔案,並採用第一個頂層 rules 清單中至少有一條規則的檔案的規則,形狀相同:

rules:
- match: {}
set:
ops.example.com/owner: shop-team

之後任何檔案的規則都不會被讀取。排在它之前、無法剖析的檔案會使建置中止。

已附加的標籤注入器,以及它所寫入的標籤

這個附加項目先為每個節點設定擁有者,然後為 datastore 類別中的節點設定備份排程與不同的擁有者:

plugins:
governance:
- uri: governance/label-injector:0.9.0
role: label-transform
with:
settings:
rules:
- match: {}
set:
ops.example.com/owner: shop-team
- match:
component.aggrega.ai/category: datastore
set:
backup.example.com/schedule: nightly
ops.example.com/owner: data-team

單獨附加到 examples/policy-label-transform/ 的 Aggregate——db 是 core/datastore/mariadb,cache 是 core/datastore/redis,web 是 core/devtool/adminer——aggrega build --release release-production.yaml --auto-pull --ir-graph 會印出一個建置圖,其節點帶有:

"db": {
"labels": {
"component.aggrega.ai/type": "stateful-server",
"component.aggrega.ai/category": "datastore",
"component.aggrega.ai/upstream-licensing": "osi-approved",
"backup.example.com/schedule": "nightly",
"ops.example.com/owner": "data-team"
},
[… omitted: the node's Component, its version and the rest of the node]
},
"cache": {
"labels": {
"component.aggrega.ai/type": "server",
"component.aggrega.ai/category": "datastore",
"datastore.aggrega.ai/engine": "kv",
"component.aggrega.ai/hosting": "mixed",
"component.aggrega.ai/upstream-licensing": "mixed",
"backup.example.com/schedule": "nightly",
"ops.example.com/owner": "data-team"
},
[… omitted: the node's Component, its version and the rest of the node]
},
"web": {
"labels": {
"component.aggrega.ai/type": "server",
"component.aggrega.ai/category": "devtool",
"component.aggrega.ai/upstream-licensing": "osi-approved",
"ops.example.com/owner": "shop-team"
},
[… omitted: the node's Component, its version and the rest of the node]
}

每個節點都符合第一條規則並取得 shop-team;位於 datastore 類別的 db 和 cache 也符合第二條規則,它把該值取代為 data-team。其餘每個標籤都是 Component 自己的。

governance/selector-tagger

選擇器標記器在每個節點上寫入一個 tag 標籤,其值依節點的 Component 查得。

選擇器標記器的對應表與設定

它一律寫入鍵 tag;該鍵不可設定。

對應表來自 sources 之下的檔案。每個檔案都是一個從 Component 名稱對應到其節點所取標記的 YAML 對應表:

core/datastore/mariadb: orders
core/datastore/redis: sessions

每個檔案都會依檔名順序讀取,後面檔案中的項目會取代較早檔案中同一個 Component 的項目。每個標記都是字串:YAML 讀成其他型別的值,例如 3,會使建置中止,所以要加上引號——"3"。

設定型別預設效果
default字串空當對應表沒有列出節點的 Component 時,該節點所取的標記;為空時,這樣的節點不會有 tag

沒有 sources 時,每個節點都取 default。

已附加的選擇器標記器,以及它所寫入的標籤

這個附加項目從 tags.yaml(上面的對應表)為資料庫與快取加上標記,並為其他每個節點加上 untagged:

plugins:
governance:
- uri: governance/selector-tagger:0.9.0
role: label-transform
with:
sources:
- tags.yaml
settings:
default: untagged

單獨附加到同一個 Aggregate 時,aggrega build --release release-production.yaml --auto-pull --ir-graph 會印出一個建置圖,其節點帶有:

"db": {
"labels": {
"component.aggrega.ai/type": "stateful-server",
"component.aggrega.ai/category": "datastore",
"component.aggrega.ai/upstream-licensing": "osi-approved",
"tag": "orders"
},
[… omitted: the node's Component, its version and the rest of the node]
},
"cache": {
"labels": {
"component.aggrega.ai/type": "server",
"component.aggrega.ai/category": "datastore",
"datastore.aggrega.ai/engine": "kv",
"component.aggrega.ai/hosting": "mixed",
"component.aggrega.ai/upstream-licensing": "mixed",
"tag": "sessions"
},
[… omitted: the node's Component, its version and the rest of the node]
},
"web": {
"labels": {
"component.aggrega.ai/type": "server",
"component.aggrega.ai/category": "devtool",
"component.aggrega.ai/upstream-licensing": "osi-approved",
"tag": "untagged"
},
[… omitted: the node's Component, its version and the rest of the node]
}

core/devtool/adminer 不在對應表中,所以 web 取了 default。沒有 default 設定時,web 不帶有 tag,而其他兩個則如上所述加上標記。

編譯器不解讀的地方

兩個外掛程式和編譯器都不知道擁有者、備份排程或標記是什麼意思。標籤鍵和值是附加項目提供的字串;編譯器把節點交給每個外掛程式、套用它回報的標籤,並把它們攜帶在建置圖中,由讀取它們的原則賦予它們意義。