Skip to content

Commit

Permalink
feat(prefect-server): support sqlite database (#418)
Browse files Browse the repository at this point in the history
* feat(prefect-server): support sqlite database

Supports using sqlite for the database in place of postgresql.

Closes https://linear.app/prefect/issue/PLA-139/support-sqlite-database

* Disable hpa, secret if sqlite enabled

- HPA is not desired given that sqlite requires 1 replica
- secret is postgresql-specific

* Add tests for sqlite settings

* Fix resiliency of separate test

This test, separate from my changes, failed because it was looking for a
volumeMount at a specific index. This change allows it to just confirm
the entry is present without worrying about a specific location in the
list.

* Fix mount setting, hosting at /data

The application was failing to start, saying it couldn't access the
database file.

This change mounts a directory without a subPath setting, allowing the
application to create the file instead.

* Make persistence settings more configurable

* Add to values schema

* Add documentation on enabling sqlite

* Replace hard-coded PVC name

Uses a template to calculate the PVC name, matching the pattern used for
other objects like the Deployment.
  • Loading branch information
mitchnielsen authored Dec 5, 2024
1 parent 1904f18 commit f1e9d3a
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 7 deletions.
27 changes: 27 additions & 0 deletions charts/prefect-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ prefect-server:
postgresql+asyncpg://{username}:{password}@{hostname}/{database}?ssl=verify-ca
```

## SQLite Configuration

SQLite can be used as an alternative to PostgreSQL. As mentioned in the
[documentation on hosting Prefect](https://docs-3.prefect.io/v3/manage/self-host),
SQLite is only recommended for lightweight, single-server deployments.

To use SQLite for the database, provide the following configuration values:

```yaml
postgresql:
enabled: false
sqlite:
enabled: true
```

More configuration options are available in [`values.yaml`](./values.yaml).

By default, a PersistentVolumeClaim persists the SQLite database file between
Pod restarts.

Note that enabling SQLite enforces 1 replica in the Deployment, and disables
the HorizontalPodAutoscaler.

## Maintainers

| Name | Email | Url |
Expand Down Expand Up @@ -227,6 +250,10 @@ prefect-server:
| serviceAccount.annotations | object | `{}` | additional service account annotations (evaluated as a template) |
| serviceAccount.create | bool | `true` | specifies whether a ServiceAccount should be created |
| serviceAccount.name | string | `""` | the name of the ServiceAccount to use. if not set and create is true, a name is generated using the common.names.fullname template |
| sqlite.enabled | bool | `false` | enable use of the embedded SQLite database |
| sqlite.persistence.enabled | bool | `true` | enable SQLite data persistence using PVC |
| sqlite.persistence.size | string | `"1Gi"` | size for the PVC |
| sqlite.persistence.storageClassName | string | `""` | storage class name for the PVC |

----------------------------------------------
Autogenerated from chart metadata using [helm-docs v1.13.1](https://github.com/norwoodj/helm-docs/releases/v1.13.1)
15 changes: 14 additions & 1 deletion charts/prefect-server/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ metadata:
{{- end }}
spec:
revisionHistoryLimit: {{ .Values.server.revisionHistoryLimit }}
replicas: {{ .Values.server.replicaCount }}
replicas: {{ .Values.sqlite.enabled | ternary 1 .Values.server.replicaCount }}
selector:
matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
app.kubernetes.io/component: server
Expand Down Expand Up @@ -99,10 +99,14 @@ spec:
- name: PREFECT_UI_STATIC_DIRECTORY
value: {{ .Values.server.uiConfig.prefectUiStaticDirectory | quote }}
- name: PREFECT_API_DATABASE_CONNECTION_URL
{{- if .Values.sqlite.enabled }}
value: "sqlite+aiosqlite:////data/prefect.db"
{{- else }}
valueFrom:
secretKeyRef:
name: {{ include "server.postgres-string-secret-name" . }}
key: connection-string
{{- end }}
{{- if .Values.server.env }}
{{- include "common.tplvalues.render" (dict "value" .Values.server.env "context" $) | nindent 12 }}
{{- end }}
Expand Down Expand Up @@ -145,6 +149,10 @@ spec:
- mountPath: {{ .Values.server.uiConfig.prefectUiStaticDirectory }}
name: scratch
subPathExpr: ui-build
{{- if .Values.sqlite.enabled }}
- mountPath: /data
name: sqlite-storage
{{- end }}
{{- if .Values.server.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.server.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
Expand All @@ -154,6 +162,11 @@ spec:
volumes:
- name: scratch
emptyDir: {}
{{- if .Values.sqlite.enabled }}
- name: sqlite-storage
persistentVolumeClaim:
claimName: {{ template "common.names.fullname" . }}-sqlite
{{- end }}
{{- if .Values.server.extraVolumes }}
{{- include "common.tplvalues.render" (dict "value" .Values.server.extraVolumes "context" $) | nindent 8 }}
{{- end }}
2 changes: 1 addition & 1 deletion charts/prefect-server/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if and .Values.server.autoscaling.enabled .Values.server.resources.requests }}
{{- if and .Values.server.autoscaling.enabled .Values.server.resources.requests (not .Values.sqlite.enabled) }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
Expand Down
16 changes: 16 additions & 0 deletions charts/prefect-server/templates/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- if and .Values.sqlite.enabled .Values.sqlite.persistence.enabled }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ template "common.names.fullname" . }}-sqlite
namespace: {{.Release.Namespace}}
spec:
{{- with .Values.sqlite.persistence.storageClassName }}
storageClassName: {{.}}
{{- end }}
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{.Values.sqlite.persistence.size}}
{{- end }}
2 changes: 1 addition & 1 deletion charts/prefect-server/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if .Values.secret.create }}
{{- if and .Values.secret.create (not .Values.sqlite.enabled) }}
apiVersion: v1
kind: Secret
metadata:
Expand Down
75 changes: 75 additions & 0 deletions charts/prefect-server/tests/database_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,81 @@ envSecretPath: &envSecretPath .spec.template.spec.containers[?(@.name == "prefec
defaultSecretName: &defaultSecretName prefect-server-postgresql-connection

tests:
# SQLite embedded database tests

- it: Should correctly configure SQLite connection string
set:
postgresql:
enabled: false
sqlite:
enabled: true
asserts:
- template: deployment.yaml
contains:
path: .spec.template.spec.containers[0].env
content:
name: PREFECT_API_DATABASE_CONNECTION_URL
value: sqlite+aiosqlite:////data/prefect.db

- it: Should correctly configure SQLite storage settings
set:
postgresql:
enabled: false
sqlite:
enabled: true
asserts:
- template: deployment.yaml
contains:
path: .spec.template.spec.volumes
content:
name: sqlite-storage
persistentVolumeClaim:
claimName: prefect-server-sqlite
- template: deployment.yaml
contains:
path: .spec.template.spec.containers[0].volumeMounts
content:
mountPath: /data
name: sqlite-storage
- template: pvc.yaml
containsDocument:
kind: PersistentVolumeClaim
apiVersion: v1
name: prefect-server-sqlite
namespace: prefect

- it: Should not allow an HPA
set:
postgresql:
enabled: false
sqlite:
enabled: true
server:
autoscaling:
enabled: true
asserts:
- template: hpa.yaml
not: true
containsDocument:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
name: prefect-server
namespace: prefect

- it: Should not allow more than 1 replica
set:
postgresql:
enabled: false
sqlite:
enabled: true
server:
replicaCount: 5
asserts:
- template: deployment.yaml
equal:
path: .spec.replicas
value: 1

# Bundled PostgreSQL chart tests

- it: Should produce the expected secret name and content with the defaults
Expand Down
10 changes: 6 additions & 4 deletions charts/prefect-server/tests/server_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,11 @@ tests:
mountPath: /app/config
asserts:
- template: deployment.yaml
equal:
path: .spec.template.spec.containers[0].volumeMounts[3].name
value: config
contains:
path: .spec.template.spec.containers[0].volumeMounts
content:
name: config
mountPath: /app/config

- it: Should set extra arguments
set:
Expand Down Expand Up @@ -600,4 +602,4 @@ tests:
- template: hpa.yaml
equal:
path: .spec.metrics[1].resource.target.averageUtilization
value: 80
value: 80
36 changes: 36 additions & 0 deletions charts/prefect-server/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,42 @@
}
}
},
"sqlite": {
"type": "object",
"title": "SQLite",
"description": "sqlite configuration",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "enable sqlite database"
},
"persistence": {
"type": "object",
"title": "Persistence",
"description": "persistence settings for sqlite",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "enable sqlite database persistence"
},
"size": {
"type": "string",
"title": "Size",
"description": "size of the persistent volume claim"
},
"storageClassName": {
"type": "string",
"title": "Storage class name",
"description": "storage class name for the persistent volume claim"
}
}
}
}
},
"serviceAccount": {
"type": "object",
"title": "Service Account",
Expand Down
14 changes: 14 additions & 0 deletions charts/prefect-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ secret:
# -- database for the PostgreSQL connection string
database: ""

# SQLite configuration
# Recommended for lightweight, single-server deployments.
sqlite:
# -- enable use of the embedded SQLite database
enabled: false

persistence:
# -- enable SQLite data persistence using PVC
enabled: true
# -- size for the PVC
size: 1Gi
# -- storage class name for the PVC
storageClassName: ""

# PostgreSQL subchart - default overrides
postgresql:
# -- enable use of bitnami/postgresql subchart
Expand Down

0 comments on commit f1e9d3a

Please sign in to comment.