Skip to content

Commit b2cee21

Browse files
authored
Merge pull request #32 from commitdev/fix/db-name
bug: fix where DB_NAME is not always project name
2 parents c22fbc4 + 69d9c1b commit b2cee21

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

db-ops/create-db-user.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
# docker image with postgres client only
44
DOCKER_IMAGE_TAG=governmentpaas/psql:latest
55

6-
DB_ENDPOINT=$(aws rds describe-db-instances --region=$REGION --query "DBInstances[?DBName=='$PROJECT_NAME'].Endpoint.Address" | jq -r '.[0]')
6+
DB_ENDPOINT=database.$PROJECT_NAME
7+
DB_NAME=$(aws rds describe-db-instances --region=$REGION --query "DBInstances[?DBInstanceIdentifier=='$PROJECT_NAME-$ENVIRONMENT'].DBName" | jq -r '.[0]')
78
SECRET_ID=$(aws secretsmanager list-secrets --region $REGION --query "SecretList[?Name=='$PROJECT_NAME-$ENVIRONMENT-rds-$SEED'].Name" | jq -r ".[0]")
89
# RDS MASTER
910
MASTER_RDS_USERNAME=master_user
1011
SECRET_PASSWORD=$(aws secretsmanager get-secret-value --region=$REGION --secret-id=$SECRET_ID | jq -r ".SecretString")
1112
# APPLICATION DB ADMIN
12-
DB_APP_USERNAME=$PROJECT_NAME
13+
DB_APP_USERNAME=$DB_NAME
1314
DB_APP_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | base64 | head -c 16)
1415

1516
# Fill in env-vars to db user creation manifest
@@ -19,13 +20,13 @@ eval "echo \"$(cat ./db-ops/job-create-db.yml.tpl)\"" > ./k8s-job-create-db.yml
1920
# 2. Secret in db-ops: db-create-users (with master password, and a .sql file
2021
# 3. Job in db-ops: db-create-users (runs the .sql file against the RDS given master_password from env)
2122
# 4. Secret in Application namespace with DB_USERNAME / DB_PASSWORD
22-
kubectl create -f ./k8s-job-create-db.yml
23+
kubectl apply -f ./k8s-job-create-db.yml
2324

2425
# Deleting the entire db-ops namespace, leaving ONLY application-namespace's secret behind
2526
kubectl -n db-ops wait --for=condition=complete --timeout=10s job db-create-users
2627
if [ $? -eq 0 ]
2728
then
28-
kubectl delete namespace db-ops
29+
kubectl get namespace db-ops
2930
else
3031
echo "Failed to create application database user, please see 'kubectl logs -n db-ops -l job-name=db-create-users'"
3132
fi

db-ops/job-create-db.yml.tpl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type: Opaque
1212
stringData:
1313
create-user.sql: |
1414
create user $DB_APP_USERNAME with encrypted password '$DB_APP_PASSWORD';
15-
grant all privileges on database $PROJECT_NAME to $DB_APP_USERNAME;
15+
grant all privileges on database $DB_NAME to $DB_APP_USERNAME;
1616
RDS_MASTER_PASSWORD: $SECRET_PASSWORD
1717
---
1818
apiVersion: v1
@@ -45,7 +45,7 @@ spec:
4545
- sh
4646
args:
4747
- '-c'
48-
- psql -U$MASTER_RDS_USERNAME -h $DB_ENDPOINT $PROJECT_NAME -a -f/db-ops/create-user.sql > /dev/null
48+
- psql -U$MASTER_RDS_USERNAME -h $DB_ENDPOINT $DB_NAME -a -f/db-ops/create-user.sql > /dev/null
4949
env:
5050
- name: PGPASSWORD
5151
valueFrom:
@@ -62,3 +62,36 @@ spec:
6262
secretName: db-create-users
6363
restartPolicy: Never
6464
backoffLimit: 1
65+
---
66+
apiVersion: apps/v1
67+
kind: Deployment
68+
metadata:
69+
name: db-pod
70+
namespace: $PROJECT_NAME
71+
spec:
72+
# this is purposely left at 0 so it can be enabled for troubleshooting purposes
73+
replicas: 0
74+
selector:
75+
matchLabels:
76+
app: db-pod
77+
template:
78+
metadata:
79+
labels:
80+
app: db-pod
81+
spec:
82+
automountServiceAccountToken: false
83+
containers:
84+
- command:
85+
- tail
86+
- -f
87+
- /dev/null
88+
image: governmentpaas/psql:latest
89+
imagePullPolicy: Always
90+
name: db-pod
91+
env:
92+
- name: PGPASSWORD
93+
valueFrom:
94+
secretKeyRef:
95+
name: $PROJECT_NAME
96+
key: DATABASE_PASSWORD
97+

0 commit comments

Comments
 (0)