Skip to content

Commit

Permalink
cleanup: make the scripts error free
Browse files Browse the repository at this point in the history
  • Loading branch information
Shabirmean committed Oct 26, 2021
1 parent 604148f commit 36f3508
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions extras/cloudsql-multicluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ gcloud container clusters get-credentials ${CLUSTER_2_NAME} --zone ${CLUSTER_2_Z
kubectx cluster2="gke_${PROJECT_ID}_${CLUSTER_2_ZONE}_${CLUSTER_2_NAME}"
```

6. **Set up Workload Identity** for both clusters. When the script is run for the second time, you'll see some errors (GCP service account already exists), this is ok.
6. **Set up Workload Identity** for both clusters.

```
kubectx cluster1
Expand All @@ -83,7 +83,7 @@ kubectx cluster2
../cloudsql/setup_workload_identity.sh
```

7. **Run the Cloud SQL instance create script** on both clusters. You'll see errors when running on the second cluster, this is ok.
7. **Run the Cloud SQL instance create script** on both clusters.

```
kubectx cluster1
Expand Down
23 changes: 16 additions & 7 deletions extras/cloudsql/create_cloudsql_instance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ echo "☁️ Enabling the Cloud SQL API..."
gcloud config set project ${PROJECT_ID}
gcloud services enable sqladmin.googleapis.com

echo "☁️ Creating Cloud SQL instance: ${INSTANCE_NAME} ..."
gcloud sql instances create $INSTANCE_NAME \
CSQL_EXISTS=$(gcloud sql instances list --filter="${INSTANCE_NAME}")
if [ $CSQL_EXISTS = "Listed 0 items." ]; then
echo "☁️ Creating Cloud SQL instance: ${INSTANCE_NAME} ..."
gcloud sql instances create $INSTANCE_NAME \
--database-version=POSTGRES_12 --tier=db-custom-1-3840 \
--region=${DB_REGION} --project ${PROJECT_ID}
fi

echo "☁️ All done creating ${INSTANCE_NAME} ..."
INSTANCE_CONNECTION_NAME=$(gcloud sql instances describe $INSTANCE_NAME --format='value(connectionName)')
Expand All @@ -32,11 +35,17 @@ gcloud sql users create admin \
--instance=$INSTANCE_NAME --password=admin

# Create Accounts DB
echo "☁️ Creating accounts-db in ${INSTANCE_NAME}..."
gcloud sql databases create accounts-db --instance=$INSTANCE_NAME
ACCOUNTS_DB_EXISTS=$(gcloud sql databases list --instance=${INSTANCE_NAME} --filter="accounts-db")
if [ $ACCOUNTS_DB_EXISTS = "Listed 0 items." ]; then
echo "☁️ Creating accounts-db in ${INSTANCE_NAME}..."
gcloud sql databases create accounts-db --instance=$INSTANCE_NAME
fi

# Create Ledger DB
echo "☁️ Creating ledger-db in ${INSTANCE_NAME}..."
gcloud sql databases create ledger-db --instance=$INSTANCE_NAME
LEDGER_DB_EXISTS=$(gcloud sql databases list --instance=${INSTANCE_NAME} --filter="ledger-db")
if [ $LEDGER_DB_EXISTS = "Listed 0 items." ]; then
echo "☁️ Creating ledger-db in ${INSTANCE_NAME}..."
gcloud sql databases create ledger-db --instance=$INSTANCE_NAME
fi

echo "⭐️ Done."
echo "⭐️ Done."
7 changes: 5 additions & 2 deletions extras/cloudsql/setup_workload_identity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ kubectl create namespace $NAMESPACE

echo "✅ Creating GCP and K8s service accounts..."
kubectl create serviceaccount --namespace $NAMESPACE $KSA_NAME
gcloud iam service-accounts create $GSA_NAME

SA_EXISTS=$(gcloud iam service-accounts list --filter="${GSA_NAME}")
if [ $SA_EXISTS = "Listed 0 items." ]; then
gcloud iam service-accounts create $GSA_NAME
fi

echo "✅ Annotating service accounts to connect your GSA and KSA..."
gcloud iam service-accounts add-iam-policy-binding \
Expand All @@ -53,4 +56,4 @@ gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member "serviceAccount:${GSA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" \
--role roles/cloudsql.client

echo "⭐️ Done."
echo "⭐️ Done."

0 comments on commit 36f3508

Please sign in to comment.