Skip to content
Merged
37 changes: 27 additions & 10 deletions .github/workflows/.deployer-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

directory:
description: Crunchy Chart directory
default: 'charts/crunchy'
default: 'actions/crunchy/charts/crunchy'
required: false
type: string
oc_server:
Expand Down Expand Up @@ -102,7 +102,13 @@ jobs:
echo "S3 endpoint not found"
exit 1
fi

- name: Release Name
id: release_name
shell: bash
run: |
RELEASE_NAME=pg-$(echo -n "${{github.event.repository.name}}" | md5sum | cut -c 1-8)
echo "RELEASE_NAME=${RELEASE_NAME}" >> $GITHUB_OUTPUT
echo "Release name: ${RELEASE_NAME}"
- name: Deploy Database
uses: bcgov/action-oc-runner@v1.2.0
with:
Expand All @@ -115,21 +121,30 @@ jobs:
commands: |
echo 'Deploying crunchy helm chart'
cd ${{ inputs.directory }}
sed -i 's/^name:.*/name: ${{ github.event.repository.name }}/' Chart.yaml
helm package -u .
if [ ${{ inputs.s3_enabled }} == true ]; then
helm upgrade --install --wait --set crunchy.pgBackRest.s3.enabled=true \
--set-string crunchy.pgBackRest.s3.accessKey=${{ secrets.s3_access_key }} \
--set-string crunchy.pgBackRest.s3.secretKey=${{ secrets.s3_secret_key }} \
--set-string crunchy.pgBackRest.s3.bucket=${{ secrets.s3_bucket }} \
--set-string crunchy.pgBackRest.s3.endpoint=${{ secrets.s3_endpoint }} \
--values ${{ inputs.values }} ${{github.event.repository.name}} .
--set-string crunchy.postgresVersion="16" \
--set-string crunchy.postgisVersion="3.4" \
--values ${{ inputs.values }} ${{steps.release_name.outputs.RELEASE_NAME}} \
./${{ github.event.repository.name }}-5.5.1.tgz
else
helm upgrade --install --wait --values ${{ inputs.values }} ${{github.event.repository.name}} .
helm upgrade --install --wait --values ${{ inputs.values }} \
--set-string crunchy.postgresVersion="16" \
--set-string crunchy.postgisVersion="3.4" \
${{steps.release_name.outputs.RELEASE_NAME}} \
./${{ github.event.repository.name }}-5.5.1.tgz
fi
# check if operator deployed the db successfully, retry 10 times with 60 seconds interval
READY=false
for i in {1..10}; do
# Check if the 'db' instance has at least 1 ready replica
if oc get PostgresCluster/${{github.event.repository.name}}-crunchy -o json | jq -e '.status.instances[] | select(.name=="db") | .readyReplicas > 0' > /dev/null 2>&1; then
if oc get PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy -o json | jq -e '.status.instances[] | select(.name=="db") | .readyReplicas > 0' > /dev/null 2>&1; then
echo "Crunchy DB instance 'db' is ready "
READY=true
break
Expand All @@ -145,7 +160,7 @@ jobs:
fi

- name: Add PR specific user to Crunchy DB # only for PRs
if: github.event_name == 'pull_request'
if: (github.event_name == 'pull_request' && github.event.action != 'closed')
uses: bcgov/action-oc-runner@v1.2.0
with:
oc_namespace: ${{ secrets.oc_namespace }}
Expand All @@ -155,7 +170,7 @@ jobs:
commands: |
echo 'Adding PR specific user to Crunchy DB'
NEW_USER='{"databases":["app-${{ github.event.number }}"],"name":"app-${{ github.event.number }}"}'
CURRENT_USERS=$(oc get PostgresCluster/${{github.event.repository.name}}-crunchy -o json | jq '.spec.users')
CURRENT_USERS=$(oc get PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy -o json | jq '.spec.users')
echo "${CURRENT_USERS}"

# check if current_users already contains the new_user
Expand All @@ -166,12 +181,12 @@ jobs:

UPDATED_USERS=$(echo "${CURRENT_USERS}" | jq --argjson NEW_USER "${NEW_USER}" '. + [$NEW_USER]')
PATCH_JSON=$(jq -n --argjson users "${UPDATED_USERS}" '{"spec": {"users": $users}}')
oc patch PostgresCluster/${{github.event.repository.name}}-crunchy --type=merge -p "${PATCH_JSON}"
oc patch PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy --type=merge -p "${PATCH_JSON}"

# wait for sometime as it takes time to create the user, query the secret and check if it is created, otherwise wait in a loop for 5 rounds
SECRET_FOUND=false
for i in {1..5}; do
if oc get secret ${{github.event.repository.name}}-crunchy-pguser-app-${{ github.event.number }} -o jsonpath='{.metadata.name}' > /dev/null 2>&1; then
if oc get secret ${{steps.release_name.outputs.RELEASE_NAME}}-crunchy-pguser-app-${{ github.event.number }} -o jsonpath='{.metadata.name}' > /dev/null 2>&1; then
echo "Secret created"
SECRET_FOUND=true
break
Expand All @@ -182,7 +197,9 @@ jobs:
done

if [ "$SECRET_FOUND" = false ]; then
echo "Error: Secret ${{github.event.repository.name}}-crunchy-pguser-app-${{ github.event.number }} was not created after 5 attempts."
echo "Error: Secret ${{steps.release_name.outputs.RELEASE_NAME}}-crunchy-pguser-app-${{ github.event.number }} was not created after 5 attempts."
exit 1
fi



24 changes: 24 additions & 0 deletions .github/workflows/.pr-close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ jobs:
# If found, then remove
helm status ${{ env.release }} && helm uninstall --no-hooks ${{ env.release }} || \
echo "Not found: ${{ env.release }}"
# check if crunchy exists or else exit
CRUNCHY_RELEASE_NAME=pg-$(echo -n "${{github.event.repository.name}}" | md5sum | cut -c 1-8)
oc get PostgresCluster/${CRUNCHY_RELEASE_NAME}-crunchy || exit 0

# Remove the user from the crunchy cluster yaml and apply the changes
USER_TO_REMOVE='{"databases":["app-${{ github.event.number }}"],"name":"app-${{ github.event.number }}"}'

echo 'getting current users from crunchy'
CURRENT_USERS=$(oc get PostgresCluster/${CRUNCHY_RELEASE_NAME}-crunchy -o json | jq '.spec.users')
echo "${CURRENT_USERS}"

# Remove the user from the list,
UPDATED_USERS=$(echo "${CURRENT_USERS}" | jq --argjson user "${USER_TO_REMOVE}" 'map(select(. != $user))')

PATCH_JSON=$(jq -n --argjson users "${UPDATED_USERS}" '{"spec": {"users": $users}}')
oc patch PostgresCluster/${CRUNCHY_RELEASE_NAME}-crunchy --type=merge -p "${PATCH_JSON}"

# get primary crunchy pod and remove the role and db
CRUNCHY_PG_PRIMARY_POD_NAME=$(oc get pods -l postgres-operator.crunchydata.com/cluster=${CRUNCHY_RELEASE_NAME}-crunchy,postgres-operator.crunchydata.com/role=master -o json | jq -r '.items[0].metadata.name')

echo "${CRUNCHY_PG_PRIMARY_POD_NAME}"
# Terminate all connections to the database before trying terminate and Drop the databse and role right after
oc exec -it "${CRUNCHY_PG_PRIMARY_POD_NAME}" -- bash -c "psql -U postgres -d postgres -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'app-${{ github.event.number }}' AND pid <> pg_backend_pid();\" && \psql -U postgres -d postgres -c \"DROP DATABASE \\\"app-${{ github.event.number }}\\\";\" && \psql -U postgres -d postgres -c \"DROP ROLE \\\"app-${{ github.event.number }}\\\";\""
echo 'database and role deleted'

- name: OC Template (label) Cleanup
if: inputs.cleanup == 'label'
Expand Down
40 changes: 31 additions & 9 deletions actions/crunchy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: 'Deploy a Crunchy PostgreSQL database to OpenShift'
inputs:
directory:
description: 'Crunchy Chart directory'
default: 'charts/crunchy'
default: 'actions/crunchy/charts/crunchy'
required: false
oc_server:
description: 'OpenShift server'
Expand Down Expand Up @@ -50,7 +50,13 @@ inputs:
s3_endpoint:
description: 'S3 endpoint'
required: false

postgres_version:
description: 'Postgres version to use; e.g. 16, 15, Must check with platform team for supported versions'
required: true
postgis_version:
description: 'PostGIS version to use; e.g. 3.4, 3.3, 3.2'
required: false
default: ''
runs:
using: "composite"
steps:
Expand All @@ -77,6 +83,13 @@ runs:
fi

- uses: actions/checkout@v4
- name: Release Name
id: release_name
shell: bash
run: |
RELEASE_NAME=pg-$(echo -n "${{github.event.repository.name}}" | md5sum | cut -c 1-8)
echo "RELEASE_NAME=${RELEASE_NAME}" >> $GITHUB_OUTPUT
echo "Release name: ${RELEASE_NAME}"
- name: Deploy Database
uses: bcgov/action-oc-runner@v1.2.0
with:
Expand All @@ -89,21 +102,30 @@ runs:
commands: |
echo 'Deploying crunchy helm chart'
cd ${{ inputs.directory }}
sed -i 's/^name:.*/name: ${{ github.event.repository.name }}/' Chart.yaml
helm package -u .
if [ "${{ inputs.s3_enabled }}" == "true" ]; then
helm upgrade --install --wait --set crunchy.pgBackRest.s3.enabled=true \
--set-string crunchy.pgBackRest.s3.accessKey=${{ inputs.s3_access_key }} \
--set-string crunchy.pgBackRest.s3.secretKey=${{ inputs.s3_secret_key }} \
--set-string crunchy.pgBackRest.s3.bucket=${{ inputs.s3_bucket }} \
--set-string crunchy.pgBackRest.s3.endpoint=${{ inputs.s3_endpoint }} \
--values ${{ inputs.values }} ${{ github.event.repository.name }} .
--set-string crunchy.postgresVersion=${{ inputs.postgres_version }} \
--set-string crunchy.postgisVersion=${{ inputs.postgis_version }} \
--values ${{ inputs.values }} ${{steps.release_name.outputs.RELEASE_NAME}} \
./${{ github.event.repository.name }}-5.5.1.tgz
else
helm upgrade --install --wait --values ${{ inputs.values }} ${{ github.event.repository.name }} .
helm upgrade --install --wait --values ${{ inputs.values }} \
--set-string crunchy.postgresVersion=${{ inputs.postgres_version }} \
--set-string crunchy.postgisVersion=${{ inputs.postgis_version }} \
${{steps.release_name.outputs.RELEASE_NAME}} \
./${{ github.event.repository.name }}-5.5.1.tgz
fi
# check if operator deployed the db successfully, retry 10 times with 60 seconds interval
READY=false
for i in {1..10}; do
# Check if the 'db' instance has at least 1 ready replica
if oc get PostgresCluster/${{github.event.repository.name}}-crunchy -o json | jq -e '.status.instances[] | select(.name=="db") | .readyReplicas > 0' > /dev/null 2>&1; then
if oc get PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy -o json | jq -e '.status.instances[] | select(.name=="db") | .readyReplicas > 0' > /dev/null 2>&1; then
echo "Crunchy DB instance 'db' is ready "
READY=true
break
Expand All @@ -129,7 +151,7 @@ runs:
commands: |
echo 'Adding PR specific user to Crunchy DB'
NEW_USER='{"databases":["app-${{ github.event.number }}"],"name":"app-${{ github.event.number }}"}'
CURRENT_USERS=$(oc get PostgresCluster/${{ github.event.repository.name }}-crunchy -o json | jq '.spec.users')
CURRENT_USERS=$(oc get PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy -o json | jq '.spec.users')
echo "${CURRENT_USERS}"

# check if current_users already contains the new_user
Expand All @@ -140,11 +162,11 @@ runs:

UPDATED_USERS=$(echo "${CURRENT_USERS}" | jq --argjson NEW_USER "${NEW_USER}" '. + [$NEW_USER]')
PATCH_JSON=$(jq -n --argjson users "${UPDATED_USERS}" '{"spec": {"users": $users}}')
oc patch PostgresCluster/${{ github.event.repository.name }}-crunchy --type=merge -p "${PATCH_JSON}"
oc patch PostgresCluster/${{steps.release_name.outputs.RELEASE_NAME}}-crunchy --type=merge -p "${PATCH_JSON}"
# wait for sometime as it takes time to create the user, query the secret and check if it is created, otherwise wait in a loop for 5 rounds
SECRET_FOUND=false
for i in {1..5}; do
if oc get secret ${{github.event.repository.name}}-crunchy-pguser-app-${{ github.event.number }} -o jsonpath='{.metadata.name}' > /dev/null 2>&1; then
if oc get secret ${{steps.release_name.outputs.RELEASE_NAME}}-crunchy-pguser-app-${{ github.event.number }} -o jsonpath='{.metadata.name}' > /dev/null 2>&1; then
echo "Secret created"
SECRET_FOUND=true
break
Expand All @@ -155,6 +177,6 @@ runs:
done

if [ "$SECRET_FOUND" = false ]; then
echo "Error: Secret ${{github.event.repository.name}}-crunchy-pguser-app-${{ github.event.number }} was not created after 5 attempts."
echo "Error: Secret ${{steps.release_name.outputs.RELEASE_NAME}}-crunchy-pguser-app-${{ github.event.number }} was not created after 5 attempts."
exit 1
fi
4 changes: 2 additions & 2 deletions actions/crunchy/charts/crunchy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ global:
dbName: app #test
crunchy: # enable it for TEST and PROD, for PR based pipelines simply use single postgres
enabled: true
postgresVersion: 16
postGISVersion: '3.4'
postgresVersion: ~
postGISVersion: ~
imagePullPolicy: IfNotPresent
# enable below to start a new crunchy cluster after disaster from a backed-up location, crunchy will choose the best place to recover from.
# follow https://access.crunchydata.com/documentation/postgres-operator/5.2.0/tutorial/disaster-recovery/
Expand Down