@@ -9,26 +9,25 @@ usage () {
99# check parameters
1010# REGION - AWS region to use
1111# SEED - Random seed that is part of the name of the AWS secret containing the db master password
12- # PROJECT_NAME - Name of the project and the k8s namespace containing the database service
12+ # PROJECT_NAME - Name of the project
1313# ENVIRONMENT - stage or prod
14- # NAMESPACE - The target k8s namespace to create and create a secret in
14+ # NAMESPACE - The target k8s namespace to create a secret in
1515# DATABASE_TYPE - The type of database - mysql, postgres
1616# DATABASE_NAME - The name of the database(s) to create in the database server
1717# USER_NAME - The name of the user to create and grant access to the database specified above
1818# USER_PASSWORD - The password of the user to create and grant access to the database specified above (optional)
19- # SECRET_NAME - The secret of the database user to get password
19+ # SECRET_NAME - The suffix name of the secret created in AWS Secret Manager that will contain the created credentials
2020# CREATE_SECRET - A template file to render to create a secret (optional)
21- # CREATE_DB_POD - A template file to render to create a db pod for troubleshooting (optional)
2221([[ -z " ${REGION} " ]] || \
2322 [[ -z " ${SEED} " ]] || \
2423 [[ -z " ${PROJECT_NAME} " ]] || \
2524 [[ -z " ${ENVIRONMENT} " ]] || \
2625 [[ -z " ${NAMESPACE} " ]] || \
26+ [[ -z " ${SECRET_NAME} " ]] || \
2727 [[ -z " ${DATABASE_TYPE} " ]] || \
2828 [[ -z " ${DATABASE_NAME} " ]] || \
29- [[ -z " ${SECRET_NAME} " ]] || \
3029 [[ -z " ${USER_NAME} " ]] ) && \
31- echo " Some environment variables (REGION, SEED, PROJECT_NAME, ENVIRONMENT, NAMESPACE, DATABASE_TYPE, DATABASE_NAME, USER_NAME) are not set properly." && usage
30+ echo " Some environment variables (REGION, SEED, PROJECT_NAME, ENVIRONMENT, NAMESPACE, SECRET_NAME, DATABASE_TYPE, DATABASE_NAME, USER_NAME) are not set properly." && usage
3231
3332# docker image with postgres + mysql clients
3433DOCKER_IMAGE_TAG=commitdev/zero-k8s-utilities:0.0.3
@@ -46,6 +45,7 @@ MASTER_RDS_PASSWORD=$(aws secretsmanager get-secret-value --region=${REGION} --s
4645# # get application user/pass
4746DB_APP_USERNAME=$( echo " ${USER_NAME} " | tr -dc ' A-Za-z0-9' )
4847DB_APP_PASSWORD=${USER_PASSWORD:- $(LC_ALL=C tr -dc ' A-Za-z0-9' < / dev/ urandom | base64 | head -c 24)}
48+ JOB_ID=$( LC_ALL=C tr -dc ' a-z0-9' < /dev/urandom | head -c 8)
4949
5050# get correct dsn string for db type
5151if [[ " ${DB_TYPE} " == " postgres" ]]; then
@@ -54,28 +54,24 @@ elif [[ "${DB_TYPE}" == "mysql" ]]; then
5454 DB_ENDPOINT_FOR_DSN=" tcp(${DB_ENDPOINT} )"
5555fi
5656
57- # fill in env-vars to db user creation manifest
58- JOB_ID=$( LC_ALL=C tr -dc ' a-z0-9' < /dev/urandom | head -c 8)
59- eval " echo \" $( cat ./db-ops/job-create-db-${DATABASE_TYPE} .yml.tpl) \" " > ./k8s-job-create-db.yml
60- [[ -z " ${CREATE_SECRET} " ]] || eval " echo \" $( cat ./db-ops/${CREATE_SECRET} ) \" " >> ./k8s-job-create-db.yml
6157# the manifest creates these things
6258# 1. Namespaces: db-ops, $NAMESPACE
6359# 2. Secret in db-ops: db-create-users (with master password, and a .sql file
6460# 3. Job in db-ops: db-create-users (runs the .sql file against the RDS given master_password from env)
65- # 4. Secret in $NAMESPACE namespace with DB_USERNAME / DB_PASSWORD
6661
67- # execution
68- kubectl apply -f ./k8s-job-create-db.yml
69- rm -f ./k8s-job-create-db.yml
62+ # Run the job in the kubernetes cluster that will create the database user
63+ eval " echo \" $( cat ./db-ops/job-create-db-${DATABASE_TYPE} .yml.tpl) \" " | kubectl apply -f -
64+
65+ # Create a secret in AWS Secrets Manager. The contents of this secret will be automatically pulled into a kubernetes secret by external-secrets
66+ [[ -z " ${CREATE_SECRET} " ]] || aws secretsmanager create-secret --name " ${PROJECT_NAME} /kubernetes/${ENVIRONMENT} /${SECRET_NAME} " --description " Application secrets" --tags " [{\" Key\" :\" application-secret\" ,\" Value\" :\" ${PROJECT} -${ENVIRONMENT} -${SECRET_NAME} \" }]" --secret-string " $( eval " echo \" $( cat ./db-ops/${CREATE_SECRET} ) \" " ) "
7067
71- # clean up
72- # # Deleting the entire db-ops namespace, leaving ONLY application-namespace's secret behind
68+ # # Delete the entire db-ops namespace
7369kubectl -n db-ops wait --for=condition=complete --timeout=10s job db-create-users-$NAMESPACE -${JOB_ID}
7470if [ $? -eq 0 ]
7571then
7672 kubectl delete namespace db-ops
7773else
7874 echo " Failed to create application database user, please see 'kubectl logs -n db-ops -l job-name=db-create-users-$NAMESPACE -${JOB_ID} '"
79- kubectl delete secret -n db-ops ${SECRET_NAME}
75+ kubectl delete secret -n db-ops db-create-users
8076fi
8177
0 commit comments