|
17 | 17 | set -o nounset
|
18 | 18 | set -o errexit
|
19 | 19 |
|
| 20 | +. $(dirname $0)/common.sh |
| 21 | + |
20 | 22 | readonly PKGDIR="${GOPATH}/src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver"
|
21 | 23 | readonly KUBEDEPLOY="${PKGDIR}/deploy/kubernetes"
|
22 |
| -readonly BIND_ROLES="roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT}/roles/gcp_compute_persistent_disk_csi_driver_custom_role" |
| 24 | +readonly BIND_ROLES=$(get_needed_roles) |
23 | 25 | readonly IAM_NAME="${GCE_PD_SA_NAME}@${PROJECT}.iam.gserviceaccount.com"
|
24 | 26 |
|
| 27 | +# Check if SA exists |
| 28 | +CREATE_SA=true |
| 29 | +SA_JSON=$(gcloud iam service-accounts list --filter="name:${IAM_NAME}" --format="json") |
| 30 | +if [ "[]" != "${SA_JSON}" ]; |
| 31 | +then |
| 32 | + CREATE_SA=false |
| 33 | + echo "Service account ${IAM_NAME} exists. Would you like to create a new one (y) or reuse the existing one (n)" |
| 34 | + read -p "(y/n)" -n 1 -r REPLY |
| 35 | + echo |
| 36 | + if [[ ${REPLY} =~ ^[Yy]$ ]]; |
| 37 | + then |
| 38 | + CREATE_SA=true |
| 39 | + fi |
| 40 | +fi |
| 41 | + |
| 42 | +if [ "${CREATE_SA}" = true ]; |
| 43 | +then |
| 44 | + # Delete Service Account Key |
| 45 | + if [ -f "${GCE_PD_SA_DIR}/cloud-sa.json" ]; |
| 46 | + then |
| 47 | + rm "${GCE_PD_SA_DIR}/cloud-sa.json" |
| 48 | + fi |
| 49 | + # Delete ALL EXISTING Bindings |
| 50 | + gcloud projects get-iam-policy "${PROJECT}" --format json > "${PKGDIR}/deploy/iam.json" |
| 51 | + sed -i "/serviceAccount:${IAM_NAME}/d" "${PKGDIR}/deploy/iam.json" |
| 52 | + gcloud projects set-iam-policy "${PROJECT}" "${PKGDIR}/deploy/iam.json" |
| 53 | + rm -f "${PKGDIR}/deploy/iam.json" |
| 54 | + # Delete Service Account |
| 55 | + gcloud iam service-accounts delete "${IAM_NAME}" --project "${PROJECT}" --quiet || true |
| 56 | + |
| 57 | + # Create new Service Account |
| 58 | + gcloud iam service-accounts create "${GCE_PD_SA_NAME}" --project "${PROJECT}" |
| 59 | +fi |
| 60 | + |
25 | 61 | # Create or Update Custom Role
|
26 | 62 | if gcloud iam roles describe gcp_compute_persistent_disk_csi_driver_custom_role --project "${PROJECT}";
|
27 | 63 | then
|
28 | 64 | gcloud iam roles update gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
|
29 |
| - --project "${PROJECT}" \ |
30 |
| - --file "${PKGDIR}/deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml" |
| 65 | + --project "${PROJECT}" \ |
| 66 | + --file "${PKGDIR}/deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml" |
31 | 67 | else
|
32 | 68 | gcloud iam roles create gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
|
33 |
| - --project "${PROJECT}" \ |
34 |
| - --file "${PKGDIR}/deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml" |
| 69 | + --project "${PROJECT}" \ |
| 70 | + --file "${PKGDIR}/deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml" |
35 | 71 | fi
|
36 | 72 |
|
37 |
| -# Delete Service Account Key |
38 |
| -if [ -f "${GCE_PD_SA_DIR}/cloud-sa.json" ]; then |
39 |
| - rm "${GCE_PD_SA_DIR}/cloud-sa.json" |
40 |
| -fi |
41 |
| -# Delete ALL EXISTING Bindings |
42 |
| -gcloud projects get-iam-policy "${PROJECT}" --format json > "${PKGDIR}/deploy/iam.json" |
43 |
| -sed -i "/serviceAccount:${IAM_NAME}/d" "${PKGDIR}/deploy/iam.json" |
44 |
| -gcloud projects set-iam-policy "${PROJECT}" "${PKGDIR}/deploy/iam.json" |
45 |
| -rm -f "${PKGDIR}/deploy/iam.json" |
46 |
| -# Delete Service Account |
47 |
| -gcloud iam service-accounts delete "${IAM_NAME}" --project "${PROJECT}" --quiet || true |
48 |
| - |
49 |
| -# Create new Service Account and Keys |
50 |
| -gcloud iam service-accounts create "${GCE_PD_SA_NAME}" --project "${PROJECT}" |
| 73 | +# Bind service account to roles |
51 | 74 | for role in ${BIND_ROLES}
|
52 | 75 | do
|
53 | 76 | gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"${IAM_NAME}" --role ${role}
|
54 | 77 | done
|
55 |
| -gcloud iam service-accounts keys create "${GCE_PD_SA_DIR}/cloud-sa.json" --iam-account "${IAM_NAME}" --project "${PROJECT}" |
| 78 | + |
| 79 | +# Export key if needed |
| 80 | +if [ "${CREATE_SA}" = true ]; |
| 81 | +then |
| 82 | + gcloud iam service-accounts keys create "${GCE_PD_SA_DIR}/cloud-sa.json" --iam-account "${IAM_NAME}" --project "${PROJECT}" |
| 83 | +fi |
0 commit comments