diff --git a/charts/fylr/Chart.yaml b/charts/fylr/Chart.yaml index 13d1406..9949e62 100644 --- a/charts/fylr/Chart.yaml +++ b/charts/fylr/Chart.yaml @@ -8,7 +8,7 @@ type: application # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.154 +version: 0.1.155 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/charts/fylr/templates/autodbDeletion-job.yaml b/charts/fylr/templates/autodbDeletion-job.yaml new file mode 100644 index 0000000..f2c102a --- /dev/null +++ b/charts/fylr/templates/autodbDeletion-job.yaml @@ -0,0 +1,25 @@ +{{- if .Values.autoManageDB.enabled -}} +apiVersion: batch/v1 +kind: Job +metadata: + name: autodb-delete-{{ .Values.autoManageDB.name }} + labels: + {{- include "fylr.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": "post-delete" + "helm.sh/hook-weight": "0" + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded +spec: + template: + spec: + restartPolicy: Never + containers: + - name: psql-delete + image: postgres:17.5 + env: + - name: PGPASSWORD + value: "{{ .Values.autoManageDB.pgPass }}" + command: ["/bin/sh", "-c"] + args: + - psql -h {{ .Values.autoManageDB.pghost }} -U {{ .Values.autoManageDB.pgUser }} -d postgres -c "DROP DATABASE IF EXISTS \"{{ .Values.autoManageDB.name }}\";" -c "DROP ROLE IF EXISTS \"{{ .Values.autoManageDB.user }}\";" +{{- end }} \ No newline at end of file diff --git a/charts/fylr/templates/autodbcreation-configmap.yaml b/charts/fylr/templates/autodbcreation-configmap.yaml new file mode 100644 index 0000000..0295b69 --- /dev/null +++ b/charts/fylr/templates/autodbcreation-configmap.yaml @@ -0,0 +1,52 @@ +{{- if .Values.autoManageDB.enabled -}} +apiVersion: v1 +kind: ConfigMap +metadata: + name: autodb-create-{{ .Values.autoManageDB.name }} + labels: + {{- include "fylr.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": pre-install + "helm.sh/hook-weight": "0" + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded +data: + init-role-db.sh: | + #!/bin/bash + + # Wait for PostgreSQL to be ready + until psql -h "$PGHOST" -U "$PGUSER" -c '\l'; do + echo "Waiting for PostgreSQL to be ready..." + sleep 2 + done + + # Check if role exists + echo "Checking if role $DB_USER exists..." + ROLE_EXISTS=$(psql -h "$PGHOST" -U "$PGUSER" -t -c "SELECT 1 FROM pg_roles WHERE rolname = '$DB_USER'") + + # Check if database exists + echo "Checking if database $DB_NAME exists..." + DB_EXISTS=$(psql -h "$PGHOST" -U "$PGUSER" -t -c "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'") + + # Check both conditions together + if [ -z "$ROLE_EXISTS" ] || [ -z "$DB_EXISTS" ]; then + # Create role if it doesn't exist + if [ -z "$ROLE_EXISTS" ]; then + echo "Creating role $DB_USER..." + psql -h "$PGHOST" -U "$PGUSER" -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASSWORD';" + else + echo "Role $DB_USER already exists" + fi + + # Create database if it doesn't exist + if [ -z "$DB_EXISTS" ]; then + echo "Creating database $DB_NAME..." + psql -h "$PGHOST" -U "$PGUSER" -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" + else + echo "Database $DB_NAME already exists" + fi + else + echo "Both role $DB_USER and database $DB_NAME already exist, nothing to do." + fi + + echo "Role and database creation complete. Job finished." +{{- end }} diff --git a/charts/fylr/templates/autodbcreation-job.yaml b/charts/fylr/templates/autodbcreation-job.yaml new file mode 100644 index 0000000..11a7390 --- /dev/null +++ b/charts/fylr/templates/autodbcreation-job.yaml @@ -0,0 +1,48 @@ +{{- if .Values.autoManageDB.enabled -}} +apiVersion: batch/v1 +kind: Job +metadata: + name: autodb-create-{{ .Values.autoManageDB.name }} + labels: + {{- include "fylr.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": pre-install + "helm.sh/hook-weight": "0" + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded +spec: + template: + spec: + restartPolicy: Never + containers: + - name: psql-init + image: postgres:17.5 # should match the postgres cluster image to avoid compatibility issues + env: + - name: PGPASSWORD + value: {{ .Values.autoManageDB.pgPass }} + + - name: PGHOST + value: {{ .Values.autoManageDB.pghost }} + + - name: PGUSER + value: {{ .Values.autoManageDB.pgUser }} + + - name: DB_NAME + value: {{ .Values.autoManageDB.name }} + + - name: DB_USER + value: {{ .Values.autoManageDB.user }} + + - name: DB_PASSWORD + value: {{ .Values.autoManageDB.password }} + + command: ["/bin/sh", "-c"] + args: + - "sh /scripts/init-role-db.sh" + volumeMounts: + - name: scripts + mountPath: /scripts + volumes: + - name: scripts + configMap: + name: autodb-create-{{ .Values.autoManageDB.name }} +{{- end }} \ No newline at end of file diff --git a/charts/fylr/values.yaml b/charts/fylr/values.yaml index 64795ad..2385c49 100644 --- a/charts/fylr/values.yaml +++ b/charts/fylr/values.yaml @@ -321,6 +321,7 @@ fylr: driver: "postgres" # -- (object) postgresql connection settings # NOTE: this is ignored if postgresql-ha.enabled is set to true. + # If you enable autoManageDB, please fill the db info here. postgres: # -- (string) host is the host of the postgres server. host: "localhost" @@ -600,6 +601,22 @@ minio: conditions: - StringEquals: '"aws:username": "fylr"' +# If enabled, the chart will create a database on the provided database server URL $pghost using a Helm pre-install hook with the given details. +# It will also delete the database when the chart is uninstalled using a Helm post-delete hook. +autoManageDB: + # Set to true to enable auto creation/deletion + enabled: false + # Database Postgres URl + pghost: "url" + pgUser: "super user" + pgPass: "super pass" + # database name to create in the $pghost database server. + name: "fylr_db" + # The user owning the database to create in the $pghost database server. + user: "fylr_user" + # The password to assign to the user $user created in the $pghost database server. + password: "securepassword" + # @ignored defines the settings for the postgresql application # for more information see: https://github.com/bitnami/charts/tree/master/bitnami/postgresql-ha postgresql-ha: