Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create psql templates to reduce boilerplace #1668

Merged
merged 6 commits into from
Oct 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion library/common-test/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ icon: https://localhost/icon
dependencies:
- name: common
repository: file://../common
version: ~1.1.0
version: ~1.2.0
maintainers:
- name: truenas
url: https://www.truenas.com/
Expand Down
2 changes: 1 addition & 1 deletion library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: common
description: A library chart for iX Official Catalog
type: library
version: 1.1.1
version: 1.2.0
appVersion: v1
annotations:
title: Common Library Chart
Expand Down
104 changes: 90 additions & 14 deletions library/common/templates/app_functions/_postgres.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ backupChownMode (optional): Whether to chown the backup directory or
{{- $backupChownMode := .backupChownMode | default "check" -}}
{{- $ixChartContext := .ixChartContext -}}
{{- $resources := (required "Postgres - Resources are required" .resources) }}

{{ $name }}:
enabled: true
type: Deployment
Expand Down Expand Up @@ -61,20 +62,25 @@ backupChownMode (optional): Whether to chown the backup directory or
- -c
- "until pg_isready -U ${POSTGRES_USER} -h localhost; do sleep 2; done"
initContainers:
{{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999) | nindent 6 }}
{{- $enableBackupJob := false -}}
{{- if hasKey $ixChartContext "isUpgrade" -}}
{{- if $ixChartContext.isUpgrade -}}
{{- include "ix.v1.common.app.permissions"
(dict
"UID" 999
"GID" 999
"type" "install"
"containerName" "permissions"
) | nindent 6 }}

{{- $enableBackupJob := false -}}
{{- if hasKey $ixChartContext "isUpgrade" -}}
{{- if $ixChartContext.isUpgrade -}}
{{- $enableBackupJob = true -}}
{{- end -}}
{{- else -}}
{{/* If the key is not present in ixChartContext, means we
are outside SCALE (Probably CI), let upgrade job run */}}
{{- $enableBackupJob = true -}}
{{- end -}}
{{- else -}}
{{/*
If the key is not present in ixChartContext,
means we are outside SCALE (Probably CI),
let upgrade job run
*/}}
{{- $enableBackupJob = true -}}
{{- end }}
{{- end }}

postgresbackup:
enabled: {{ $enableBackupJob }}
type: Job
Expand Down Expand Up @@ -116,7 +122,14 @@ postgresbackup:
pg_dump --dbname=${POSTGRES_URL} --file {{ $backupPath }}/${POSTGRES_DB}_$(date +%Y-%m-%d_%H-%M-%S).sql || echo "Failed to create backup"
echo "Backup finished"
initContainers:
{{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999 "type" "init" "mode" $backupChownMode) | nindent 6 }}
{{- include "ix.v1.common.app.permissions"
(dict
"UID" 999
"GID" 999
"type" "init"
"mode" $backupChownMode
"containerName" "permissions"
) | nindent 6 }}
{{- end -}}

{{/* Returns a postgres-wait container for waiting for postgres to be ready */}}
Expand All @@ -130,6 +143,7 @@ secretName (required): Name of the secret containing the postgres credentials
{{- define "ix.v1.common.app.postgresWait" -}}
{{- $name := .name | default "postgres-wait" -}}
{{- $secretName := (required "Postgres-Wait - Secret Name is required" .secretName) }}

{{ $name }}:
enabled: true
type: init
Expand All @@ -150,3 +164,65 @@ secretName (required): Name of the secret containing the postgres credentials
sleep 2
done
{{- end -}}

{{/* Returns persistence entries for postgres */}}
{{/* Call this template:
{{ include "ix.v1.common.app.postgresPersistence" (dict "pgData" .Values.storage.pgData "pgBackup" .Values.storage.pgBackup) }}

pgData (required): Data persistence configuration
pgBackup (required): Data persistence configuration for backup
*/}}

{{- define "ix.v1.common.app.postgresPersistence" -}}
{{- $data := .pgData -}}
{{- $backup := .pgBackup }}

{{- if not $data -}}
{{- fail "Postgres - Data persistence configuration is required" -}}
{{- end -}}

{{- if not $backup -}}
{{- fail "Postgres - Backup persistence configuration is required" -}}
{{- end -}}

postgresdata:
enabled: true
type: {{ $data.type }}
datasetName: {{ $data.datasetName | default "" }}
hostPath: {{ $data.hostPath | default "" }}
targetSelector:
postgres:
postgres:
mountPath: /var/lib/postgresql/data
permissions:
mountPath: /mnt/directories/postgres_data
postgresbackup:
enabled: true
type: {{ $backup.type }}
datasetName: {{ $backup.datasetName | default "" }}
hostPath: {{ $backup.hostPath | default "" }}
targetSelector:
postgresbackup:
postgresbackup:
mountPath: /postgres_backup
permissions:
mountPath: /mnt/directories/postgres_backup
{{- end -}}

{{/* Returns service entry for postgres */}}
{{/* Call this template:
{{ include "ix.v1.common.app.postgresService" . }}
*/}}
{{- define "ix.v1.common.app.postgresService" -}}
postgres:
enabled: true
type: ClusterIP
targetSelector: postgres
ports:
postgres:
enabled: true
primary: true
port: 5432
targetPort: 5432
targetSelector: postgres
{{- end -}}
6 changes: 3 additions & 3 deletions library/ix-dev/community/briefkasten/Chart.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
- name: common
repository: file://../../../common
version: 1.1.1
digest: sha256:a7dbe3e4d42dbcd4325776e5e01a1d630c7f185f79e7ebf22b1b9cc80f56eed7
generated: "2023-09-21T17:36:42.077526429+03:00"
version: 1.2.0
digest: sha256:a87a6988cedfc3f8a90eb308dffaaeaa1cbd0cad31c20e15b1147084360b9ec1
generated: "2023-10-25T19:30:16.729964915+03:00"
4 changes: 2 additions & 2 deletions library/ix-dev/community/briefkasten/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Briefkasten is a self hosted bookmarking app
annotations:
title: Briefkasten
type: application
version: 1.1.1
version: 1.1.2
apiVersion: v2
appVersion: latest
kubeVersion: '>=1.16.0-0'
Expand All @@ -14,7 +14,7 @@ maintainers:
dependencies:
- name: common
repository: file://../../../common
version: 1.1.1
version: 1.2.0
home: https://github.com/ndom91/briefkasten
icon: https://media.sys.truenas.net/apps/briefkasten/icons/icon.svg
sources:
Expand Down
Binary file not shown.
Binary file not shown.
37 changes: 5 additions & 32 deletions library/ix-dev/community/briefkasten/templates/_persistence.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,10 @@ persistence:
briefkasten:
briefkasten:
mountPath: {{ $storage.mountPath }}
{{- end }}
{{- end -}}

{{/* Database */}}
postgresdata:
enabled: true
type: {{ .Values.briefkastenStorage.pgData.type }}
datasetName: {{ .Values.briefkastenStorage.pgData.datasetName | default "" }}
hostPath: {{ .Values.briefkastenStorage.pgData.hostPath | default "" }}
targetSelector:
# Postgres pod
postgres:
# Postgres container
postgres:
mountPath: /var/lib/postgresql/data
# Postgres - Permissions container
# Different than the 01-permissions
permissions:
mountPath: /mnt/directories/postgres_data
postgresbackup:
enabled: true
type: {{ .Values.briefkastenStorage.pgBackup.type }}
datasetName: {{ .Values.briefkastenStorage.pgBackup.datasetName | default "" }}
hostPath: {{ .Values.briefkastenStorage.pgBackup.hostPath | default "" }}
targetSelector:
# Postgres backup pod
postgresbackup:
# Postgres backup container
postgresbackup:
mountPath: /postgres_backup
# Postgres - Permissions container
# Different than the 01-permissions
permissions:
mountPath: /mnt/directories/postgres_backup
{{- include "ix.v1.common.app.postgresPersistence"
(dict "pgData" .Values.briefkastenStorage.pgData
"pgBackup" .Values.briefkastenStorage.pgBackup
) | nindent 2 }}
{{- end -}}
13 changes: 2 additions & 11 deletions library/ix-dev/community/briefkasten/templates/_service.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ service:
port: {{ .Values.briefkastenNetwork.webPort }}
nodePort: {{ .Values.briefkastenNetwork.webPort }}
targetSelector: briefkasten
postgres:
enabled: true
type: ClusterIP
targetSelector: postgres
ports:
postgres:
enabled: true
primary: true
port: 5432
targetPort: 5432
targetSelector: postgres
{{- include "ix.v1.common.app.postgresService" $ | nindent 2 }}

{{- end -}}
Loading