From 4219ecdc7091b6cc6c09dd7f68d7d6d515e5656d Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Tue, 21 Oct 2025 12:19:41 +0200 Subject: [PATCH] feat(odoo): enhance staging environment setup and improve documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Refactor staging database initialization with improved readiness checks and neutralization - Copy filestore data during staging database clone process - Update environment variables: replace `ODOO_DATABASE` with `DB_FILTER` in deployments - Adjust PVC naming for clarity (`data-odoo` → `data-odoo-main`) - Modify ingress configuration to properly enable staging ingress when enabled - Update README installation steps for better clarity and ordering - Add `forward-odoo-staging` task and update port-forward messages - Grant CREATEDB privilege to CNPG owner user via postInitSQL - Remove unused `PROXY_MODE` and `LIST_DB` from ConfigMap --- README.md | 20 ++++------- odoo/templates/cm.yaml | 5 ++- odoo/templates/cnpg.yaml | 2 ++ odoo/templates/deploy-staging.yaml | 55 ++++++++++++++---------------- odoo/templates/ingress.yaml | 4 +-- odoo/templates/pvc.yaml | 2 +- task | 10 ++++-- 7 files changed, 46 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 3b70cdf..0ec0b30 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,12 @@ Install the CloudNativePG chart: task install-chart cnpg values/knd.local.yaml ``` +Install ingress-nginx in the current cluster. + +```bash +task install-chart ingress-nginx values/knd.local.yaml +``` + Install the Odoo chart: ```bash @@ -106,20 +112,6 @@ task install-chart odoo values/odoo.knd.local.yaml The Odoo database will be initialized automatically. -Once the pod is ready, run this command to port forward the service: - -```bash -task forward odoo -``` - -### Setup ingress nginx - -Install ingress-nginx in the current cluster. - -```bash -task install-chart ingress-nginx values/knd.local.yaml -``` - Forward the ingress-nginx port. ```bash diff --git a/odoo/templates/cm.yaml b/odoo/templates/cm.yaml index 3ec36ab..d0c2adb 100644 --- a/odoo/templates/cm.yaml +++ b/odoo/templates/cm.yaml @@ -5,10 +5,9 @@ metadata: labels: app: odoo data: - ODOO_DATABASE: {{ .Values.database }} ADDONS_GIT_REPOS: {{ join "," .Values.addonsGitRepos | quote }} + ODOO_DATABASE: {{ .Values.database }} ODOO_INIT_LANG: {{ .Values.initLang | quote }} ODOO_INIT_LOGIN: {{ .Values.initLogin | quote }} ODOO_INIT_PASSWORD: {{ .Values.initPassword | quote }} - PROXY_MODE: {{ .Values.proxyMode | quote }} - LIST_DB: {{ .Values.listDB | quote }} + DB_FILTER: {{ .Values.database }} diff --git a/odoo/templates/cnpg.yaml b/odoo/templates/cnpg.yaml index eaa640c..8ab458f 100644 --- a/odoo/templates/cnpg.yaml +++ b/odoo/templates/cnpg.yaml @@ -11,4 +11,6 @@ spec: initdb: database: {{ .Values.cnpg.database }} owner: {{ .Values.cnpg.owner }} + postInitSQL: + - ALTER USER {{ .Values.cnpg.owner }} CREATEDB; {{- end }} diff --git a/odoo/templates/deploy-staging.yaml b/odoo/templates/deploy-staging.yaml index 3859a88..fede6ec 100644 --- a/odoo/templates/deploy-staging.yaml +++ b/odoo/templates/deploy-staging.yaml @@ -21,49 +21,44 @@ spec: checksum/config: {{ include (print $.Template.BasePath "/cm.yaml") . | sha256sum }} spec: initContainers: - - name: wait-for-main-odoo - image: {{ .Values.image }} - imagePullPolicy: {{ .Values.imagePullPolicy }} - command: - - sh - - -c - - | - # Wait for main Odoo deployment pods to be running and ready - echo "Waiting for main Odoo deployment to be ready..." - until curl -sf http://{{ .Release.Name }}-odoo:8069/web/health > /dev/null 2>&1; do - echo "Waiting for main Odoo service to respond..." - sleep 5 - done - echo "Main Odoo deployment is ready!" - - name: post-init + - name: init image: {{ .Values.image }} imagePullPolicy: {{ .Values.imagePullPolicy }} command: - /bin/bash - -c - | - echo "Creating staging database from template..." + echo "Waiting for main Odoo deployment to be ready..." + until curl -f http://odoo:8069/web/health > /dev/null 2>&1; do + echo "Waiting for main Odoo service to respond..." + sleep 5 + done + echo "Main Odoo deployment is ready!" - # First, try to terminate any active connections to the source database - PGPASSWORD=$(cat /etc/secrets/postgresql-app/password) psql -h {{ .Release.Name }}-postgresql-rw.{{ .Release.Namespace }}.svc.cluster.local -U {{ .Values.cnpg.owner }} -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{{ .Values.cnpg.database }}' AND pid <> pg_backend_pid();" || true - - # Check if staging database already exists and drop it - DB_EXISTS=$(PGPASSWORD=$(cat /etc/secrets/postgresql-app/password) psql -h {{ .Release.Name }}-postgresql-rw.{{ .Release.Namespace }}.svc.cluster.local -U {{ .Values.cnpg.owner }} -tAc "SELECT 1 FROM pg_database WHERE datname='{{ .Values.database }}-{{ .Values.staging.name }}';" 2>/dev/null) + echo "Clone and neutralize Odoo staging database from main database..." + echo "Check if staging database already exists and drop it..." + DB_EXISTS=$(PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='{{ .Values.database }}-{{ .Values.staging.name }}';" 2>/dev/null) if [ "$DB_EXISTS" = "1" ]; then echo "Terminating connections to existing staging database..." - PGPASSWORD=$(cat /etc/secrets/postgresql-app/password) psql -h {{ .Release.Name }}-postgresql-rw.{{ .Release.Namespace }}.svc.cluster.local -U {{ .Values.cnpg.owner }} -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{{ .Values.database }}-{{ .Values.staging.name }}' AND pid <> pg_backend_pid();" || true + PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{{ .Values.database }}-{{ .Values.staging.name }}' AND pid <> pg_backend_pid();" || true echo "Dropping existing staging database..." - PGPASSWORD=$(cat /etc/secrets/postgresql-app/password) psql -h {{ .Release.Name }}-postgresql-rw.{{ .Release.Namespace }}.svc.cluster.local -U {{ .Values.cnpg.owner }} -c "DROP DATABASE {{ .Values.database }}-{{ .Values.staging.name }};" + PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -d postgres -c "DROP DATABASE {{ .Values.database }}-{{ .Values.staging.name }};" fi - # Create staging database from template (much faster than dump/restore) - echo "Creating database from template..." - PGPASSWORD=$(cat /etc/secrets/postgresql-app/password) psql -h {{ .Release.Name }}-postgresql-rw.{{ .Release.Namespace }}.svc.cluster.local -U {{ .Values.cnpg.owner }} -c "CREATE DATABASE \"{{ .Values.database }}-{{ .Values.staging.name }}\" WITH TEMPLATE \"{{ .Values.cnpg.database }}\";" + echo "Terminate any active connections to the source database..." + PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{{ .Values.cnpg.database }}' AND pid <> pg_backend_pid();" || true + + echo "Copying staging database with template..." + PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -d postgres -c "CREATE DATABASE \"{{ .Values.database }}-{{ .Values.staging.name }}\" WITH TEMPLATE \"{{ .Values.cnpg.database }}\";" - # Neutralize the staging database using Odoo's neutralize command echo "Neutralizing staging database with Odoo neutralize command..." - odoo -d {{ .Values.database }}-{{ .Values.staging.name }} --neutralize --stop-after-init - echo "Database creation from template and neutralization completed for {{ .Values.staging.name }}." + odoo neutralize -d {{ .Values.database }}-{{ .Values.staging.name }} --stop-after-init + + echo "Copy filestore from /main-data/ to /staging-data ..." + mkdir -p /staging-data/filestore + cp -r /main-data/filestore/{{ .Values.database }} /staging-data/filestore/{{ .Values.database }}-{{ .Values.staging.name }} + + echo "Clone and and neutralization staging database completed for {{ .Values.staging.name }}." envFrom: - configMapRef: name: {{ .Release.Name }}-configmap @@ -154,7 +149,7 @@ spec: - configMapRef: name: {{ .Release.Name }}-configmap env: - - name: ODOO_DATABASE + - name: DB_FILTER value: {{ .Values.database }}-{{ .Values.staging.name }} {{- if .Values.cnpg.enabled }} - name: PGHOST diff --git a/odoo/templates/ingress.yaml b/odoo/templates/ingress.yaml index c159551..19a7692 100644 --- a/odoo/templates/ingress.yaml +++ b/odoo/templates/ingress.yaml @@ -74,8 +74,7 @@ spec: {{- end }} secretName: odoo-tls {{- end }} -{{- end }} -{{- if and .Values.staging.enabled .Values.ingress.enabled }} +{{- if and .Values.staging.enabled }} --- apiVersion: networking.k8s.io/v1 kind: Ingress @@ -115,4 +114,5 @@ spec: - {{ .Values.staging.name }}.{{ .Values.ingress.host }} secretName: odoo-{{ .Values.staging.name }}-tls {{- end }} +{{- end }} {{- end }} \ No newline at end of file diff --git a/odoo/templates/pvc.yaml b/odoo/templates/pvc.yaml index 0e64f6d..c3e558f 100644 --- a/odoo/templates/pvc.yaml +++ b/odoo/templates/pvc.yaml @@ -1,7 +1,7 @@ apiVersion: v1 kind: PersistentVolumeClaim metadata: - name: data-odoo + name: data-odoo-main spec: accessModes: - ReadWriteOnce diff --git a/task b/task index e4eeb01..267c681 100755 --- a/task +++ b/task @@ -307,14 +307,20 @@ function forward-postgres() { } function forward-odoo() { - PORT=8069 + PORT=8080 echo "Open http://localhost:$PORT url in your browser." kubectl port-forward service/odoo $PORT:8069 } +function forward-odoo-staging() { + PORT=8080 + echo "Open http://localhost:$PORT url in your browser." + kubectl port-forward service/odoo-staging $PORT:8069 +} + function forward-ingress-nginx() { PORT=8080 - echo "Open http://odoo.knd.local:$PORT or http://odoo.local:$PORT url in your browser." + echo "Open http://staging.odoo.knd.local:$PORT or http://odoo.knd.local:$PORT or http://odoo.local:$PORT url in your browser." kubectl port-forward -n ingress-nginx svc/ingress-nginx-controller $PORT:80 }