Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,17 @@ jobs:
run: node --test ../scripts/bench/lib.test.mjs
- name: Build (含类型检查)
run: pnpm build

# Helm chart 由社区维护:lint 两种数据库模式,并核对 appVersion 与 compose 的镜像默认标签
chart:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/setup-helm@v4
- name: appVersion matches docker-compose.yml
run: ./scripts/check_chart_version.sh
- name: Helm lint
run: |
helm lint deploy/chart
helm lint deploy/chart --set database.url=postgres://utopia@db:5432/utopia
helm lint deploy/chart --set database.mode=cloudnativepg
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Or build from source:
docker compose -f docker-compose.yml -f docker-compose.build.yml --profile app up -d --build
```

For Kubernetes, a community-maintained Helm chart lives in [`deploy/chart`](deploy/chart/README.md).

### Local development

```bash
Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ docker compose --profile app up -d
docker compose -f docker-compose.yml -f docker-compose.build.yml --profile app up -d --build
```

Kubernetes 上可用社区维护的 Helm chart,见 [`deploy/chart`](deploy/chart/README.md)。

### 本地开发

```bash
Expand Down
7 changes: 7 additions & 0 deletions deploy/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v2
name: utopia
description: Utopia, the bitemporal knowledge graph, as one replica with its own data volume
type: application
version: 0.1.0
# Kept equal to the image default in docker-compose.yml; CI checks it.
appVersion: "0.1.0-rc9"
65 changes: 65 additions & 0 deletions deploy/chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Utopia Helm chart

This chart is community-maintained. The supported way to run Utopia is still
`docker-compose.yml` at the root of the repo, and this chart follows it: the
image tag defaults to the chart's `appVersion`, which CI keeps equal to the
compose default.

It runs one replica with a `Recreate` rollout on a single `ReadWriteOnce`
volume, because the data directory (original files, full-text index) and the
worker belong to one process. Everything optional is off by default.

## With your own Postgres

You need Postgres 16 or newer with pgvector. Before the first start, create
the owner, the database, the extension and the `utopia_app` runtime role as a
superuser. This is what `docker/init-app-role.sh` does for compose:

```sql
CREATE ROLE utopia LOGIN PASSWORD '<owner password>';
CREATE DATABASE utopia OWNER utopia;
\c utopia
CREATE EXTENSION IF NOT EXISTS vector;
CREATE ROLE utopia_app LOGIN PASSWORD '<app password>';
GRANT CONNECT ON DATABASE utopia TO utopia_app;
```

The migrations run as the owner and grant `utopia_app` the tables it needs,
so the role must exist before the first start. The app then runs as
`utopia_app`, which can append to the audit log but not change it.

```bash
kubectl create secret generic utopia-db \
--from-literal=database-url='postgres://utopia_app:<app password>@<host>:5432/utopia' \
--from-literal=migration-url='postgres://utopia:<owner password>@<host>:5432/utopia'
helm install utopia deploy/chart --set database.existingSecret=utopia-db
```

To skip the runtime role, leave out `migration-url` and point `database-url`
at the owner. `database.url` and `database.migrationUrl` also work instead of
a Secret you create, and the chart then writes the Secret itself.

## With CloudNativePG

With the [CloudNativePG](https://cloudnative-pg.io) operator installed, the
chart can create a one-instance cluster with pgvector and `utopia_app`, and
generate its password:

```bash
helm install utopia deploy/chart --set database.mode=cloudnativepg
```

## Keys

Without other settings the app keeps its credential key in `secret.key` on the
data volume and its JWT secret in the database, so back up the volume together
with the database. To hold them in Kubernetes instead, set `keys.existingSecret` to
a Secret with the keys `secret-key` and `jwt-secret`.

## Reaching it

`kubectl port-forward svc/utopia 1516:80` opens it on http://localhost:1516.
For an Ingress, set `ingress.enabled=true` and `ingress.host`, plus
`ingress.tlsSecretName` for TLS. Uploads go up to 100 MB, so the controller
must allow request bodies that large. Other app settings, such as single
sign-on, go in `extraEnv`, with the names from `.env.example`.
8 changes: 8 additions & 0 deletions deploy/chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- define "utopia.labels" -}}
app.kubernetes.io/name: utopia
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{- define "utopia.dbSecret" -}}
{{- .Values.database.existingSecret | default (printf "%s-db" .Release.Name) -}}
{{- end -}}
14 changes: 14 additions & 0 deletions deploy/chart/templates/data-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Release.Name }}-data
labels:
{{- include "utopia.labels" . | nindent 4 }}
spec:
accessModes: [ReadWriteOnce]
{{- with .Values.data.storageClass }}
storageClassName: {{ . }}
{{- end }}
resources:
requests:
storage: {{ .Values.data.size }}
63 changes: 63 additions & 0 deletions deploy/chart/templates/database.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{- $mode := .Values.database.mode }}
{{- if not (has $mode (list "external" "cloudnativepg")) }}
{{- fail "database.mode must be external or cloudnativepg" }}
{{- end }}
{{- if and (eq $mode "external") (not .Values.database.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "utopia.dbSecret" . }}
labels:
{{- include "utopia.labels" . | nindent 4 }}
stringData:
database-url: {{ required "database.url or database.existingSecret is required" .Values.database.url | quote }}
{{- with .Values.database.migrationUrl }}
migration-url: {{ . | quote }}
{{- end }}
{{- end }}
{{- if eq $mode "cloudnativepg" }}
{{- $roleSecret := printf "%s-db-app-role" .Release.Name }}
{{- $existing := lookup "v1" "Secret" .Release.Namespace $roleSecret }}
apiVersion: v1
kind: Secret
metadata:
name: {{ $roleSecret }}
labels:
{{- include "utopia.labels" . | nindent 4 }}
type: kubernetes.io/basic-auth
data:
username: {{ "utopia_app" | b64enc }}
password: {{ dig "data" "password" (randAlphaNum 32 | b64enc) $existing }}
---
# The owner utopia runs the migrations. utopia_app must exist before the
# first one, which grants it the tables only when the role is already there.
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: {{ .Release.Name }}-db
labels:
{{- include "utopia.labels" . | nindent 4 }}
spec:
instances: 1
imageName: {{ .Values.database.cloudnativepg.image }}
bootstrap:
initdb:
database: utopia
owner: utopia
postInitApplicationSQL:
- CREATE EXTENSION IF NOT EXISTS vector
- CREATE ROLE utopia_app LOGIN
- GRANT CONNECT ON DATABASE utopia TO utopia_app
managed:
roles:
- name: utopia_app
ensure: present
login: true
passwordSecret:
name: {{ $roleSecret }}
storage:
size: {{ .Values.database.cloudnativepg.size }}
{{- with .Values.database.cloudnativepg.storageClass }}
storageClass: {{ . }}
{{- end }}
{{- end }}
96 changes: 96 additions & 0 deletions deploy/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}
labels:
{{- include "utopia.labels" . | nindent 4 }}
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
{{- include "utopia.labels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "utopia.labels" . | nindent 8 }}
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
fsGroupChangePolicy: OnRootMismatch
seccompProfile:
type: RuntimeDefault
containers:
- name: utopia
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: [ALL]
ports:
- name: http
containerPort: 1516
env:
{{- if eq .Values.database.mode "cloudnativepg" }}
- name: UTOPIA_APP_DB_PASSWORD
valueFrom:
secretKeyRef: { name: {{ .Release.Name }}-db-app-role, key: password }
- name: UTOPIA_DATABASE_URL
value: postgres://utopia_app:$(UTOPIA_APP_DB_PASSWORD)@{{ .Release.Name }}-db-rw:5432/utopia
- name: UTOPIA_MIGRATION_URL
valueFrom:
secretKeyRef: { name: {{ .Release.Name }}-db-app, key: uri }
{{- else }}
- name: UTOPIA_DATABASE_URL
valueFrom:
secretKeyRef: { name: {{ include "utopia.dbSecret" . }}, key: database-url }
- name: UTOPIA_MIGRATION_URL
valueFrom:
secretKeyRef: { name: {{ include "utopia.dbSecret" . }}, key: migration-url, optional: true }
{{- end }}
{{- with .Values.keys.existingSecret }}
- name: UTOPIA_SECRET_KEY
valueFrom:
secretKeyRef: { name: {{ . }}, key: secret-key, optional: true }
- name: UTOPIA_JWT_SECRET
valueFrom:
secretKeyRef: { name: {{ . }}, key: jwt-secret, optional: true }
{{- end }}
- name: UTOPIA_BIND_ADDR
value: 0.0.0.0:1516
- name: UTOPIA_WEB_DIST
value: /app/web-dist
- name: UTOPIA_DATA_DIR
value: /app/data
{{- with .Values.extraEnv }}
{{- toYaml . | nindent 12 }}
{{- end }}
volumeMounts:
- { name: data, mountPath: /app/data }
- { name: tmp, mountPath: /tmp }
# Migrations run before the port opens and can take long on a large
# base, so liveness waits for the startup probe.
startupProbe:
httpGet: { path: /api/v1/health, port: http }
periodSeconds: 10
failureThreshold: 360
readinessProbe:
httpGet: { path: /api/v1/health, port: http }
periodSeconds: 10
livenessProbe:
httpGet: { path: /api/v1/health, port: http }
periodSeconds: 30
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
- name: data
persistentVolumeClaim:
claimName: {{ .Release.Name }}-data
- name: tmp
emptyDir: {}
32 changes: 32 additions & 0 deletions deploy/chart/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}
labels:
{{- include "utopia.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- with .Values.ingress.className }}
ingressClassName: {{ . }}
{{- end }}
{{- with .Values.ingress.tlsSecretName }}
tls:
- hosts: [{{ $.Values.ingress.host | quote }}]
secretName: {{ . }}
{{- end }}
rules:
- host: {{ .Values.ingress.host | quote }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ .Release.Name }}
port:
name: http
{{- end }}
13 changes: 13 additions & 0 deletions deploy/chart/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}
labels:
{{- include "utopia.labels" . | nindent 4 }}
spec:
selector:
{{- include "utopia.labels" . | nindent 4 }}
ports:
- name: http
port: {{ .Values.service.port }}
targetPort: http
52 changes: 52 additions & 0 deletions deploy/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
image:
repository: ghcr.io/deeplethe/utopia
# Empty: the chart's appVersion.
tag: ""
pullPolicy: IfNotPresent

# Original files and the full-text index. One disk, one writer, so the
# Deployment runs one replica and recreates rather than rolls.
data:
size: 20Gi
storageClass: ""

database:
# external: your own Postgres 16+ with pgvector.
# cloudnativepg: a CloudNativePG Cluster made by this chart (needs the operator).
mode: external
# external mode. Either name a Secret with the keys database-url and
# migration-url (optional), or give the URLs here and the chart writes one.
existingSecret: ""
url: ""
migrationUrl: ""
# cloudnativepg mode.
cloudnativepg:
image: ghcr.io/cloudnative-pg/postgresql:16-bookworm
size: 20Gi
storageClass: ""

# Optional Secret with the keys secret-key and jwt-secret. Without it the app
# keeps the key in the data volume and the JWT secret in the database.
keys:
existingSecret: ""

# Extra container environment, as Kubernetes EnvVar entries.
extraEnv: []
# - name: UTOPIA_DB_MAX_CONNECTIONS
# value: "32"

resources:
requests: { cpu: 250m, memory: 512Mi }
limits: { memory: 2Gi }

service:
port: 80

# The controller must allow request bodies up to 100 MB (uploads).
ingress:
enabled: false
className: ""
host: utopia.example.com
# Name of the TLS Secret; empty: no tls section.
tlsSecretName: ""
annotations: {}
Loading
Loading