Skip to content

Commit 36f3508

Browse files
committed
cleanup: make the scripts error free
1 parent 604148f commit 36f3508

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

extras/cloudsql-multicluster/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ gcloud container clusters get-credentials ${CLUSTER_2_NAME} --zone ${CLUSTER_2_Z
7373
kubectx cluster2="gke_${PROJECT_ID}_${CLUSTER_2_ZONE}_${CLUSTER_2_NAME}"
7474
```
7575

76-
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.
76+
6. **Set up Workload Identity** for both clusters.
7777

7878
```
7979
kubectx cluster1
@@ -83,7 +83,7 @@ kubectx cluster2
8383
../cloudsql/setup_workload_identity.sh
8484
```
8585

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

8888
```
8989
kubectx cluster1

extras/cloudsql/create_cloudsql_instance.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ echo "☁️ Enabling the Cloud SQL API..."
1919
gcloud config set project ${PROJECT_ID}
2020
gcloud services enable sqladmin.googleapis.com
2121

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

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

3437
# Create Accounts DB
35-
echo "☁️ Creating accounts-db in ${INSTANCE_NAME}..."
36-
gcloud sql databases create accounts-db --instance=$INSTANCE_NAME
38+
ACCOUNTS_DB_EXISTS=$(gcloud sql databases list --instance=${INSTANCE_NAME} --filter="accounts-db")
39+
if [ $ACCOUNTS_DB_EXISTS = "Listed 0 items." ]; then
40+
echo "☁️ Creating accounts-db in ${INSTANCE_NAME}..."
41+
gcloud sql databases create accounts-db --instance=$INSTANCE_NAME
42+
fi
3743

3844
# Create Ledger DB
39-
echo "☁️ Creating ledger-db in ${INSTANCE_NAME}..."
40-
gcloud sql databases create ledger-db --instance=$INSTANCE_NAME
45+
LEDGER_DB_EXISTS=$(gcloud sql databases list --instance=${INSTANCE_NAME} --filter="ledger-db")
46+
if [ $LEDGER_DB_EXISTS = "Listed 0 items." ]; then
47+
echo "☁️ Creating ledger-db in ${INSTANCE_NAME}..."
48+
gcloud sql databases create ledger-db --instance=$INSTANCE_NAME
49+
fi
4150

42-
echo "⭐️ Done."
51+
echo "⭐️ Done."

extras/cloudsql/setup_workload_identity.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ kubectl create namespace $NAMESPACE
2525

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

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

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

56-
echo "⭐️ Done."
59+
echo "⭐️ Done."

0 commit comments

Comments
 (0)