共用的結構描述與對應程式庫:一個 MariaDB Component
這一頁解釋一個概念:把 Component 的大部分結構描述與 Recipe 的對應保存在多個實體共用的程式庫中,它不是可執行的工作區。MariaDB + Adminer——可執行的雙目標範例 從結構描述程式庫組成它的資料庫的儲存設定;請用以下命令把它寫入你自己的資料夾:
aggrega draft --example mariadb-adminer接著它會印出要執行的下一批命令:
cd mariadb-admineraggrega pull --release release-compose.yamlaggrega build --release release-compose.yaml這一頁的實體是你自己的目錄,位於命名空間 my-org;每個實體的形狀就是一個正式環境等級的 MariaDB Component、它的 Recipe 以及背後的兩個程式庫所採用的形狀。
結構描述程式庫保存形狀
SchemaLibrary 宣告具名的結構描述,以精簡形式撰寫——每個欄位一行,含型別、限制與預設值——建置會在任何值對它檢查之前將它編譯為 JSON Schema 2020-12。節錄:
apiVersion: aggrega.ai/v1beta1kind: SchemaLibrarymetadata: name: Datastores version: 1.0.0spec: schemas: PodResources: requests: cpu: { type: string, pattern: "^[0-9]+(m|\\.[0-9]+)?$", default: "250m" } memory: { type: string, pattern: "^[1-9][0-9]*(Mi|Gi)$", default: "512Mi" } NodePool: replicaCount: { type: integer, min: 1, max: 9, default: 1 } resources: { $ref: "Datastores#/schemas/PodResources" } MariaDBPayload: architecture: { type: string, enum: [standalone, replication], default: standalone } image: tag: { type: string, validate: '{{ ne . "latest" }}', validateMessage: "image.tag must name a version: latest moves with every chart release" } primary: { $ref: "Datastores#/schemas/NodePool", required: true } secondary: when: '{{ eq $.architecture "replication" }}' required: true properties: replicaCount: { type: integer, min: 2, max: 8, default: 2 }參照以程式庫的 metadata.name 指名它——Datastores#/schemas/<entry>——絕不以程式庫儲存的位置指名,在程式庫內部也是如此,所以無論程式庫位於工作區的目錄中還是發布到登錄庫,同一個參照都能解析。validate: 與 validateMessage: 為欄位自己新增檢查;when: 讓整個物件成為有條件的,所以次要集區的限制只在架構解析為 replication 時才綁定。跨文件的 $ref: 旁邊只接受 properties、required 與 description,這就是為什麼次要集區是寫出來的,而不是參照 NodePool。
Component 參照它
apiVersion: aggrega.ai/v1beta1kind: Componentmetadata: name: my-org/datastore/mariadb version: 1.0.0 labels: component.aggrega.ai/type: stateful-server component.aggrega.ai/category: datastorespec: schema: values: $ref: Datastores#/schemas/MariaDBPayload infra: cpu_arch: { type: string, enum: [amd64, arm64], default: amd64 } provides: host: { type: string, format: hostname } port: { type: integer, min: 1024, max: 65535 } featuresSchema: root_credentials: required: true inject: [] replication_credentials: required: false inject: [] assertions: replication-needs-credentials: | {{- if eq .values.architecture "replication" -}} {{- if not (hasKey .features "replication_credentials") -}} {{- fail "architecture=replication requires replication_credentials secret to be supplied" -}} {{- end -}} {{- end -}}values 支柱是一行 $ref:;小而屬於 Component 專屬的 infra 與 provides 保持內嵌。斷言保存 Component 契約中沒有任何結構描述關鍵字能表達的規則,無論由哪個 Recipe 實現它。
對應程式庫保存轉譯
MappingLibrary 保存 Recipe 匯入的範本片段。一個項目渲染 chart 的值,並以 define 宣告一個輔助函式,再對每個節點集區以 template 呼叫它:
apiVersion: aggrega.ai/v1beta1kind: MappingLibrarymetadata: name: BitnamiHelpers version: 1.0.0spec: mappings: MariaDBBitnamiCore: | {{- define "BitnamiNodePool" }} resources: requests: cpu: {{ .pool.resources.requests.cpu | quote }} memory: {{ .pool.resources.requests.memory | quote }} {{- end }} {{- with .values -}} fullnameOverride: {{ $.context.rendered_name | quote }} architecture: {{ .architecture }} primary: name: primary {{- template "BitnamiNodePool" (dict "pool" .primary) }} {{- end }}輔助函式宣告在使用它的項目內,因為 Recipe 匯入一個項目,而定義隨持有它的項目一起傳遞。該片段由 Aggrega 的渲染器求值,所以它使用 Sprig 的函式與 Aggrega 為可能不存在的鍵提供的 optional(見 sprig-functions.md);Helm 的 include 不在其列,而 template 會把它的輸出恰好寫在它所在之處。
Recipe 匯入它
apiVersion: aggrega.ai/v1beta1kind: Recipemetadata: name: mariadb-bitnami version: 1.0.0spec: capability: helm implements: my-org/datastore/mariadb:^1.0.0 source: kind: helm-oci-chart url: oci://registry-1.docker.io/bitnamicharts/mariadb version: "25.0.8" valuesMapping: $import: BitnamiHelpers#/mappings/MariaDBBitnamiCore provides: host: "{{ .context.rendered_name }}{{ if eq .values.architecture `replication` }}-primary{{ end }}" port: "3306"Recipe 很小:對應存在程式庫中,而 Recipe 保留屬於它自己的部分——它渲染的 chart 與它發布的位址。因為對應將 chart 的 fullnameOverride 固定到 .context.rendered_name,Recipe 可以計算主要節點的 Service 名稱,而不必假設它。
程式庫換來什麼
若寫成 JSON Schema,每個欄位的單行對應會變成巢狀區塊,每個物件都要加上 type: object、properties: 與 required: 清單;when: 與 validate: 在 JSON Schema 中沒有自己的關鍵字。精簡形式涵蓋這個 Component 所需的一切,不需要 $jsonschema: 逃生艙口。節省隨共用而增加:程式庫只寫一次,之後每個 Component 的 values 支柱都只是單一的 $ref: 一行。