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 #160

Merged
merged 9 commits into from
Apr 25, 2024
31 changes: 31 additions & 0 deletions ansible/roles/schulcloud-calendar-core/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
- name: Check if secret with database credentials already exists
kubernetes.core.k8s_info:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
kind: Secret
name: "pg-calendar-secret"
register: db_secret_present
when: WITH_BRANCH_POSTGRES_DB_MANAGEMENT

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

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

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

- name: Service
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: pg-calendar-configmap-init
namespace: {{ NAMESPACE }}
labels:
app: calendar-postgres-init
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ data:
{% if CAL_IS_MIGRATION is defined %}
IS_MIGRATION: "{{ CAL_IS_MIGRATION }}"
{% endif %}
{% if WITH_BRANCH_POSTGRES_DB_MANAGEMENT is defined and WITH_BRANCH_POSTGRES_DB_MANAGEMENT|bool %}
DB_HOST: "{{ POSTGRES_MANAGEMENT_HOST }}"
DB_SSL: "true"
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,21 @@ spec:
ports:
- containerPort: 3000
envFrom:
- secretRef:
name: calendar-secret
- configMapRef:
name: calendar-configmap
{% if WITH_BRANCH_POSTGRES_DB_MANAGEMENT is defined and WITH_BRANCH_POSTGRES_DB_MANAGEMENT|bool %}
- secretRef:
name: calendar-secret
name: pg-calendar-secret
YannickEvers marked this conversation as resolved.
Show resolved Hide resolved
env:
- name: DB_PASSWORD
value: "$(DB_USER_PASSWORD)"
- name: DB_USERNAME
value: "$(DB_USER)"
- name: DB_DATABASE
value: "$(DB_NAME)"
{% endif %}
livenessProbe:
failureThreshold: 3
httpGet:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: batch/v1
kind: Job
metadata:
name: pg-calendar-init-job-{{ 1000000 | random | hash('md5') }}
namespace: {{ NAMESPACE }}
labels:
app: calendar-postgres-init
app.kubernetes.io/part-of: schulcloud-verbund
app.kubernetes.io/name: calendar-postgres-init
app.kubernetes.io/component: calendar
app.kubernetes.io/managed-by: ansible
git.repo: {{ SCHULCLOUD_CALENDAR_REPO_NAME }}
spec:
template:
metadata:
labels:
app: calendar-postgres-init
app.kubernetes.io/part-of: schulcloud-verbund
app.kubernetes.io/name: calendar-postgres-init
app.kubernetes.io/component: calendar
app.kubernetes.io/managed-by: ansible
git.repo: {{ SCHULCLOUD_CALENDAR_REPO_NAME }}
spec:
volumes:
- name: config-script
configMap:
name: pg-calendar-configmap-init
# 711 in decimal is 457
defaultMode: 457
containers:
- name: psql-calendar-config
image: {{ POSTGRES_MANAGEMENT_JOB_IMAGE }}
command:
- /bin/bash
- -c
args:
- /scripts/config_script.sh
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: config-script
mountPath: /scripts/
envFrom:
- secretRef:
name: pg-calendar-secret
env:
- name: PGHOST
value: {{ POSTGRES_MANAGEMENT_HOST }}
- name: PGUSER
valueFrom:
secretKeyRef:
name: pg-cluster-secret
key: username
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: pg-cluster-secret
key: password
restartPolicy: Never
ttlSecondsAfterFinished: 1800
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Secret
metadata:
name: pg-calendar-secret
namespace: {{ NAMESPACE }}
labels:
app: calendar-postgres-init
type: Opaque
data:
DB_USER: "{{ (POSTGRES_MANAGEMENT_PREFIX + 'calendar') | b64encode }}"
DB_USER_PASSWORD: "{{ lookup('ansible.builtin.password', '/dev/null') | b64encode }}"
DB_NAME: "{{ (POSTGRES_MANAGEMENT_PREFIX + 'calendar') | b64encode }}"
12 changes: 11 additions & 1 deletion ansible/roles/schulcloud-calendar-init/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
name: calendar-db-init-file
when: not WITH_CALENDAR_INIT

- name: Test if init job exists
kubernetes.core.k8s_info:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
api_version: batch/v1
kind: Job
name: calendar-db-init-job
register: calendar_init_job_present
when: WITH_CALENDAR_INIT

- name: Calendar db init job
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: job_init_db.yml.j2
when: WITH_CALENDAR_INIT
when: WITH_CALENDAR_INIT and calendar_init_job_present.resources|length == 0
simoncolincap marked this conversation as resolved.
Show resolved Hide resolved

- name: Calendar db init job
kubernetes.core.k8s:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ spec:
- name: calendar-db-init
image: schulcloud/infra-tools:latest
envFrom:
- secretRef:
name: calendar-secret
- configMapRef:
name: calendar-configmap
{% if WITH_BRANCH_POSTGRES_DB_MANAGEMENT is defined and WITH_BRANCH_POSTGRES_DB_MANAGEMENT|bool %}
- secretRef:
name: calendar-secret
name: pg-calendar-secret
env:
- name: DB_PASSWORD
value: "$(DB_USER_PASSWORD)"
- name: DB_USERNAME
value: "$(DB_USER)"
- name: DB_DATABASE
value: "$(DB_NAME)"
{% endif %}
volumeMounts:
- name: script
mountPath: /update.sh
Expand Down