PostgreSQL 初始化指令碼與自訂設定——合成的 helm
core/datastore/postgres 公開兩個可選的開發者值,用來決定資料庫的第一次啟動:init_scripts(初始化時執行一次的 SQL 或 shell)以及 custom_config(postgresql.conf 片段)。在合成的 helm 目標上,Recipe 會把它們以掛載的 ConfigMaps 帶進容器。這個範例展示該功能是累加式的:兩個值都沒設定的 Aggregate,渲染結果與最小資料庫完全相同;設定了它們的 Aggregate 則多出 ConfigMaps、磁碟區與掛載,其他一切都不受影響。
同一個 Component、同一個 Manifest 的兩個 Releases,只有開發者值不同:
release-bare.yaml(aggregate-bare.yaml)——init_scripts和custom_config未設定。release-config.yaml(aggregate-config.yaml)——兩者皆已設定。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example postgres-init-config它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd postgres-init-config建置
aggrega build --release release-bare.yaml --auto-pullaggrega build --release release-config.yaml --auto-pull目錄從 .aggrega-config.yaml 中指名的 hub 登錄庫解析,所以不需要 --catalog 旗標,而 --auto-pull 會在建置期間寫入剛複製的存放庫所缺少的鎖定檔。未設定的 Release 在 dist/helm/helm/ 之下寫出三個檔案;已設定的 Release 則寫出這三個檔案,外加 dist/helm/helm/db/ 之下的另外兩個。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,不要自行建立任何資料夾。如果工作區資料夾中還沒有 `postgres-init-config` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `postgres-init-config`,且不指定 `dir`:它會把範例寫入新的 `postgres-init-config` 資料夾。接著為每個 Release 各呼叫一次 `build` 工具,依序為 `release-bare.yaml` 和 `release-config.yaml`,每次都將 `workspace_root` 設為 `postgres-init-config`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫出了哪些檔案;它們位於 `postgres-init-config/dist/` 之下。未設定——累加式基準
aggregate-bare.yaml 讓兩個值都保持未設定。渲染輸出是一個 StatefulSet、一個 ClusterIP Service 和一個 Headless Service——沒有 ConfigMap、沒有 configmap 磁碟區、沒有掛載到初始化目錄的磁碟區掛載,容器上也沒有設定檔引數。這是最小 PostgreSQL 資料庫的一般形狀:
dist/helm/helm/db-statefulset.yamldist/helm/helm/db-default-service.yamldist/helm/helm/db-headless-service.yaml由於兩個值在 Recipe 中都以存在與否把關,未設定的值不會輸出任何東西——ConfigMap 主體渲染為空並被略過,磁碟區、掛載與伺服器引數的各行也被擋掉。從未碰過這些值的 Aggregate 不會因它們的存在而改變。
已設定——掛載了指令碼與設定
aggregate-config.yaml 設定了兩個值:
values: init_scripts: 01-schema.sql: | CREATE TABLE app_users ( ... ); 02-seed.sql: | INSERT INTO app_users (email) VALUES ('founder@example.com'); custom_config: | max_connections = 200 shared_buffers = 256MB建置現在會多寫出兩個 ConfigMaps,並把它們連接到 StatefulSet:
dist/helm/helm/db/initdb-configmap.yamldist/helm/helm/db/config-configmap.yaml兩者都位於 db/ 之下,那是元件自己的名稱。Recipe 在它的 artifacts: 區塊中指名它們,而在那裡指名的檔案帶有其作者所寫的鍵,而不是任何識別元件的東西——所以建置會把它寫在宣告它的元件之下,共用同一個 Recipe 的兩個元件不會落在同一條路徑上。StatefulSet 和兩個 Services 由目標依其所屬的元件命名,所以它們留在邊界自己的目錄。
這兩個值放在各自獨立的 ConfigMaps 中,因為每一個都整份掛載到不同的容器路徑:
init_scripts→ ConfigMapdb-initdb,每個指令碼一個資料鍵(01-schema.sql、02-seed.sql)。configmap磁碟區把它掛載在/docker-entrypoint-initdb.d,PostgreSQL 會在第一次初始化時依鍵的字母順序執行一次這些檔案。custom_config→ ConfigMapdb-config,一個資料鍵postgresql.conf。第二個configmap磁碟區把它掛載在/etc/postgresql,而容器的args:讓伺服器指向它:postgres -c config_file=/etc/postgresql/postgresql.conf。片段省略的設定維持其內建預設值。
# db-statefulset.yaml (container)containers: - name: main image: postgres:16 args: - postgres - -c - config_file=/etc/postgresql/postgresql.conf [… omitted: the container's port, environment and resources] volumeMounts: - name: data mountPath: /var/lib/postgresql/data - name: initdb mountPath: /docker-entrypoint-initdb.d - name: pgconf mountPath: /etc/postgresql [… omitted: the liveness and readiness probes]volumes: - name: initdb configMap: name: db-initdb - name: pgconf configMap: name: db-config這些伺服器引數以 args: 而不是 command: 的方式傳入,而這個差異決定了資料庫啟動後是否已有內容:args: 是把引數提供給映像檔的進入點,而 command: 則是取代它。該進入點是啟動指令碼,它會在空的資料目錄上建立資料庫、讀取 POSTGRES_* 環境,並執行 /docker-entrypoint-initdb.d 之下的檔案。取代它會讓伺服器針對掛載的 postgresql.conf 啟動,同時跳過初始化與引導指令碼——Aggregate 所設定的值會渲染進資訊清單卻從不執行。
只設定兩個值中的一個也同樣有效——另一個的 ConfigMap、磁碟區與掛載就是不存在,而設定檔引數只有在設定了 custom_config 時才會出現。
Compose
core/datastore/postgres 的 Compose Recipe 讀取同一個 Component,並透過 compose 自己的基本元素實現這兩個值:它會渲染頂層的 configs: 項目,服務再把每一個掛載為檔案。
init_scripts→ 每個鍵一個 config,各自掛載在/docker-entrypoint-initdb.d之下它自己的檔名。映像檔會依檔名順序執行它在那裡找到的檔案一次,而且只在資料目錄為空時才執行——已經有資料的資料庫不會被動到。custom_config→ 一個 config,掛載為單一檔案/etc/postgresql/postgresql.conf,並以相同的postgres -c config_file=…引數讓伺服器指向它。片段省略的設定維持其內建預設值。
這裡的每一半同樣以存在與否把關,所以讓其中一個值保持未設定的 Aggregate,渲染結果就像那個鍵不存在一樣。
為 compose 部署撰寫 custom_config 區塊時,有一個設定值得特別點出:listen_addresses 預設只接受本機連線,所以省略它的區塊會讓資料庫無法從 compose 網路上的其他服務連到。
這個範例中的 Manifest 把 core/datastore/postgres 導向 helm Recipe。要在 compose 上建置同一個 Aggregate,就需要改把它導向 compose/postgres 的 Manifest;開發者值維持不變。