1
1
#! /bin/bash
2
2
3
+ # This script will setup the given project with a Service Account that has the correct
4
+ # restricted permissions to run the gcp_compute_persistent_disk_csi_driver and download
5
+ # the keys to a specified directory
6
+
7
+ # WARNING: This script will delete and recreate the service accounts, bindings, and keys
8
+ # associated with ${GCE_PD_SA_NAME}. Great care must be taken to not run the script
9
+ # with a service account that is currently in use.
10
+
11
+ # Args:
12
+ # PROJECT: GCP project
13
+ # GCE_PD_SA_NAME: Name of the service account to create
14
+ # GCE_PD_SA_DIR: Directory to save the service account key
15
+
16
+
3
17
set -o nounset
4
18
set -o errexit
5
19
6
20
readonly PKGDIR=" ${GOPATH} /src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver"
7
21
readonly KUBEDEPLOY=" ${PKGDIR} /deploy/kubernetes"
8
-
9
- BIND_ROLES=" roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT} /roles/gcp_compute_persistent_disk_csi_driver_custom_role"
10
- IAM_NAME=" ${GCEPD_SA_NAME} @${PROJECT} .iam.gserviceaccount.com"
22
+ readonly BIND_ROLES=" roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT} /roles/gcp_compute_persistent_disk_csi_driver_custom_role"
23
+ readonly IAM_NAME=" ${GCE_PD_SA_NAME} @${PROJECT} .iam.gserviceaccount.com"
11
24
12
25
# Create or Update Custom Role
13
26
if gcloud iam roles describe gcp_compute_persistent_disk_csi_driver_custom_role --project " ${PROJECT} " ;
22
35
fi
23
36
24
37
# Delete Service Account Key
25
- if [ -f $SA_FILE ]; then
26
- rm " $SA_FILE "
38
+ if [ -f " ${GCE_PD_SA_DIR} /cloud-sa.json " ]; then
39
+ rm " ${GCE_PD_SA_DIR} /cloud-sa.json "
27
40
fi
28
41
# Delete ALL EXISTING Bindings
29
42
gcloud projects get-iam-policy " ${PROJECT} " --format json > " ${PKGDIR} /deploy/iam.json"
@@ -34,9 +47,9 @@ rm -f "${PKGDIR}/deploy/iam.json"
34
47
gcloud iam service-accounts delete " ${IAM_NAME} " --project " ${PROJECT} " --quiet || true
35
48
36
49
# Create new Service Account and Keys
37
- gcloud iam service-accounts create " ${GCEPD_SA_NAME } " --project " ${PROJECT} "
50
+ gcloud iam service-accounts create " ${GCE_PD_SA_NAME } " --project " ${PROJECT} "
38
51
for role in ${BIND_ROLES}
39
52
do
40
53
gcloud projects add-iam-policy-binding " ${PROJECT} " --member serviceAccount:" ${IAM_NAME} " --role ${role}
41
54
done
42
- gcloud iam service-accounts keys create " ${SA_FILE} " --iam-account " ${IAM_NAME} " --project " ${PROJECT} "
55
+ gcloud iam service-accounts keys create " ${GCE_PD_SA_DIR} /cloud-sa.json " --iam-account " ${IAM_NAME} " --project " ${PROJECT} "
0 commit comments