Memcached — 一个 Aggregate,两个目标,以及一个被拒绝的密码
这个精简示例将一个由开发者编写的 Aggregate 编译为单个 Memcached 内存缓存的两种实现:
- 用于本地开发的 Docker Compose,以及
- 合成的 helm Recipe,直接在 Kubernetes 上运行官方上游
memcached镜像(无需外部 chart)。
合成的 helm Recipe 是首选的 Kubernetes 路径:它只依赖 官方镜像标签而非第三方 chart,其渲染出的资源 集合与 workload 声明的内容完全一致。
同一个 aggregate.yaml 驱动这两种实现。每种实现之间只有 Manifest 所选的 Recipe 层会变化。另外两个 Aggregate 为缓存附加了密码,它们的 Releases 是反例:构建会中止,因为没有任何实现能够强制执行 Memcached 密码。
文件
| 文件 | 角色 |
|---|---|
aggregate.yaml | 开发者意图:一个类型为 core/datastore/memcached 的 cache 组件,无凭据——缓存在无身份验证的情况下运行。 |
aggregate-auth.yaml | 同一缓存,在组件的可选 password 槽位上附加了 inject-value 密码凭据(Compose 路径)。其构建会中止。 |
aggregate-helm-synth-auth.yaml | 同一缓存,为合成 helm 路径附加了可引用的密码凭据。其构建会中止。 |
manifest-compose.yaml | 将 Memcached 类型映射到 Compose Recipe(compose/memcached),并将凭据映射到 dotenv-secret。 |
manifest-helm-synth.yaml | 将 Memcached 类型映射到合成的 helm Recipe(helm/memcached),并将凭据映射到 k8s-secret-from-parameter。 |
release-compose.yaml / release-helm-synth.yaml | 两种实现的运维记录。 |
release-negative-compose-auth.yaml / release-negative-helm-synth-auth.yaml | 基于两个带密码的 Aggregate 的运维记录。每次构建都预期会失败。 |
演示内容
- 一个编写界面,多个目标。 单个 Aggregate 是栈是什么的 唯一事实来源。每个 Manifest 说明如何实现它。
- 无状态数据存储。 Memcached 将条目保存在内存中,不做任何持久化
存储,因此该组件的类型是普通的
server,而非stateful-server。在 Kubernetes 上,Recipe 渲染的是 Deployment 而非 StatefulSet,且不请求持久卷;在 Compose 上,服务 不声明数据卷。 - Workload 合成(Kubernetes)。 合成的 helm Recipe 声明了一个 Workload——一个 Deployment 加一个运行官方
memcached镜像的 ClusterIP Service——由 helm provider 在无外部 chart 的情况下将其转换为 Kubernetes 清单。渲染出的资源集合与 Workload 声明的内容完全一致。由于 Compose Recipe 和合成的 helm Recipe 都使用名称memcached,Manifests 以 provider 作用域的方式引用它们:compose/memcached和helm/memcached。 - 实现无法提供的保护会被拒绝,而不是被丢弃。 该组件声明了一个可选的
password槽位。memcached镜像不强制执行任何密码:它的两种认证模式是仅通过二进制协议使用的 SASL 和 ASCII 认证文件,而目录中的 Memcached 客户端两者都不使用。接受该附件的 Recipe 会渲染出一个应答所有客户端的缓存,而 Aggregate 却声称它受到保护,因此每个 Recipe 都会中止构建,并指出该槽位。
生成示例
你可以用下面的命令构建这个示例,也可以让你的 AI 助手替你构建。
将示例写入一个以其命名的新文件夹:
aggrega scaffold --example memcached-cache然后它会打印接下来要运行的命令。第一条命令进入新文件夹:
cd memcached-cache构建
此工作区不带 .aggrega-config.yaml,因此标准库实体从 hub 注册表 hub.aggrega.ai 解析,无需目录标志;--auto-pull 会在构建期间写入新克隆所缺少的锁文件:
# Local development (Docker Compose)aggrega build --release release-compose.yaml --auto-pull
# Kubernetes (synthesized helm Recipe, official image)aggrega build --release release-helm-synth.yaml --auto-pull渲染产物位于 dist/ 下。
借助 AI 助手
已配置 Aggrega 工具的 AI 助手也能完成同样的操作:把下面这条提示粘贴给它。
只使用 Aggrega 工具,不要自己创建任何文件夹。如果工作区文件夹中还没有 `memcached-cache` 文件夹,调用一次 `scaffold` 工具,将 `example` 设为 `memcached-cache`,不设 `dir`:它会把示例写入一个新的 `memcached-cache` 文件夹。然后按以下顺序为每个 Release 各调用一次 `build` 工具——`release-compose.yaml` 和 `release-helm-synth.yaml`——每次都将 `workspace_root` 设为 `memcached-cache`、`release` 设为该文件、`auto_pull` 设为 true、`write` 设为 true。告诉我每次构建写入了哪些文件;它们位于 `memcached-cache/dist/` 下。对照:缓存无法强制执行的密码
两个反例 Release 构建带密码的 Aggregate,每次构建都预期会失败:
aggrega build --release release-negative-compose-auth.yaml --auto-pullaggrega build --release release-negative-helm-synth-auth.yaml --auto-pull每次都在 Memcached Recipe 中中止,在写入任何内容之前,且中止信息以 Recipe 自己的消息结尾:
a password is attached to the password slot of "cache", but the memcachedimage this Recipe runs cannot enforce a password: it authenticates only throughSASL over the binary protocol or an ASCII authentication file, which thecatalog's Memcached clients do not use, so the cache would accept every clientanyway; remove the attachment, and restrict who can reach the cache through itsnetwork拒绝以附件本身为依据,而不取决于凭据如何交付,因此每个能填充该槽位的 Feature 都会以同样方式中止。托管实现的行为也一样:Amazon ElastiCache 不提供 Memcached 认证,它的 Recipes 同样会因该附件而中止。因此,Memcached 缓存靠谁能访问它——即它所在的网络——来保护,应构建的形式是基础的 aggregate.yaml。