Skip to content

Commit 1ce4e3b

Browse files
committed
A Helm chart runs the server with one replica on its own volume, against your own Postgres or a CloudNativePG one
Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com>
1 parent 969f407 commit 1ce4e3b

13 files changed

Lines changed: 444 additions & 0 deletions

File tree

‎.github/workflows/checks.yml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,41 @@ jobs:
187187
run: node --test ../scripts/bench/lib.test.mjs
188188
- name: Build (含类型检查)
189189
run: pnpm build
190+
191+
# Helm chart 由社区维护:lint 并渲染两种数据库模式、用 kubeconform 校验,并核对 appVersion 与 compose 的镜像默认标签
192+
chart:
193+
runs-on: ubuntu-latest
194+
steps:
195+
- uses: actions/checkout@v4
196+
- uses: azure/setup-helm@v4
197+
- name: appVersion matches docker-compose.yml
198+
run: ./scripts/check_chart_version.sh
199+
- name: Helm lint
200+
run: |
201+
helm lint deploy/chart --set database.url=postgres://utopia@db:5432/utopia
202+
helm lint deploy/chart --set database.mode=cloudnativepg
203+
# helm lint 只把缺失的 required 值当提示,这里确认没配数据库时渲染真的失败
204+
- name: Render without settings fails
205+
run: |
206+
if out=$(helm template utopia deploy/chart 2>&1); then
207+
echo "::error::helm template succeeded without database settings"
208+
exit 1
209+
fi
210+
grep -F "database.url or database.existingSecret is required" <<<"$out"
211+
- name: Install kubeconform
212+
run: |
213+
curl -sSfLo kubeconform.tar.gz https://github.com/yannh/kubeconform/releases/download/v0.8.0/kubeconform-linux-amd64.tar.gz
214+
echo "9bc2bffbf71f261128533edaf912153948b7ff238f9a531ae6d34466ec287883 kubeconform.tar.gz" | sha256sum -c
215+
sudo tar -xzf kubeconform.tar.gz -C /usr/local/bin kubeconform
216+
# CloudNativePG 的 Cluster 用 datreeio/CRDs-catalog 固定提交里的 schema 校验
217+
- name: Render and validate
218+
run: |
219+
crds='https://raw.githubusercontent.com/datreeio/CRDs-catalog/a156431e79b70bc42c4a280a90abc1d5f1045e69/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'
220+
for settings in \
221+
"database.url=postgres://utopia@db:5432/utopia" \
222+
"database.existingSecret=utopia-db,keys.existingSecret=utopia-keys,ingress.enabled=true,ingress.tlsSecretName=utopia-tls" \
223+
"database.mode=cloudnativepg" \
224+
"database.mode=cloudnativepg,database.cloudnativepg.existingSecret=utopia-db-role"; do
225+
helm template utopia deploy/chart --set "$settings" \
226+
| kubeconform -strict -summary -schema-location default -schema-location "$crds"
227+
done

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Or build from source:
107107
docker compose -f docker-compose.yml -f docker-compose.build.yml --profile app up -d --build
108108
```
109109

110+
For Kubernetes, a community-maintained Helm chart lives in [`deploy/chart`](deploy/chart/README.md).
111+
110112
### Local development
111113

112114
```bash

‎README.zh-CN.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ docker compose --profile app up -d
108108
docker compose -f docker-compose.yml -f docker-compose.build.yml --profile app up -d --build
109109
```
110110

111+
Kubernetes 上可用社区维护的 Helm chart,见 [`deploy/chart`](deploy/chart/README.md)。
112+
111113
### 本地开发
112114

113115
```bash

‎deploy/chart/Chart.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v2
2+
name: utopia
3+
description: Utopia, the bitemporal knowledge graph, as one replica with its own data volume
4+
type: application
5+
version: 0.1.0
6+
# Kept equal to the image default in docker-compose.yml; CI checks it.
7+
appVersion: "0.1.0-rc9"

‎deploy/chart/README.md‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Utopia Helm chart
2+
3+
This chart is community-maintained. The supported way to run Utopia is still
4+
`docker-compose.yml` at the root of the repo, and this chart follows it: the
5+
image tag defaults to the chart's `appVersion`, which CI keeps equal to the
6+
compose default.
7+
8+
It runs one replica with a `Recreate` rollout on a single `ReadWriteOnce`
9+
volume, because the data directory (original files, full-text index) and the
10+
worker belong to one process. Everything optional is off by default.
11+
12+
## With your own Postgres
13+
14+
You need Postgres 16 or newer with pgvector. Before the first start, create
15+
the owner, the database, the extension and the `utopia_app` runtime role as a
16+
superuser. This is what `docker/init-app-role.sh` does for compose:
17+
18+
```sql
19+
CREATE ROLE utopia LOGIN PASSWORD '<owner password>';
20+
CREATE DATABASE utopia OWNER utopia;
21+
\c utopia
22+
CREATE EXTENSION IF NOT EXISTS vector;
23+
CREATE ROLE utopia_app LOGIN PASSWORD '<app password>';
24+
GRANT CONNECT ON DATABASE utopia TO utopia_app;
25+
```
26+
27+
The migrations run as the owner and grant `utopia_app` the tables it needs,
28+
so the role must exist before the first start. The app then runs as
29+
`utopia_app`, which can append to the audit log but not change it.
30+
31+
```bash
32+
kubectl create secret generic utopia-db \
33+
--from-literal=database-url='postgres://utopia_app:<app password>@<host>:5432/utopia' \
34+
--from-literal=migration-url='postgres://utopia:<owner password>@<host>:5432/utopia'
35+
helm install utopia deploy/chart --set database.existingSecret=utopia-db
36+
```
37+
38+
To skip the runtime role, leave out `migration-url` and point `database-url`
39+
at the owner. `database.url` and `database.migrationUrl` also work instead of
40+
a Secret you create, and the chart then writes the Secret itself.
41+
42+
## With CloudNativePG
43+
44+
With the [CloudNativePG](https://cloudnative-pg.io) operator installed, the
45+
chart can create a one-instance cluster with pgvector and `utopia_app`, and
46+
generate its password:
47+
48+
```bash
49+
helm install utopia deploy/chart --set database.mode=cloudnativepg
50+
```
51+
52+
The generated password survives upgrades only through Helm's `lookup`, which
53+
returns nothing under `helm template`, `--dry-run`, Argo CD and Flux. There it
54+
would change on every render, so create the Secret yourself and name it:
55+
56+
```bash
57+
kubectl create secret generic utopia-db-role --type=kubernetes.io/basic-auth \
58+
--from-literal=username=utopia_app --from-literal=password="$(openssl rand -hex 24)"
59+
helm install utopia deploy/chart --set database.mode=cloudnativepg \
60+
--set database.cloudnativepg.existingSecret=utopia-db-role
61+
```
62+
63+
The default image is a CloudNativePG `standard` image, which ships pgvector.
64+
If you set `database.cloudnativepg.image`, keep a `-standard-` tag.
65+
66+
## Keys
67+
68+
Without other settings the app keeps its credential key in `secret.key` on the
69+
data volume and its JWT secret in the database, so back up the volume together
70+
with the database. To hold them in Kubernetes instead, set `keys.existingSecret` to
71+
a Secret with the keys `secret-key` and `jwt-secret`.
72+
73+
## Moving from compose
74+
75+
The pod runs as uid 1000, while compose runs the app as root, so a `data`
76+
directory copied from compose is owned by root and the app cannot write to it.
77+
Run `sudo chown -R 1000:1000 data` before copying it, and copy with a tool that
78+
keeps the owner, such as `tar -p` or `rsync -a`. Copy the database with
79+
`pg_dump` and `pg_restore` as usual.
80+
81+
## Reaching it
82+
83+
`kubectl port-forward svc/utopia 1516:80` opens it on http://localhost:1516.
84+
For an Ingress, set `ingress.enabled=true` and `ingress.host`, plus
85+
`ingress.tlsSecretName` for TLS, which also sets `UTOPIA_COOKIE_SECURE=true`.
86+
If TLS ends somewhere else, set `ingress.cookieSecure` to `true` or `false`
87+
yourself. Uploads go up to 100 MB, so the controller
88+
must allow request bodies that large. Other app settings, such as single
89+
sign-on, go in `extraEnv`, with the names from `.env.example`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{- define "utopia.labels" -}}
2+
app.kubernetes.io/name: utopia
3+
app.kubernetes.io/instance: {{ .Release.Name }}
4+
{{- end -}}
5+
6+
{{- define "utopia.dbSecret" -}}
7+
{{- .Values.database.existingSecret | default (printf "%s-db" .Release.Name) -}}
8+
{{- end -}}
9+
10+
{{- define "utopia.appRoleSecret" -}}
11+
{{- .Values.database.cloudnativepg.existingSecret | default (printf "%s-db-app-role" .Release.Name) -}}
12+
{{- end -}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: {{ .Release.Name }}-data
5+
labels:
6+
{{- include "utopia.labels" . | nindent 4 }}
7+
spec:
8+
accessModes: [ReadWriteOnce]
9+
{{- with .Values.data.storageClass }}
10+
storageClassName: {{ . }}
11+
{{- end }}
12+
resources:
13+
requests:
14+
storage: {{ .Values.data.size }}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{{- $mode := .Values.database.mode }}
2+
{{- if not (has $mode (list "external" "cloudnativepg")) }}
3+
{{- fail "database.mode must be external or cloudnativepg" }}
4+
{{- end }}
5+
{{- if and (eq $mode "external") (not .Values.database.existingSecret) }}
6+
apiVersion: v1
7+
kind: Secret
8+
metadata:
9+
name: {{ include "utopia.dbSecret" . }}
10+
labels:
11+
{{- include "utopia.labels" . | nindent 4 }}
12+
stringData:
13+
database-url: {{ required "database.url or database.existingSecret is required" .Values.database.url | quote }}
14+
{{- with .Values.database.migrationUrl }}
15+
migration-url: {{ . | quote }}
16+
{{- end }}
17+
{{- end }}
18+
{{- if eq $mode "cloudnativepg" }}
19+
{{- $roleSecret := include "utopia.appRoleSecret" . }}
20+
{{- if not .Values.database.cloudnativepg.existingSecret }}
21+
{{- $existing := lookup "v1" "Secret" .Release.Namespace $roleSecret }}
22+
apiVersion: v1
23+
kind: Secret
24+
metadata:
25+
name: {{ $roleSecret }}
26+
labels:
27+
{{- include "utopia.labels" . | nindent 4 }}
28+
type: kubernetes.io/basic-auth
29+
data:
30+
username: {{ "utopia_app" | b64enc }}
31+
password: {{ dig "data" "password" (randAlphaNum 32 | b64enc) $existing }}
32+
---
33+
{{- end }}
34+
# The owner utopia runs the migrations. utopia_app must exist before the
35+
# first one, which grants it the tables only when the role is already there.
36+
apiVersion: postgresql.cnpg.io/v1
37+
kind: Cluster
38+
metadata:
39+
name: {{ .Release.Name }}-db
40+
labels:
41+
{{- include "utopia.labels" . | nindent 4 }}
42+
spec:
43+
instances: 1
44+
imageName: {{ .Values.database.cloudnativepg.image }}
45+
bootstrap:
46+
initdb:
47+
database: utopia
48+
owner: utopia
49+
postInitApplicationSQL:
50+
- CREATE EXTENSION IF NOT EXISTS vector
51+
- CREATE ROLE utopia_app LOGIN
52+
- GRANT CONNECT ON DATABASE utopia TO utopia_app
53+
managed:
54+
roles:
55+
- name: utopia_app
56+
ensure: present
57+
login: true
58+
passwordSecret:
59+
name: {{ $roleSecret }}
60+
storage:
61+
size: {{ .Values.database.cloudnativepg.size }}
62+
{{- with .Values.database.cloudnativepg.storageClass }}
63+
storageClass: {{ . }}
64+
{{- end }}
65+
{{- end }}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ .Release.Name }}
5+
labels:
6+
{{- include "utopia.labels" . | nindent 4 }}
7+
spec:
8+
replicas: 1
9+
strategy:
10+
type: Recreate
11+
selector:
12+
matchLabels:
13+
{{- include "utopia.labels" . | nindent 6 }}
14+
template:
15+
metadata:
16+
labels:
17+
{{- include "utopia.labels" . | nindent 8 }}
18+
spec:
19+
securityContext:
20+
runAsNonRoot: true
21+
runAsUser: 1000
22+
runAsGroup: 1000
23+
fsGroup: 1000
24+
fsGroupChangePolicy: OnRootMismatch
25+
seccompProfile:
26+
type: RuntimeDefault
27+
containers:
28+
- name: utopia
29+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
30+
imagePullPolicy: {{ .Values.image.pullPolicy }}
31+
securityContext:
32+
allowPrivilegeEscalation: false
33+
readOnlyRootFilesystem: true
34+
capabilities:
35+
drop: [ALL]
36+
ports:
37+
- name: http
38+
containerPort: 1516
39+
env:
40+
{{- if eq .Values.database.mode "cloudnativepg" }}
41+
- name: UTOPIA_APP_DB_PASSWORD
42+
valueFrom:
43+
secretKeyRef: { name: {{ include "utopia.appRoleSecret" . }}, key: password }
44+
- name: UTOPIA_DATABASE_URL
45+
value: postgres://utopia_app:$(UTOPIA_APP_DB_PASSWORD)@{{ .Release.Name }}-db-rw:5432/utopia
46+
- name: UTOPIA_MIGRATION_URL
47+
valueFrom:
48+
secretKeyRef: { name: {{ .Release.Name }}-db-app, key: uri }
49+
{{- else }}
50+
- name: UTOPIA_DATABASE_URL
51+
valueFrom:
52+
secretKeyRef: { name: {{ include "utopia.dbSecret" . }}, key: database-url }
53+
- name: UTOPIA_MIGRATION_URL
54+
valueFrom:
55+
secretKeyRef: { name: {{ include "utopia.dbSecret" . }}, key: migration-url, optional: true }
56+
{{- end }}
57+
{{- with .Values.keys.existingSecret }}
58+
- name: UTOPIA_SECRET_KEY
59+
valueFrom:
60+
secretKeyRef: { name: {{ . }}, key: secret-key, optional: true }
61+
- name: UTOPIA_JWT_SECRET
62+
valueFrom:
63+
secretKeyRef: { name: {{ . }}, key: jwt-secret, optional: true }
64+
{{- end }}
65+
- name: UTOPIA_BIND_ADDR
66+
value: 0.0.0.0:1516
67+
- name: UTOPIA_WEB_DIST
68+
value: /app/web-dist
69+
- name: UTOPIA_DATA_DIR
70+
value: /app/data
71+
{{- $tls := and .Values.ingress.enabled .Values.ingress.tlsSecretName }}
72+
- name: UTOPIA_COOKIE_SECURE
73+
value: {{ toString .Values.ingress.cookieSecure | default (ternary "true" "false" (not (empty $tls))) | quote }}
74+
{{- with .Values.extraEnv }}
75+
{{- toYaml . | nindent 12 }}
76+
{{- end }}
77+
volumeMounts:
78+
- { name: data, mountPath: /app/data }
79+
- { name: tmp, mountPath: /tmp }
80+
# Migrations run before the port opens and can take long on a large
81+
# base, so liveness waits for the startup probe.
82+
startupProbe:
83+
httpGet: { path: /api/v1/health, port: http }
84+
periodSeconds: 10
85+
failureThreshold: 360
86+
readinessProbe:
87+
httpGet: { path: /api/v1/health, port: http }
88+
periodSeconds: 10
89+
livenessProbe:
90+
httpGet: { path: /api/v1/health, port: http }
91+
periodSeconds: 30
92+
resources:
93+
{{- toYaml .Values.resources | nindent 12 }}
94+
volumes:
95+
- name: data
96+
persistentVolumeClaim:
97+
claimName: {{ .Release.Name }}-data
98+
- name: tmp
99+
emptyDir: {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{- if .Values.ingress.enabled }}
2+
apiVersion: networking.k8s.io/v1
3+
kind: Ingress
4+
metadata:
5+
name: {{ .Release.Name }}
6+
labels:
7+
{{- include "utopia.labels" . | nindent 4 }}
8+
{{- with .Values.ingress.annotations }}
9+
annotations:
10+
{{- toYaml . | nindent 4 }}
11+
{{- end }}
12+
spec:
13+
{{- with .Values.ingress.className }}
14+
ingressClassName: {{ . }}
15+
{{- end }}
16+
{{- with .Values.ingress.tlsSecretName }}
17+
tls:
18+
- hosts: [{{ $.Values.ingress.host | quote }}]
19+
secretName: {{ . }}
20+
{{- end }}
21+
rules:
22+
- host: {{ .Values.ingress.host | quote }}
23+
http:
24+
paths:
25+
- path: /
26+
pathType: Prefix
27+
backend:
28+
service:
29+
name: {{ .Release.Name }}
30+
port:
31+
name: http
32+
{{- end }}

0 commit comments

Comments
 (0)