跳转到内容
AggregaAggregaAggregaAggrega
选择语言: 简体中文
本页面由机器翻译。阅读英文原文。

部署目录中没有的服务

当目录中没有某个云服务对应的 Component 时,供应商自己对该服务资源类型的描述可以顶替它。aggrega import 读取这份描述——云供应商为某种资源类型发布的 schema——并据此起草两个文件:一个 Component,在 Aggrega 读取的各支柱中声明该类型的设置;以及一个 Recipe,通过恰好写入一个该类型的资源,为某个提供方实现该 Component。你审阅这些草稿,把使用它们的行添加到 Aggregate 和 Release 中,然后构建。

读取三种格式,每种格式由为其服务的一个核心导入器读取:

格式你导出的 schemaRecipe 的 provider
cloudformation-schemaCloudFormation 资源类型,来自 aws cloudformation describe-type 或提供方 schema 文件cloudformation
terraform-schemaTerraform 或 OpenTofu 提供方的 schema,来自 terraform providers schema -jsonterraform
arm-schemaAzure 资源提供方的 Bicep 类型文件 types.jsonarm

本页完整演示 Azure 的情形,随后展示 CloudFormation 和 Terraform 的情形,以及一个只为目录中已有的 Component 起草 Recipe 的变体。下面 Azure 部分的命令和输出来自对携带这些导入器和 arm 提供方的编译器发布版本构建产物的一次运行。

不会替你获取任何内容。你用供应商自己的工具导出 schema,因此导入读取的是你手中的文件,不会访问任何云账户。

导出 schema

每个供应商的工具都会把 schema 写入一个文件。在工作区中运行对应你所用云的那个工具:

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 schema 文件及其许可证

2024-11-30 版本的 Microsoft.ManagedIdentity 的 types.json 是 Azure/bicep-types-az 仓库中的一个文件,版权归 Microsoft Corporation 所有,依据 MIT 许可证分发。下面的草稿所引用的描述属于 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

列出要起草的类型

一个 schema 文件可以包含多种资源类型,而导入只起草你指明的那些。列出文件中包含的类型:

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 schema,而无需导入先识别它。

mapped 一行表示该资源类型成为了它自己的草稿,目录中没有与之接近的 Component。草稿写在 catalog/ 下,工作区将该文件夹读取为目录,因此无需任何配置,它们在下一次验证和构建时即可解析。记录 .aggrega-import.json 列出了这些草稿,因此 aggrega import --sync 可以在 schema 变更后更新它们,而不会覆盖你所做的编辑。

草稿说了什么

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

草稿从供应商 schema 中取用的每段文字都以 [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)中,写一个所起草类型的 component,带上 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)中,写 component 落入的边界以及 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)

拉取 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 的形态不同。下面两种情形根据合成的 schema 起草,其供应商文字在后文中略去。

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 设置了可选输入时才写入该输入。在第一次拉取之前固定该模块,以便锁文件记录其摘要:

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 会使拉取停止,并指明 aggrega pin。每次编辑模块之后都要再次固定。

Aggregate、Manifest 和 Release 的各行与 Azure 情形相同,使用所起草的名称和提供方自己的 infra:Terraform Release 会指明提供方的放置键(例如 region),Component 在其中声明这些键。

目录中已有的 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 是该命令的完整参考。