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

部署目錄沒有的服務

當目錄中沒有雲端服務的 Component 時,廠商自己對該服務資源型別的描述可以代替它。aggrega import 會讀取該描述——雲端廠商為資源型別發布的結構描述——並由它草擬兩個檔案:一個 Component,在 Aggrega 讀取的支柱中宣告該型別的設定;以及一個 Recipe,為某個提供者實現該 Component,方式是只寫入一個該型別的資源。你檢閱這些草稿,把使用它們的內容行加到 Aggregate 與 Release 中,然後建置。

會讀取三種格式,各由為其服務的核心匯入器讀取:

格式你匯出的結構描述Recipe 的提供者
cloudformation-schemaCloudFormation 資源型別,來自 aws cloudformation describe-type 或提供者結構描述檔cloudformation
terraform-schemaTerraform 或 OpenTofu 提供者的結構描述,來自 terraform providers schema -jsonterraform
arm-schemaAzure 資源提供者的 Bicep 型別檔 types.jsonarm

本頁從頭到尾示範 Azure 的情形,接著展示 CloudFormation 與 Terraform 的情形,以及為目錄已有的 Component 只草擬 Recipe 的變體。下方 Azure 部分的命令與輸出,來自一次執行,其所用的是帶有這些匯入器與 arm 提供者的編譯器發行版本的建置。

不會替你擷取任何內容。你用廠商自己的工具匯出結構描述,所以匯入讀取的是你持有的檔案,不會連到任何雲端帳戶。

匯出結構描述

每個廠商的工具都會把結構描述寫到檔案。在工作區中,執行適用於你的雲端的那一個:

Terminal window
# CloudFormation: any registered type, third-party and private ones included.
aws cloudformation describe-type --type RESOURCE --type-name AWS::SQS::Queue > sqs-queue.json
# Terraform or OpenTofu: in a directory whose configuration requires the provider.
terraform init && terraform providers schema -json > schema.json
# Azure: no export is needed. Bicep's type files are published in the
# Azure/bicep-types-az repository, one for each resource provider and API
# version, at generated/<namespace>/<provider>/<apiVersion>/types.json.

下方的 Azure 範例使用 API 版本 2024-11-30 的 Microsoft.ManagedIdentity 檔案,放在名為 msi 的資料夾中,沿用 Bicep 為它取的名稱 types.json。

Azure 結構描述檔與其授權

2024-11-30 的 Microsoft.ManagedIdentity 之 types.json 是 Azure/bicep-types-az 存放庫中的一個檔案,著作權屬於 Microsoft Corporation,以 MIT License 散布。下方草稿所引用的描述屬於 Microsoft,授權的聲明適用於它們:

MIT License
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

工作區是一個資料夾,其中有含 workspace 鍵的 .aggrega-config.yaml,以及放著該檔案的 msi 資料夾:

.aggrega-config.yaml
msi/types.json

列出要草擬的型別

一個結構描述檔可以包含多個資源型別,而匯入只草擬你指名的那些。列出該檔案所包含的內容:

Terminal window
jq -r '.[] | select(."$type" == "ResourceType") | .name' msi/types.json
Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30
Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials@2024-11-30
Microsoft.ManagedIdentity/identities@2024-11-30

接著在 .aggrega-config.yaml 中匯入器項目的 resource_types 設定下,指名你要的那一個。每個工作區都已經提供該匯入器,所以只有要傳入 resource_types 時才需要這個項目。它的 uri 指名匯入器與編譯器的版本,後者由 aggrega version 印出;匯入器是透過這樣的項目取得它的設定,如哪個匯入器把來源轉成草稿所述:

workspace: {}
default_registry: hub.aggrega.ai
plugins:
importer:
- uri: importer/arm-schema:<compiler version>
with:
settings:
resource_types:
- Microsoft.ManagedIdentity/userAssignedIdentities

CloudFormation(AWS::SQS::Queue)與 Terraform(aws_sqs_queue)的名稱採精確比對,Azure 則不分大小寫,其名稱可以在 @ 之後帶有 API 版本。只包含一個型別的檔案不需要清單。匯入一個包含多個型別而沒有清單的檔案時,會停止並指出它們:

halt: the sources hold 3 resource types and the resource_types setting names none: write the types to draft in it, one of Microsoft.ManagedIdentity/userAssignedIdentities, Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials, Microsoft.ManagedIdentity/identities

列出的型別若檔案中沒有,匯入同樣會停止,並指出檔案中最接近的名稱。中止不會草擬任何內容,所以不會寫入任何檔案。

執行匯入

指名資料夾,或檔案:

Terminal window
aggrega import msi

在標準輸出上,它會列出所寫出的內容:

wrote catalog/component/imported/security/microsoft-managedidentity-userassignedidentities/0.1.0/component.yaml
wrote catalog/recipe/arm/microsoft-managedidentity-userassignedidentities/0.1.0/recipe.yaml
wrote .aggrega-import.json

在標準錯誤上,其中 <workspace> 代表你執行它所在的資料夾:

read as the arm-schema format, with drafted paths relative to <workspace>
mapped Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 (Microsoft.ManagedIdentity/userAssignedIdentities) to imported/security/microsoft-managedidentity-userassignedidentities as a draft, NONE at 0.05

aggrega import 會辨識名為 types.json 的 Bicep 型別檔,如同 Azure/bicep-types-az 為它們命名的方式,而 --format arm-schema 則為名稱不同的檔案指定格式。--format 也是匯入大型匯出檔的方式,例如提供者完整的 Terraform 結構描述,而不需要匯入先去辨識它。

mapped 行說明該資源型別成了自己的一份草稿,目錄中沒有接近它的 Component。草稿寫在 catalog/ 之下,這是工作區當作目錄讀取的資料夾,所以下一次驗證與建置時就會解析它們,不需要設定任何東西。記錄 .aggrega-import.json 列出它們,因此 aggrega import --sync 可以在結構描述變更時讓它們保持最新,而不會覆寫你的編輯。

草稿的內容

Component component.yaml 把資源型別的每個屬性歸入一個支柱,每個鍵旁的註解指出把它放在那裡的規則:

  • 讀回屬性,也就是由雲端設定而你無法設定的屬性,不是輸入。純量會成為使用端可以讀取的 provides 鍵。
  • 機密會被草擬為指向其值來源的參照,絕不是值,如下方的機密所述。
  • 放置位置,即資源所在之處,例如 Azure 的 location,會放進 infra,這是操作員為每個環境提供的支柱。
  • 身分,即資源本身的名稱,會放進 values,這是開發者撰寫的支柱。
  • 任何其他必要屬性會放進 infra。
  • 每個其他可寫屬性會放在 infra.overrides 之下,這是一個開放的對應表,保留廠商對每個名稱的原有拼法。

對這個資源型別,Component 內容如下:

apiVersion: aggrega.ai/v1beta1
kind: Component
metadata:
name: imported/security/microsoft-managedidentity-userassignedidentities
version: 0.1.0
description: "Draft Component of the resource type Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30, imported from a source in the arm-schema format."
labels:
component.aggrega.ai/type: resource
component.aggrega.ai/category: security
component.aggrega.ai/hosting: managed
spec:
schema:
values:
type: object
properties:
# identity: the resource's own name or identifier
# [vendor schema text] The resource name
name:
type: string
description: "[vendor schema text] The resource name"
required: true
additionalProperties: false
infra:
type: object
properties:
# placement: where the resource is deployed
# [vendor schema text] The geo-location where the resource lives
location:
type: string
description: "[vendor schema text] The geo-location where the resource lives"
required: true
# override: every writable property no other rule places, each under the vendor's own name
overrides:
type: object
description: "Writable settings of Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30, each under the vendor's own name, which the Recipe passes to the resource as written; its header lists the names it reads."
additionalProperties: true
additionalProperties: false
provides:
type: object
properties:
# read back: published under provides
# [vendor schema text] The resource id
id:
type: string
description: "[vendor schema text] The resource id"
# … client_id, principal_id and tenant_id, each read back the same way
additionalProperties: false

草稿從廠商結構描述取來的每段文字,都以 [vendor schema text] 開頭,讓你可以找到它們,並在 Component 離開你的工作區之前以你自己的話改寫。匯入自行撰寫的文字則沒有標記。

Recipe recipe.yaml 在 arm 提供者上實現該 Component。它的標頭註解列出它讀取的每個鍵,附上其規則與型別,以及它不讀取的內容:

spec:
# This Recipe realizes imported/security/microsoft-managedidentity-userassignedidentities with one resource of the type Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30. It reads the keys below, each with its type, its rule and the first sentence of the vendor's description; any other key under infra.overrides halts the build naming it.
# infra.location: string, required; placement: where the resource is deployed
# [… omitted: the first sentence of the vendor's description of the key above, marked as vendor schema text, as below each key]
# values.name: string, required; identity: the resource's own name or identifier
# [… omitted: the vendor's description of the key]
# infra.overrides.tags: object; override: writable, set under infra.overrides in the vendor's spelling
# [… omitted: the vendor's description of the key]
# infra.overrides.isolationScope: string; override: writable, set under infra.overrides in the vendor's spelling
# [… omitted: the vendor's description of the key, then a line saying template.yaml declares the resource under the symbolic name locals.symbol holds]
capability: arm
implements: imported/security/microsoft-managedidentity-userassignedidentities:0.1.0
locals:
# [… omitted: a comment saying what the symbolic name is made of]
symbol: "{{ capitalizedWords .context.instance_id }}"
provides:
# [… omitted: a comment saying each value is one whole ARM expression, evaluated when the template is deployed]
id: "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities'{{ range splitList \"/\" (toString (index .values \"name\")) }}, '{{ replace \"'\" \"''\" . }}'{{ end }})]"
client_id: "[reference('{{ .locals.symbol }}').clientId]"
# … principal_id and tenant_id the same way
assertions:
overrides_this_recipe_reads: |
# halts naming any key under infra.overrides the header does not list
artifacts:
template.yaml: |
# … the resource, written under the symbolic name locals.symbol holds

Recipe 只寫入一個資源,也就是該型別所指名的那個,沒有其他東西:沒有資源群組、沒有網路、沒有你未宣告的身分。每個 provides 值都是 Azure 在範本部署時評估的完整運算式,並以其原樣的字串傳遞給使用端。infra.overrides 中標頭未列出的鍵會讓建置中止並指名它,所以拼錯的設定會在部署任何東西之前被抓到。

建置之前請檢閱這兩個檔案。在支柱之間移動設定,是對 Component 與 Recipe 對它的讀取方式的編輯,而這些檔案屬於你:建置所依賴的內容沒有放在其他任何地方。

在 Aggregate 與 Release 中使用草稿

匯入不會草擬 Aggregate、Manifest 或 Release,所以使用草擬的 Component 的內容行由你撰寫。在 Aggregate aggregate.yaml 中,加入一個草擬型別的元件,帶有 values 支柱所持有的身分:

apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: identities
version: 1.0.0
spec:
components:
deploy-identity:
type: imported/security/microsoft-managedidentity-userassignedidentities:0.1.0
values:
name: deploy-identity

在 Manifest manifest.yaml 中,用一行說明由哪個 Recipe 實現該型別:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: identities
version: 1.0.0
spec:
mappings:
components:
type:
imported/security/microsoft-managedidentity-userassignedidentities:0.1.0: arm/microsoft-managedidentity-userassignedidentities:0.1.0

在 Release release.yaml 中,寫入元件落入的邊界,以及 Recipe 所要求的 infra 支柱,也就是放置位置:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: identities
version: 1.0.0
environment: production
spec:
aggregate: ./aggregate.yaml
manifest: ./manifest.yaml
boundaries:
azure:
directory: azure
components:
deploy-identity:
infra:
location: westeurope

驗證並建置

Terminal window
aggrega validate --release release.yaml
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)

請 pull Release 所依賴的內容,然後建置:

Terminal window
aggrega pull --release release.yaml
aggrega build --release release.yaml
Pull complete: release.lock (entities=…, source_artifacts=…, external_inputs=…)
[… omitted: the build's elapsed time]
Build completed: 1 file written to <workspace>/dist

建置會寫出 dist/azure/arm/arm-template.json,也就是 azure 邊界的那個範本,其中有一個資源,使用它的實例所賦予的符號名稱。除了資源本身的成員之外,建置還會在 metadata 之下記錄是哪個 Aggregate、Release 與 Recipe 產生了它:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"languageVersion": "2.0",
"resources": {
"DeployIdentity": {
"apiVersion": "2024-11-30",
"location": "westeurope",
"metadata": {
"aggrega.ai/provenance": {
"aggrega.ai/aggregate-name": "identities",
"aggrega.ai/boundary-path": "azure",
"aggrega.ai/compiler-version": "<compiler version>",
"aggrega.ai/component-id": "deploy-identity",
"aggrega.ai/environment": "production",
"aggrega.ai/provider": "arm",
"aggrega.ai/provider-version": "<compiler version>",
"aggrega.ai/recipe-uri": "microsoft-managedidentity-userassignedidentities:0.1.0"
}
},
"name": "deploy-identity",
"type": "Microsoft.ManagedIdentity/userAssignedIdentities"
}
}
}

這個範本就是你交給 Azure 的內容;arm 提供者說明在資源群組範圍部署它的命令。只能在訂用帳戶或管理群組範圍寫入的資源型別,會被草擬為該範圍的片段,其 Recipe 的標頭會指出 Release 所設定的邊界註記,如在訂用帳戶範圍部署與在管理群組範圍部署所述。只能在租用戶範圍寫入、或只能作為另一個資源之擴充的型別,會依名稱被拒絕。

機密

機密屬性的值絕不會寫進草稿,也不會為它草擬預設。它如何到達資源,取決於提供者:

  • Azure:範本頂層的一個 securestring 參數,依實例與屬性命名,由你在部署時提供。廠商標示為選擇性的機密仍是必要參數。
  • CloudFormation:範本中標記為 NoEcho 的 Parameters 項目,以相同方式命名。廠商標記為選用的機密,在這裡是必要的參數。
  • Terraform:Component 上的一個 Feature 槽位,以及標記為 sensitive: true 的模組變數;值透過 Release 附加到該槽位的機密傳入。

只有資源最上層的屬性會成為機密——在 Azure 上是根層級的鍵或 properties 的成員。其下的敏感成員,例如 Azure osProfile 內的 adminPassword 或 Terraform 區塊內的 password,不會被草擬為機密:Release 會把它當作一般值,設定在其父項草擬所在的鍵之內,而匯入會針對每一個以 constraint-dropped 發出警告,指出其路徑與該鍵。

CloudFormation 屬性的名稱含有認證相關字詞——password、secret、token、credential、private key、API key 或 passphrase——且不以 Arn、Id、Name 或 Ref 結尾時,就會被當作機密讀取。這種讀取是草稿的一項慣例,Recipe 的標頭會把它讀取為機密的每個屬性標記出來,讓你在檢閱時加以確認。

CloudFormation 與 Terraform 上的相同匯入

步驟相同;只有匯出與 Recipe 的形狀不同。下面兩個情形由合成的結構描述草擬,其廠商文字不出現在後續內容中。

CloudFormation

Terminal window
aws cloudformation describe-type --type RESOURCE --type-name AWS::Example::Ledger > ledger.json
aggrega import ledger.json

匯入會寫出一個 Component 與一個 Recipe,並為每個它無法承載的廠商限制發出一則警告:

wrote catalog/component/imported/component/aws-example-ledger/0.1.0/component.yaml
wrote catalog/recipe/cloudformation/aws-example-ledger/0.1.0/recipe.yaml
wrote .aggrega-import.json
read as the cloudformation-schema format, with drafted paths relative to <workspace>
warn: the property "LedgerName" does not carry the vendor schema's pattern "…": the catalog reads a pattern as an RE2 regular expression, which refuses it: …
warn: the property "Rules[].Children[]" does not carry the vendor schema's shape below it: the vendor schema's definitions refer back to themselves there, so it is drafted as an open object
mapped AWS::Example::Ledger (AWS::Example::Ledger) to imported/component/aws-example-ledger as a draft, NONE at 0.0167

Recipe 的 template.yaml 在實例所賦予的邏輯 id 下宣告該資源,機密為 NoEcho 參數,每個 provides 鍵寫成 !Ref 或 !GetAtt,並在 provides 之下重複,讓 cloudformation 提供者把它投射到堆疊的 Outputs:

artifacts:
template.yaml: |
Parameters:
{{ .locals.logical_id }}MasterPassword:
Type: String
NoEcho: true
Resources:
{{ .locals.logical_id }}:
Type: AWS::Example::Ledger
Properties:
# … one line for each key the header lists
MasterPassword: !Ref {{ .locals.logical_id }}MasterPassword
provides:
arn: !GetAtt {{ .locals.logical_id }}.Arn

Terraform

Terminal window
terraform init && terraform providers schema -json > schema.json
aggrega import schema.json
wrote catalog/component/imported/component/aws-example-ledger/0.1.0/component.yaml
wrote catalog/recipe/terraform/aws-example-ledger/0.1.0/recipe.yaml
wrote catalog/recipe/terraform/aws-example-ledger/0.1.0/module/main.tf.json
wrote .aggrega-import.json

Terraform 匯出會指名設定所需的提供者,但不含它們的版本,所以匯入會警告:

warn: no version of the provider hashicorp/aws is known, so the module drafted for aws_example_ledger requires it without a version constraint: set terraform.required_providers.aws.version in the module/main.tf.json beside the Recipe, then run aggrega pin on the Recipe

Terraform 草稿是一個旁邊帶有模組的 Recipe。module/main.tf.json 為每個輸入宣告一個變數、宣告那一個資源 this(其每個引數都從它的變數讀取),以及匯出所指名的提供者。Recipe 的 spec.source 指向該模組,其 valuesMapping 從 Component 的支柱填入每個變數,只有在 Release 設定時才寫入選用的輸入。請在第一次 pull 之前固定該模組,讓鎖定檔記錄它的摘要:

Terminal window
aggrega pin catalog/recipe/terraform/aws-example-ledger/0.1.0/recipe.yaml
Pinned Recipe "aws-example-ledger": wrote <workspace>/catalog/recipe/terraform/aws-example-ledger/0.1.0/entity.lock (remote source pins: 0, imported file pins: 0, local source file pins: 1). Commit it beside the entity

未固定的 Recipe 會讓 pull 中止,並指名 aggrega pin。每次編輯模組之後都要再固定一次。

Aggregate、Manifest 與 Release 的內容行,與 Azure 的情形相同,只是使用草擬的名稱與提供者自己的 infra:Terraform Release 會在 Component 宣告了提供者放置位置的鍵(例如 region)時指名它們。

目錄已有的 Component

當目錄已有該服務的 Component,而只缺少你的提供者的 Recipe 時,請在設定中指名該 Component。匯入就只草擬 Recipe,並依 Component 的支柱撰寫。resource_types 的每個項目是一個型別名稱,或是同時帶有型別與 Component 的物件:

plugins:
importer:
# The entry passes resource_types; the importer is served without it.
- uri: importer/arm-schema:<compiler version>
with:
settings:
resource_types:
- type: Microsoft.ManagedIdentity/userAssignedIdentities
component: acme/security/identity

Recipe 依 Component 取名為 identity-microsoft-managedidentity-userassignedidentities,所以它絕不會取用目錄已有的 Recipe 的名稱。目錄中的 Component 若宣告了與型別某個屬性同名的 values 鍵,就從 values 讀取該屬性;廠商要求而 Component 沒有對應鍵的屬性,則從 infra.overrides 讀取,並帶有一個斷言,在 Release 設定它之前會讓建置中止。Component 提供且依名稱符合某個讀回屬性的每個鍵都會被寫入。對於其他每個鍵,草稿會寫出一個指名該鍵而失敗的範本,所以建置會中止,直到你對應它為止,而匯入會警告:

warn: the Component acme/security/identity provides principal_id, and no attribute Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 reads back carries its name, so the drafted Recipe's provides.principal_id halts the build until it is mapped: map it to an attribute Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 reads back, one of id, properties.clientId, properties.principalId, properties.tenantId

Azure 把這個屬性巢狀放在 properties 之下,所以要寫的那一行,就是該型別的完整草稿所帶有的那一行:

provides:
principal_id: "[reference('{{ .locals.symbol }}').principalId]"

寫好那一行之後,Release 就在其 Aggregate 中指名該 Component、在其 Manifest 中指名該 Recipe(如上所述),在 infra.overrides.location 之下設定放置位置,然後驗證並建置出相同的範本。

目錄中沒有 Component 的名稱,會被草擬為以該名稱命名的新 Component,並且匯入會警告,所以拼錯 Component 的名稱不會被誤認為新的 Component。

接下來

  • 核心匯入器說明每個匯入器讀取什麼、如何在多個型別與多個提供者之間選擇、完整的分類、每個目標草擬什麼,以及這三者所回報的診斷。
  • arm 提供者說明 Azure 草稿所合併進的範本,以及它部署的範圍。
  • aggrega import 是該命令的完整參考。