feat(agent-unit): provision dedicated PostgreSQL runtime role
This commit is contained in:
1 parent
bcc5e0d4d0
commit
b357808e99
7 files changed
+205
-10
No files matched your search
@@ -2,5 +2,5 @@ apiVersion: v2
|
|||||||
name: mixingx-agent-unit
|
name: mixingx-agent-unit
|
||||||
description: "One MixingX agent unit release: agent backend, agent web, and agent PostgreSQL."
|
description: "One MixingX agent unit release: agent backend, agent web, and agent PostgreSQL."
|
||||||
type: application
|
type: application
|
||||||
version: 0.1.0
|
version: 0.1.1
|
||||||
appVersion: "1.0.1"
|
appVersion: "1.0.1"
|
||||||
@@ -6,6 +6,13 @@ Deploys one agent site as one Helm release:
|
|||||||
- `agent-web-<site>` frontend
|
- `agent-web-<site>` frontend
|
||||||
- `agent-<site>-postgres` StatefulSet and retained PVC
|
- `agent-<site>-postgres` StatefulSet and retained PVC
|
||||||
|
|
||||||
|
The release requires a pre-created PostgreSQL credentials Secret. By default its
|
||||||
|
name is `agent-<site>-postgres-credentials`; the required keys are
|
||||||
|
`admin-password`, `app-password`, and `connection-string`. The Agent connection
|
||||||
|
string must use `sharedInfra.postgres.appUsername`, while the init container uses
|
||||||
|
the administrator credential only to idempotently provision the non-superuser
|
||||||
|
application role and its database-local idle transaction timeout.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -14,5 +21,6 @@ helm upgrade --install mixingx-agent-workshop-a ./charts/mixingx-agent-unit \
|
|||||||
--set site=workshop-a \
|
--set site=workshop-a \
|
||||||
--set nodeName=agent-node-01 \
|
--set nodeName=agent-node-01 \
|
||||||
--set storage.className=longhorn \
|
--set storage.className=longhorn \
|
||||||
|
--set sharedInfra.postgres.appUsername=agent_workshop_a \
|
||||||
--set tag=2026.07.02-abc1234
|
--set tag=2026.07.02-abc1234
|
||||||
```
|
```
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
\set ON_ERROR_STOP on
|
||||||
|
\getenv agent_username AGENT_DB_USERNAME
|
||||||
|
\getenv agent_password AGENT_DB_PASSWORD
|
||||||
|
\getenv database_name POSTGRESQL_DATABASENAME
|
||||||
|
\getenv idle_timeout IDLE_IN_TRANSACTION_SESSION_TIMEOUT
|
||||||
|
|
||||||
|
SELECT format(
|
||||||
|
'CREATE ROLE %I LOGIN PASSWORD %L NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS',
|
||||||
|
:'agent_username',
|
||||||
|
:'agent_password')
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_roles
|
||||||
|
WHERE rolname = :'agent_username')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format(
|
||||||
|
'ALTER ROLE %I WITH LOGIN PASSWORD %L NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS',
|
||||||
|
:'agent_username',
|
||||||
|
:'agent_password')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format('GRANT CONNECT ON DATABASE %I TO %I', :'database_name', :'agent_username')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format('ALTER DATABASE %I OWNER TO %I', :'database_name', :'agent_username')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format('ALTER SCHEMA public OWNER TO %I', :'agent_username')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format('GRANT ALL ON SCHEMA public TO %I', :'agent_username')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
-- Existing installations may have objects created by the former postgres application login.
|
||||||
|
-- Transfer only user objects in public; extension-owned objects remain with their extension owner.
|
||||||
|
SELECT format(
|
||||||
|
'ALTER %s %I.%I OWNER TO %I',
|
||||||
|
CASE c.relkind
|
||||||
|
WHEN 'r' THEN 'TABLE'
|
||||||
|
WHEN 'p' THEN 'TABLE'
|
||||||
|
WHEN 'S' THEN 'SEQUENCE'
|
||||||
|
WHEN 'v' THEN 'VIEW'
|
||||||
|
WHEN 'm' THEN 'MATERIALIZED VIEW'
|
||||||
|
WHEN 'f' THEN 'FOREIGN TABLE'
|
||||||
|
END,
|
||||||
|
n.nspname,
|
||||||
|
c.relname,
|
||||||
|
:'agent_username')
|
||||||
|
FROM pg_class c
|
||||||
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||||
|
WHERE n.nspname = 'public'
|
||||||
|
AND c.relkind IN ('r', 'p', 'S', 'v', 'm', 'f')
|
||||||
|
AND pg_get_userbyid(c.relowner) <> :'agent_username'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_depend d
|
||||||
|
WHERE d.classid = 'pg_class'::regclass
|
||||||
|
AND d.objid = c.oid
|
||||||
|
AND d.deptype = 'e')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format(
|
||||||
|
'ALTER %s %I.%I(%s) OWNER TO %I',
|
||||||
|
CASE p.prokind
|
||||||
|
WHEN 'p' THEN 'PROCEDURE'
|
||||||
|
ELSE 'FUNCTION'
|
||||||
|
END,
|
||||||
|
n.nspname,
|
||||||
|
p.proname,
|
||||||
|
pg_get_function_identity_arguments(p.oid),
|
||||||
|
:'agent_username')
|
||||||
|
FROM pg_proc p
|
||||||
|
JOIN pg_namespace n ON n.oid = p.pronamespace
|
||||||
|
WHERE n.nspname = 'public'
|
||||||
|
AND p.prokind IN ('f', 'p', 'w')
|
||||||
|
AND pg_get_userbyid(p.proowner) <> :'agent_username'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_depend d
|
||||||
|
WHERE d.classid = 'pg_proc'::regclass
|
||||||
|
AND d.objid = p.oid
|
||||||
|
AND d.deptype = 'e')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format(
|
||||||
|
'ALTER ROLE %I IN DATABASE %I SET idle_in_transaction_session_timeout TO %L',
|
||||||
|
:'agent_username',
|
||||||
|
:'database_name',
|
||||||
|
:'idle_timeout')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
-- Startup migrations run as the database owner. Keep active migration and maintenance work
|
||||||
|
-- unbounded here; only idle transactions receive the safety timeout.
|
||||||
|
SELECT format('ALTER ROLE %I IN DATABASE %I RESET statement_timeout', :'agent_username', :'database_name')
|
||||||
|
\gexec
|
||||||
|
|
||||||
|
SELECT format('ALTER ROLE %I IN DATABASE %I RESET lock_timeout', :'agent_username', :'database_name')
|
||||||
|
\gexec
|
||||||
@@ -14,6 +14,10 @@
|
|||||||
{{- .Values.serviceId | default (include "mixingx-unit.name" .) -}}
|
{{- .Values.serviceId | default (include "mixingx-unit.name" .) -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
||||||
|
{{- define "mixingx-unit.postgresCredentialsSecretName" -}}
|
||||||
|
{{- .Values.sharedInfra.postgres.credentialsSecret.name | default (printf "%s-postgres-credentials" (include "mixingx-unit.name" .)) -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
{{- define "mixingx-unit.basePath" -}}
|
{{- define "mixingx-unit.basePath" -}}
|
||||||
{{- printf "/%s-%s" .Values.role (.Values.site | toString) -}}
|
{{- printf "/%s-%s" .Values.role (.Values.site | toString) -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
@@ -59,7 +63,8 @@ REDIS_HOST: "{{ .Values.sharedInfra.redis.host }}"
|
|||||||
REDIS_PORT: "{{ .Values.sharedInfra.redis.port }}"
|
REDIS_PORT: "{{ .Values.sharedInfra.redis.port }}"
|
||||||
POSTGRESQL_HOST: "{{ $pgHost }}"
|
POSTGRESQL_HOST: "{{ $pgHost }}"
|
||||||
POSTGRESQL_PORT: "{{ .Values.sharedInfra.postgres.port }}"
|
POSTGRESQL_PORT: "{{ .Values.sharedInfra.postgres.port }}"
|
||||||
POSTGRESQL_USERNAME: "{{ .Values.sharedInfra.postgres.username }}"
|
POSTGRESQL_USERNAME: "{{ .Values.sharedInfra.postgres.appUsername }}"
|
||||||
|
POSTGRESQL_DATABASENAME: "{{ include "mixingx-unit.database" . }}"
|
||||||
MESSAGING_HOST: "{{ .Values.sharedInfra.rabbitmq.host }}"
|
MESSAGING_HOST: "{{ .Values.sharedInfra.rabbitmq.host }}"
|
||||||
MESSAGING_PORT: "{{ .Values.sharedInfra.rabbitmq.port }}"
|
MESSAGING_PORT: "{{ .Values.sharedInfra.rabbitmq.port }}"
|
||||||
MESSAGING_USERNAME: "{{ .Values.sharedInfra.rabbitmq.username }}"
|
MESSAGING_USERNAME: "{{ .Values.sharedInfra.rabbitmq.username }}"
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{{- $name := include "mixingx-unit.name" . -}}
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: {{ $name }}-postgres-role-provisioning
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: mixingx
|
||||||
|
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||||
|
app.kubernetes.io/component: {{ $name }}-postgres
|
||||||
|
data:
|
||||||
|
provision-agent-role.sql: |
|
||||||
|
{{ .Files.Get "files/provision-agent-role.sql" | nindent 4 }}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
{{- $serviceId := include "mixingx-unit.serviceId" . -}}
|
{{- $serviceId := include "mixingx-unit.serviceId" . -}}
|
||||||
{{- $basePath := include "mixingx-unit.basePath" . -}}
|
{{- $basePath := include "mixingx-unit.basePath" . -}}
|
||||||
{{- $pgHost := printf "%s-postgres-service" $name -}}
|
{{- $pgHost := printf "%s-postgres-service" $name -}}
|
||||||
|
{{- $pgCredentialsSecretName := include "mixingx-unit.postgresCredentialsSecretName" . -}}
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -24,8 +25,58 @@ spec:
|
|||||||
app.kubernetes.io/component: {{ $name }}
|
app.kubernetes.io/component: {{ $name }}
|
||||||
annotations:
|
annotations:
|
||||||
checksum/config: {{ include "mixingx-unit.backendConfigData" . | sha256sum }}
|
checksum/config: {{ include "mixingx-unit.backendConfigData" . | sha256sum }}
|
||||||
|
checksum/postgres-role-sql: {{ .Files.Get "files/provision-agent-role.sql" | sha256sum }}
|
||||||
spec:
|
spec:
|
||||||
{{ include "mixingx-unit.backendScheduling" . | nindent 6 }}
|
{{ include "mixingx-unit.backendScheduling" . | nindent 6 }}
|
||||||
|
initContainers:
|
||||||
|
- name: provision-postgres-role
|
||||||
|
image: {{ .Values.images.postgres.repository }}:{{ .Values.images.postgres.tag | default "latest" }}
|
||||||
|
imagePullPolicy: IfNotPresent
|
||||||
|
command: ["/bin/sh", "-ec"]
|
||||||
|
args:
|
||||||
|
- |
|
||||||
|
attempt=0
|
||||||
|
until pg_isready --host="$POSTGRESQL_HOST" --port="$POSTGRESQL_PORT" --username="$POSTGRES_ADMIN_USERNAME" --dbname="$POSTGRESQL_DATABASENAME"; do
|
||||||
|
attempt=$((attempt + 1))
|
||||||
|
if [ "$attempt" -ge 60 ]; then
|
||||||
|
echo "PostgreSQL did not become ready within 120 seconds" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
exec psql \
|
||||||
|
--host="$POSTGRESQL_HOST" \
|
||||||
|
--port="$POSTGRESQL_PORT" \
|
||||||
|
--username="$POSTGRES_ADMIN_USERNAME" \
|
||||||
|
--dbname="$POSTGRESQL_DATABASENAME" \
|
||||||
|
--file=/opt/mixingx/postgres/provision-agent-role.sql
|
||||||
|
env:
|
||||||
|
- name: POSTGRESQL_HOST
|
||||||
|
value: "{{ $pgHost }}"
|
||||||
|
- name: POSTGRESQL_PORT
|
||||||
|
value: "{{ .Values.sharedInfra.postgres.port }}"
|
||||||
|
- name: POSTGRESQL_DATABASENAME
|
||||||
|
value: "{{ $database }}"
|
||||||
|
- name: POSTGRES_ADMIN_USERNAME
|
||||||
|
value: "{{ .Values.sharedInfra.postgres.adminUsername }}"
|
||||||
|
- name: AGENT_DB_USERNAME
|
||||||
|
value: "{{ .Values.sharedInfra.postgres.appUsername }}"
|
||||||
|
- name: IDLE_IN_TRANSACTION_SESSION_TIMEOUT
|
||||||
|
value: "{{ .Values.sharedInfra.postgres.idleInTransactionSessionTimeout }}"
|
||||||
|
- name: PGPASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: {{ $pgCredentialsSecretName }}
|
||||||
|
key: {{ .Values.sharedInfra.postgres.credentialsSecret.adminPasswordKey }}
|
||||||
|
- name: AGENT_DB_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: {{ $pgCredentialsSecretName }}
|
||||||
|
key: {{ .Values.sharedInfra.postgres.credentialsSecret.appPasswordKey }}
|
||||||
|
volumeMounts:
|
||||||
|
- name: postgres-role-provisioning
|
||||||
|
mountPath: /opt/mixingx/postgres
|
||||||
|
readOnly: true
|
||||||
containers:
|
containers:
|
||||||
- name: {{ $name }}
|
- name: {{ $name }}
|
||||||
{{ $backendImg := .Values.images.backend }}
|
{{ $backendImg := .Values.images.backend }}
|
||||||
@@ -46,6 +97,16 @@ spec:
|
|||||||
env:
|
env:
|
||||||
- name: TZ
|
- name: TZ
|
||||||
value: "{{ .Values.timezone | default "Asia/Shanghai" }}"
|
value: "{{ .Values.timezone | default "Asia/Shanghai" }}"
|
||||||
|
- name: ConnectionStrings__PostgreSQL
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: {{ $pgCredentialsSecretName }}
|
||||||
|
key: {{ .Values.sharedInfra.postgres.credentialsSecret.connectionStringKey }}
|
||||||
|
- name: POSTGRESQL_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: {{ $pgCredentialsSecretName }}
|
||||||
|
key: {{ .Values.sharedInfra.postgres.credentialsSecret.appPasswordKey }}
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /health
|
path: /health
|
||||||
@@ -62,6 +123,10 @@ spec:
|
|||||||
periodSeconds: 30
|
periodSeconds: 30
|
||||||
timeoutSeconds: 10
|
timeoutSeconds: 10
|
||||||
failureThreshold: 8
|
failureThreshold: 8
|
||||||
|
volumes:
|
||||||
|
- name: postgres-role-provisioning
|
||||||
|
configMap:
|
||||||
|
name: {{ $name }}-postgres-role-provisioning
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -99,10 +164,6 @@ stringData:
|
|||||||
ConnectionStrings__redis: "{{ .Values.sharedInfra.redis.host }}:{{ .Values.sharedInfra.redis.port }},password={{ .Values.sharedInfra.redis.password }}"
|
ConnectionStrings__redis: "{{ .Values.sharedInfra.redis.host }}:{{ .Values.sharedInfra.redis.port }},password={{ .Values.sharedInfra.redis.password }}"
|
||||||
REDIS_PASSWORD: "{{ .Values.sharedInfra.redis.password }}"
|
REDIS_PASSWORD: "{{ .Values.sharedInfra.redis.password }}"
|
||||||
REDIS_URI: "redis://:{{ .Values.sharedInfra.redis.password }}@{{ .Values.sharedInfra.redis.host }}:{{ .Values.sharedInfra.redis.port }}"
|
REDIS_URI: "redis://:{{ .Values.sharedInfra.redis.password }}@{{ .Values.sharedInfra.redis.host }}:{{ .Values.sharedInfra.redis.port }}"
|
||||||
ConnectionStrings__PostgreSQL: "Host={{ $pgHost }};Port={{ .Values.sharedInfra.postgres.port }};Username={{ .Values.sharedInfra.postgres.username }};Password={{ .Values.sharedInfra.postgres.password }};Database={{ $database }}"
|
|
||||||
POSTGRESQL_PASSWORD: "{{ .Values.sharedInfra.postgres.password }}"
|
|
||||||
POSTGRESQL_URI: "postgresql://{{ .Values.sharedInfra.postgres.username }}:{{ .Values.sharedInfra.postgres.password }}@{{ $pgHost }}:{{ .Values.sharedInfra.postgres.port }}/{{ $database }}"
|
|
||||||
POSTGRESQL_DATABASENAME: "{{ $database }}"
|
|
||||||
ConnectionStrings__messaging: "amqp://{{ .Values.sharedInfra.rabbitmq.username }}:{{ .Values.sharedInfra.rabbitmq.password }}@{{ .Values.sharedInfra.rabbitmq.host }}:{{ .Values.sharedInfra.rabbitmq.port }}"
|
ConnectionStrings__messaging: "amqp://{{ .Values.sharedInfra.rabbitmq.username }}:{{ .Values.sharedInfra.rabbitmq.password }}@{{ .Values.sharedInfra.rabbitmq.host }}:{{ .Values.sharedInfra.rabbitmq.port }}"
|
||||||
MESSAGING_PASSWORD: "{{ .Values.sharedInfra.rabbitmq.password }}"
|
MESSAGING_PASSWORD: "{{ .Values.sharedInfra.rabbitmq.password }}"
|
||||||
MESSAGING_URI: "amqp://{{ .Values.sharedInfra.rabbitmq.username }}:{{ .Values.sharedInfra.rabbitmq.password }}@{{ .Values.sharedInfra.rabbitmq.host }}:{{ .Values.sharedInfra.rabbitmq.port }}"
|
MESSAGING_URI: "amqp://{{ .Values.sharedInfra.rabbitmq.username }}:{{ .Values.sharedInfra.rabbitmq.password }}@{{ .Values.sharedInfra.rabbitmq.host }}:{{ .Values.sharedInfra.rabbitmq.port }}"
|
||||||
@@ -206,11 +267,14 @@ spec:
|
|||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
env:
|
env:
|
||||||
- name: POSTGRES_PASSWORD
|
- name: POSTGRES_PASSWORD
|
||||||
value: "{{ .Values.sharedInfra.postgres.password }}"
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: {{ $pgCredentialsSecretName }}
|
||||||
|
key: {{ .Values.sharedInfra.postgres.credentialsSecret.adminPasswordKey }}
|
||||||
- name: POSTGRES_DB
|
- name: POSTGRES_DB
|
||||||
value: "{{ $database }}"
|
value: "{{ $database }}"
|
||||||
- name: POSTGRES_USER
|
- name: POSTGRES_USER
|
||||||
value: "{{ .Values.sharedInfra.postgres.username }}"
|
value: "{{ .Values.sharedInfra.postgres.adminUsername }}"
|
||||||
- name: PGDATA
|
- name: PGDATA
|
||||||
value: /var/lib/postgresql/data/pgdata
|
value: /var/lib/postgresql/data/pgdata
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -42,9 +42,16 @@ sharedInfra:
|
|||||||
host: ultra-service
|
host: ultra-service
|
||||||
port: 8080
|
port: 8080
|
||||||
postgres:
|
postgres:
|
||||||
username: postgres
|
adminUsername: postgres
|
||||||
password: "Admin12345"
|
appUsername: agent_runtime
|
||||||
port: 5432
|
port: 5432
|
||||||
|
idleInTransactionSessionTimeout: 60s
|
||||||
|
credentialsSecret:
|
||||||
|
# Leave empty to use <role>-<site>-postgres-credentials.
|
||||||
|
name: ""
|
||||||
|
adminPasswordKey: admin-password
|
||||||
|
appPasswordKey: app-password
|
||||||
|
connectionStringKey: connection-string
|
||||||
redis:
|
redis:
|
||||||
host: redis-service
|
host: redis-service
|
||||||
port: 6379
|
port: 6379
|
||||||
|
|||||||
Reference in new issue
Block a user