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

BC-7024-migration-to-ionos-postgres-dev #834

Merged
merged 38 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b38cd7c
First draft for branch specific databases
YannickEvers Apr 4, 2024
beca83c
Set the real postgres hosts
YannickEvers Apr 4, 2024
69a5391
Add missing changes to postgres role
YannickEvers Apr 4, 2024
75a3ba1
Add missing changes to WITH
YannickEvers Apr 4, 2024
fd9ca0b
Fix loop
YannickEvers Apr 4, 2024
82e1e1a
Fix secret template
YannickEvers Apr 4, 2024
be8fe94
Additional loop to check if secrets already generated
YannickEvers Apr 4, 2024
78f6cc3
Fix secret check, limit loop output
YannickEvers Apr 4, 2024
24ef005
limit loop output
YannickEvers Apr 4, 2024
b1b768b
Fix parentheses
YannickEvers Apr 4, 2024
aaf9b11
Move cleanup from clean role (not used anymore) to suspended job in p…
YannickEvers Apr 5, 2024
409acfb
Fix indentation
YannickEvers Apr 5, 2024
87c900c
Merge deletion into one job
YannickEvers Apr 5, 2024
30cf471
Trigger database deletion in clean worflow, use loop
YannickEvers Apr 5, 2024
a12ff1a
Changes to test clean workflow
YannickEvers Apr 5, 2024
1fb88c8
Cleanup and fresh rollout
YannickEvers Apr 5, 2024
0941e08
Revert changes for testing clean workflow
YannickEvers Apr 5, 2024
82d5fe9
Make infra-tools image configurable
YannickEvers Apr 5, 2024
4bddf5d
Force Job replacement
YannickEvers Apr 5, 2024
b9022e9
Make cleanup script independent from the postgres database list
YannickEvers Apr 11, 2024
b799875
Merge branch 'main' into BC-7024-migration-to-ionos-postgres-dev
YannickEvers Apr 11, 2024
b47c1b6
Add apply for job to avoid problems with autogenerated properties
YannickEvers Apr 11, 2024
26ad71f
Test idea without list/loop, but reusable role
YannickEvers Apr 11, 2024
d9989d6
Fix syntax include_role
YannickEvers Apr 11, 2024
3f76f1f
Fix length check
YannickEvers Apr 11, 2024
749b3dc
Escaping
YannickEvers Apr 11, 2024
3cba587
Escape in SQL
YannickEvers Apr 11, 2024
4bd6655
Change Postgres Clusters (switch from SSD to HDD)
YannickEvers Apr 11, 2024
52ca930
Merge branch 'BC-7024-migration-to-ionos-postgres-dev' into BC-7024-t…
YannickEvers Apr 12, 2024
d3b0ea1
Test moving postgres role into the roles that need it
YannickEvers Apr 22, 2024
adca529
Fix and in condition
YannickEvers Apr 22, 2024
1a02c6c
Remove dof_postgresql_management role (now part of the roles that nee…
YannickEvers Apr 22, 2024
2715271
Switch workflow back to main
YannickEvers Apr 23, 2024
b3fdc81
Merge branch 'main' into BC-7024-migration-to-ionos-postgres-dev
YannickEvers Apr 23, 2024
af9ed61
Move parts back to general role, add labels, smaller improvements
YannickEvers Apr 23, 2024
cca6fb5
Fix search-replace mistake for variable
YannickEvers Apr 23, 2024
b7213dd
Keep postgres deployment in kubernetes for loadtest for now
YannickEvers Apr 24, 2024
9df4d72
Merge branch 'main' into BC-7024-migration-to-ionos-postgres-dev
YannickEvers Apr 25, 2024
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 .github/workflows/clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
clean:
needs:
- branch_name
uses: hpi-schul-cloud/dof_app_deploy/.github/workflows/clean_workflow.yml@main
uses: hpi-schul-cloud/dof_app_deploy/.github/workflows/clean_workflow.yml@BC-7024-migration-to-ionos-postgres-dev
YannickEvers marked this conversation as resolved.
Show resolved Hide resolved
with:
branch: ${{ needs.branch_name.outputs.branch }}
secrets:
Expand Down
28 changes: 11 additions & 17 deletions .github/workflows/clean_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,19 @@ jobs:
echo "${{ secrets.DEV_KUBE_CONFIG_NBC }}" > files/config_nbc
echo "${{ secrets.DEV_KUBE_CONFIG_THR }}" > files/config_thr
echo "${{ secrets.DEV_KUBE_CONFIG_DBC }}" > files/config_dbc
- name: delete custom resources and namespaces
- name: delete custom resources, databases and namespaces
run: |
branch_identifier='${{ needs.create_branch_identifier.outputs.id_branch }}'
kubectl --kubeconfig=files/config_brb --namespace $branch_identifier delete --ignore-not-found=true --all=true ScaledObject
kubectl --kubeconfig=files/config_brb --namespace $branch_identifier delete --ignore-not-found=true --all=true TriggerAuthentication
kubectl --kubeconfig=files/config_brb --namespace $branch_identifier delete --ignore-not-found=true --all=true OnePasswordItem
kubectl --kubeconfig=files/config_brb delete --ignore-not-found=true ns $branch_identifier
kubectl --kubeconfig=files/config_nbc --namespace $branch_identifier delete --ignore-not-found=true --all=true ScaledObject
kubectl --kubeconfig=files/config_nbc --namespace $branch_identifier delete --ignore-not-found=true --all=true TriggerAuthentication
kubectl --kubeconfig=files/config_nbc --namespace $branch_identifier delete --ignore-not-found=true --all=true OnePasswordItem
kubectl --kubeconfig=files/config_nbc delete --ignore-not-found=true ns $branch_identifier
kubectl --kubeconfig=files/config_thr --namespace $branch_identifier delete --ignore-not-found=true --all=true ScaledObject
kubectl --kubeconfig=files/config_thr --namespace $branch_identifier delete --ignore-not-found=true --all=true TriggerAuthentication
kubectl --kubeconfig=files/config_thr --namespace $branch_identifier delete --ignore-not-found=true --all=true OnePasswordItem
kubectl --kubeconfig=files/config_thr delete --ignore-not-found=true ns $branch_identifier
kubectl --kubeconfig=files/config_dbc --namespace $branch_identifier delete --ignore-not-found=true --all=true ScaledObject
kubectl --kubeconfig=files/config_dbc --namespace $branch_identifier delete --ignore-not-found=true --all=true TriggerAuthentication
kubectl --kubeconfig=files/config_dbc --namespace $branch_identifier delete --ignore-not-found=true --all=true OnePasswordItem
kubectl --kubeconfig=files/config_dbc delete --ignore-not-found=true ns $branch_identifier
for CLUSTER in brb nbc thr dbc
do
echo "Cleanup for $CLUSTER"
kubectl --kubeconfig=files/config_$CLUSTER --namespace $branch_identifier patch job/pg-deletion-job -p '{"spec":{"suspend":false}}' || echo "::warning::Couldn't clean up the postgres databases ($CLUSTER)"
mamutmk5 marked this conversation as resolved.
Show resolved Hide resolved
kubectl --kubeconfig=files/config_$CLUSTER --namespace $branch_identifier delete --ignore-not-found=true --all=true ScaledObject
kubectl --kubeconfig=files/config_$CLUSTER --namespace $branch_identifier delete --ignore-not-found=true --all=true TriggerAuthentication
kubectl --kubeconfig=files/config_$CLUSTER --namespace $branch_identifier wait --for=delete pod/pg-deletion-job --timeout=180s || echo "::warning::Couldn't clean up the postgres databases ($CLUSTER)"
kubectl --kubeconfig=files/config_$CLUSTER --namespace $branch_identifier delete --ignore-not-found=true --all=true OnePasswordItem
kubectl --kubeconfig=files/config_$CLUSTER delete --ignore-not-found=true ns $branch_identifier
done
- name: remove kubeconfig
run: |
rm -rf files/config_.*
3 changes: 3 additions & 0 deletions ansible/group_vars/all/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_PREFIX: ""
POSTGRES_PORT: 5432
POSTGRES_JOB_IMAGE: "quay.io/schulcloudverbund/infra-tools:4.1"
4 changes: 3 additions & 1 deletion ansible/group_vars/all/with.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ WITH_STORAGE: false
WITH_ERWINIDM: true
WITH_LDAP: false
WITH_TSP: false
WITH_DATABASES: false
WITH_MONGO_DATABASES: false
WITH_POSTGRES_DATABASES: false
WITH_BRANCH_POSTGRES_DB_MANAGEMENT: false
WITH_SCHULCLOUD_INIT: false
WITH_CALENDAR_INIT: false
WITH_OIDCMOCK: false
Expand Down
1 change: 1 addition & 0 deletions ansible/group_vars/develop/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_PREFIX: "{{ (NAMESPACE | replace('-','_'))[:40] }}__"
3 changes: 2 additions & 1 deletion ansible/group_vars/develop/with.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
WITH_DATABASES: true
WITH_MONGO_DATABASES: true
WITH_BRANCH_POSTGRES_DB_MANAGEMENT: true
WITH_SCHULCLOUD_INIT: true
WITH_CALENDAR_INIT: true
WITH_ERWINIDM: true
Expand Down
2 changes: 1 addition & 1 deletion ansible/group_vars/infra/with.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
WITH_DATABASES: true
WITH_MONGO_DATABASES: true
WITH_SCHULCLOUD_INIT: true
mamutmk5 marked this conversation as resolved.
Show resolved Hide resolved
WITH_CALENDAR_INIT: true
WITH_STORAGE: true
2 changes: 1 addition & 1 deletion ansible/group_vars/loadtest/with.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
WITH_DATABASES: true
WITH_MONGO_DATABASES: true
WITH_SCHULCLOUD_INIT: true
YannickEvers marked this conversation as resolved.
Show resolved Hide resolved
WITH_CALENDAR_INIT: true
WITH_STORAGE: true
1 change: 1 addition & 0 deletions ansible/host_vars/brb_host/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_HOST: "pg-4ifot8r4h0ksummi.postgresql.de-txl.ionos.com"
1 change: 1 addition & 0 deletions ansible/host_vars/dbc_host/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_HOST: "pg-0em2c6d51cp7s177.postgresql.de-txl.ionos.com"
1 change: 1 addition & 0 deletions ansible/host_vars/nbc_host/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_HOST: "pg-d2n03p780atcj0fk.postgresql.de-txl.ionos.com"
1 change: 1 addition & 0 deletions ansible/host_vars/thr_host/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_HOST: "pg-15bkj89e4fo00bve.postgresql.de-txl.ionos.com"
20 changes: 10 additions & 10 deletions ansible/roles/dof_mongo/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: svc.yml.j2
when: WITH_DATABASES
when: WITH_MONGO_DATABASES

- name: remove Service
kubernetes.core.k8s:
Expand All @@ -13,14 +13,14 @@
api_version: v1
kind: Service
name: mongo-svc
when: not WITH_DATABASES
when: not WITH_MONGO_DATABASES

- name: Add or Update ServiceMonitor
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: svc-monitor.yml.j2
when: WITH_DATABASES
when: WITH_MONGO_DATABASES

- name: remove ServiceMonitor
kubernetes.core.k8s:
Expand All @@ -30,14 +30,14 @@
api_version: monitoring.coreos.com/v1
kind: ServiceMonitor
name: mongo-svc-monitor
when: not WITH_DATABASES
when: not WITH_MONGO_DATABASES

- name: Add or Update Secret by 1Password
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: onepassword.yml.j2
when: WITH_DATABASES and ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool
when: WITH_MONGO_DATABASES and ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool

- name: remove Secret by 1Password
kubernetes.core.k8s:
Expand All @@ -47,14 +47,14 @@
api_version: onepassword.com/v1
kind: OnePasswordItem
name: mongo-secret
when: not WITH_DATABASES and ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool
when: not WITH_MONGO_DATABASES and ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool

- name: Add or Update Persistent Volumes Claim
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: pvc.yml.j2
when: WITH_DATABASES
when: WITH_MONGO_DATABASES

- name: remove Persistent Volumes Claim
kubernetes.core.k8s:
Expand All @@ -64,15 +64,15 @@
api_version: v1
kind: PersistentVolumeClaim
name: mongo-pvc
when: not WITH_DATABASES
when: not WITH_MONGO_DATABASES

- name: Add or Update Deployment
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: deployment.yml.j2
apply: yes
when: WITH_DATABASES
when: WITH_MONGO_DATABASES

- name: remove Deployment
kubernetes.core.k8s:
Expand All @@ -82,4 +82,4 @@
api_version: apps/v1
kind: Deployment
name: mongo-deployment
when: not WITH_DATABASES
when: not WITH_MONGO_DATABASES
24 changes: 12 additions & 12 deletions ansible/roles/dof_postgresql/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: svc.yml.j2
when: WITH_DATABASES
when: WITH_POSTGRES_DATABASES

- name: remove Service
kubernetes.core.k8s:
Expand All @@ -13,14 +13,14 @@
kind: Service
name: postgres-svc
state: absent
when: not WITH_DATABASES
when: not WITH_POSTGRES_DATABASES

- name: Add or Update Persistent Volumes Claim
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: pvc.yml.j2
when: WITH_DATABASES
when: WITH_POSTGRES_DATABASES

- name: remove Persistent Volumes Claim
kubernetes.core.k8s:
Expand All @@ -30,15 +30,15 @@
kind: PersistentVolumeClaim
name: postgres-pvc
state: absent
when: not WITH_DATABASES
when: not WITH_POSTGRES_DATABASES

- name: Add or Update Configmap
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: configmap.yml.j2
apply: yes
when: WITH_DATABASES and (ONEPASSWORD_OPERATOR is undefined or ( ONEPASSWORD_OPERATOR is defined and not ONEPASSWORD_OPERATOR) )
when: WITH_POSTGRES_DATABASES and (ONEPASSWORD_OPERATOR is undefined or ( ONEPASSWORD_OPERATOR is defined and not ONEPASSWORD_OPERATOR) )

- name: remove Configmap
kubernetes.core.k8s:
Expand All @@ -48,15 +48,15 @@
kind: ConfigMap
name: postgres-configmap
state: absent
when: not WITH_DATABASES and (ONEPASSWORD_OPERATOR is undefined or ( ONEPASSWORD_OPERATOR is defined and not ONEPASSWORD_OPERATOR) )
when: not WITH_POSTGRES_DATABASES and (ONEPASSWORD_OPERATOR is undefined or ( ONEPASSWORD_OPERATOR is defined and not ONEPASSWORD_OPERATOR) )

- name: Add or Update init scripts Configmap
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: configmap-init.yml.j2
apply: yes
when: WITH_DATABASES
when: WITH_POSTGRES_DATABASES

- name: remove init scripts Configmap
kubernetes.core.k8s:
Expand All @@ -66,14 +66,14 @@
kind: ConfigMap
name: postgres-configmap-init
state: absent
when: not WITH_DATABASES
when: not WITH_POSTGRES_DATABASES

- name: Add or Update Secret by 1Password
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: onepassword.yml.j2
when: WITH_DATABASES and (ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool)
when: WITH_POSTGRES_DATABASES and (ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool)

- name: remove Secret by 1Password
kubernetes.core.k8s:
Expand All @@ -83,15 +83,15 @@
kind: OnePasswordItem
name: postgres-secret
state: absent
when: not WITH_DATABASES and (ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool)
when: not WITH_POSTGRES_DATABASES and (ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool)

- name: Add or Update Deployment
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: deployment.yml.j2
apply: yes
when: WITH_DATABASES
when: WITH_POSTGRES_DATABASES

- name: remove Deployment
kubernetes.core.k8s:
Expand All @@ -101,4 +101,4 @@
kind: Deployment
name: postgres-deployment
state: absent
when: not WITH_DATABASES
when: not WITH_POSTGRES_DATABASES
9 changes: 9 additions & 0 deletions ansible/roles/dof_postgresql_management/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
galaxy_info:
role_name: dof_postgresql_management
author: Schul-Cloud Verbund
description: Helper role for creating and deleting branch specific postgres databases
company: Schul-Cloud Verbund
license: license (AGPLv3)
min_ansible_version: 2.8
galaxy_tags: []
dependencies: []
48 changes: 48 additions & 0 deletions ansible/roles/dof_postgresql_management/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
- name: Add or Update Postgres Cluster Secret by 1Password
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: onepassword-pg-cluster.yml.j2
when: ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool

- name: Check if secret with database credentials already exists
kubernetes.core.k8s_info:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
kind: Secret
name: "pg-{{ database_name }}-secret"
register: db_secret_present

- name: Create Secret for the database (if not existing)
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: secret-database.yml.j2
when: db_secret_present.resources|length == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does that really work if the operator is not fast enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean the onepassword-operator, right? The secret checked here is not managed by it. There are two separate secrets: One coming from 1Password with the credentials for the whole Postgres cluster and one with the branch/database specific credentials that is created directly with a random password.


- name: Create ConfigMap with Script
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: configmap-database-init.yml.j2
apply: yes

- name: Create/execute database configuration script
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: job-database-init.yml.j2

- name: Create ConfigMap with Script for database deletion
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: configmap-database-deletion.yml.j2
apply: yes

- name: Create suspended Job for database deletion
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: job-database-deletion.yml.j2
apply: yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: pg-configmap-deletion
namespace: {{ NAMESPACE }}
labels:
app: postgres
data:
config_script.sh: |
#!/bin/bash
DB_PREFIX="{{ POSTGRES_PREFIX }}"
if [[ {{ '${#DB_PREFIX}' }} -le 5 ]]; then
echo "Postgres prefix \"{{ POSTGRES_PREFIX }}\" seems too short. Dropping all matching databases could be dangerous. Aborting."
exit 1
fi
echo "Delete databases starting with {{ POSTGRES_PREFIX }}"
echo "SELECT 'DROP DATABASE ' || quote_ident(datname) || ' WITH (FORCE);' FROM pg_database WHERE datname LIKE '{{ POSTGRES_PREFIX | replace('_','#_')}}%' ESCAPE '#' \gexec" | psql -d postgres -w
echo "Delete users starting with {{ POSTGRES_PREFIX }}"
echo "SELECT 'DROP USER ' || quote_ident(usename) || ';' FROM pg_catalog.pg_user WHERE usename LIKE '{{ POSTGRES_PREFIX | replace('_','#_')}}%' ESCAPE '#' \gexec" | psql -d postgres -w
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: pg-{{ database_name }}-configmap-init
namespace: {{ NAMESPACE }}
labels:
app: postgres
data:
config_script.sh: |
#!/bin/bash
echo "Create owner of the DB"
echo "SELECT 'CREATE USER $DB_USER' WHERE NOT EXISTS (SELECT FROM pg_user WHERE usename = '$DB_USER')\gexec" | psql -d postgres -w
echo "GRANT $DB_USER TO $PGUSER;" | psql -d postgres -w
echo "Set/update password for user $DB_USER"
echo "ALTER USER $DB_USER WITH ENCRYPTED PASSWORD '$DB_USER_PASSWORD';" | psql -d postgres -w
echo "Create database"
echo "SELECT 'CREATE DATABASE $DB_NAME OWNER $DB_USER' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec" | psql -d postgres -w
echo "Revoke permissions for public role"
echo "REVOKE ALL ON DATABASE $DB_NAME FROM PUBLIC;" | psql -d postgres -w
Loading
Loading