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

将 workload 绑定到挂载项发布的内容

一个无状态应用,由 helm provider 的 Workload 合成在 Kubernetes 上渲染——不使用外部 chart。该应用运行在一个云身份之下,其 Component 为它声明一个槽位,由 Release 填充:填充该槽位的挂载项 输出一个 Kubernetes ServiceAccount 并发布其名称, 应用的 Recipe 则将 pod 绑定到这个已发布的名称。

要点在于数据流的方向。生产方只声明一次名称; 消费方读取它。消费方不会重复声明生产方已拥有的任何值, 因此两者不会出现偏差。

生成示例

你可以用下面的命令构建这个示例,也可以让你的 AI 助手替你构建。

将示例写入一个以其命名的新文件夹:

Terminal window
aggrega scaffold --example workload-identity-binding

然后它会打印接下来要运行的命令。第一条命令进入新文件夹:

Terminal window
cd workload-identity-binding

构建

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

目录从 .aggrega-config.yaml 中指定的两个来源解析:示例自身的 ./catalog(local/compute/identity-app Component 及其 helm/identity-app 合成 Recipe),以及 hub 注册表(core/gcp-workload-identity Feature 及其 FeatureRecipe)。--auto-pull 会在构建期间写入全新克隆所缺少的锁文件。三个 manifest 生成在 dist/helm/helm/ 下。

借助 AI 助手

已配置 Aggrega 工具的 AI 助手也能完成同样的操作:把下面这条提示粘贴给它。

仅使用 Aggrega 工具,不要自行创建任何文件夹。如果工作区文件夹中还没有 `workload-identity-binding` 文件夹,请调用一次 `scaffold` 工具,将 `example` 设为 `workload-identity-binding`,且不设 `dir`:它会把示例写入新的 `workload-identity-binding` 文件夹。然后调用 `build` 工具,将 `workspace_root` 设为 `workload-identity-binding`,`release` 设为 `release.yaml`,`auto_pull` 设为 true,`write` 设为 true。告诉我构建写入了哪些文件;它们位于 `workload-identity-binding/dist/` 下。

Component 声明需求;Release 填充它

应用需要一个运行所用的身份,因此其 Component 将其声明为一个 必需的 identity 槽位:

catalog/component/local/compute/identity-app/1.0.0/component.yaml
featuresSchema:
identity:
required: true
selector: "feature.aggrega.ai/kind!=secret"
inject: []

Aggregate 不为它写任何内容。应用以哪个身份、在哪个 云上运行,由 Operator 针对每个目标选择,因此 Release 用身份 Feature 及该 Feature 所需的每个字段来填充该槽位:

release.yaml
features:
identity:
type: core/gcp-workload-identity
infra:
service_account_name: app-workload-identity
gcp_service_account: app-dev@example-project.iam.gserviceaccount.com

未填充该槽位的 Release 会中止,并指出该槽位。面向 EKS 集群的 Release 则改用 core/aws-irsa 及其 role_arn 填充同一槽位, 其 Manifest 将该 Feature 映射到对应的 FeatureRecipe。选择器会阻止 凭据进入该槽位:用 secret 类 Feature 填充它会中止。

挂载项发布

Feature 在其 schema.provides 中声明它发布一个 name,其 FeatureRecipe 则发布该值——来源于输出的 chart 用于命名 ServiceAccount 的同一个 local;当 Release 写入了 service_account_name 时,即为运维设置的该值:

# FeatureRecipe gcp-workload-identity
locals:
sa_name: '{{ default (printf "%s-%s" .context.rendered_name (.attachment_name | replace "_" "-")) .infra.service_account_name }}'
provides:
name: "{{ .locals.sa_name }}"

Recipe 消费

消费方 Recipe 在 .features.<instance-name>.provides.<key> 读取已发布的名称:

# helm/identity-app recipe workload
serviceAccount: "{{ .features.identity.provides.name }}"
containers:
main:
variables:
IDENTITY_SERVICE_ACCOUNT: {{ .features.identity.provides.name }}

两处读取都解析为挂载项实际输出的 ServiceAccount:

app--identity--app-serviceaccount-app-workload-identity.yaml
[… omitted: the apiVersion]
kind: ServiceAccount
metadata:
name: app-workload-identity
annotations:
iam.gke.io/gcp-service-account: "app-dev@example-project.iam.gserviceaccount.com"
[… omitted: the boundary and recipe annotations, and the labels, that every rendered document carries]
app-deployment.yaml
[… omitted: the Deployment's metadata, selector and pod template up to the containers]
containers:
- name: main
[… omitted: the image and the port]
env:
[… omitted: the GREETING variable]
- name: IDENTITY_SERVICE_ACCOUNT
value: app-workload-identity
serviceAccountName: app-workload-identity

为何读取而非重复

另一种做法是在消费方再写一次 app-workload-identity。 这种做法在有人修改 Operator 的 service_account_name 却只更新两处中的一处之前都可行——届时 pod 会请求一个不存在的 ServiceAccount,而编译过程 不会察觉,因为两种写法都是合法的字符串。

读取 .features.identity.provides.name 消除了第二处写法。同样的 结构适用于所有挂载项:凭据在 .features.<slot>.provides.name 发布其 Secret 的定位符, sidecar 发布其端口,身份 发布其名称。完整的逐字段挂载项结构见 templates/template-context-contract.md。

运维规格配置

release.yaml 按环境实现运维规格配置支柱:容器 镜像标签来自 infra.image_tag(myapp:1.2),身份——其 Feature 及该 Feature 的字段——来自槽位的填充。Developer 的 Aggregate 两者都不包含。