1
+ #! /bin/bash
2
+
3
+ # This script will deploy the Google Compute Engine Persistent Disk CSI Driver
4
+ # to the currently available Kubernetes cluster
5
+
6
+ # Note: setup-cluster.yaml depends on the existence of cluster-roles
7
+ # system:csi-external-attacher and system:csi-external-provisioner
8
+ # which are in Kubernetes version 1.10.5+
9
+
10
+ # Args:
11
+ # GCE_PD_SA_DIR: Directory the service account key has been saved in (generated by setup-project.sh)
12
+ # GCE_PD_DRIVER_VERSION: The kustomize overlay (located in
13
+ # deploy/kubernetes/overlays) to deploy. Can be one of {stable, dev}
14
+
15
+ set -o nounset
16
+ set -o errexit
17
+ set -x
18
+
19
+ readonly NAMESPACE=" ${GCE_PD_DRIVER_NAMESPACE:- gce-pd-csi-driver} "
20
+ readonly DEPLOY_VERSION=" ${GCE_PD_DRIVER_VERSION:- stable} "
21
+ readonly DEPLOY_OS=" ${GCE_PD_DRIVER_OS:- linux} "
22
+ readonly PKGDIR=" ${GOPATH} /src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver"
23
+ source " ${PKGDIR} /deploy/common.sh"
24
+
25
+ print_usage ()
26
+ {
27
+ echo " deploy-driver.sh [--skip-sa-check]\n"
28
+ echo " \t--skip-sa-check: don't check the service account for required roles"
29
+ echo
30
+ }
31
+
32
+ skip_sa_check=
33
+ while [ ! -z " ${1-} " ]; do
34
+ case $1 in
35
+ --skip-sa-check ) shift
36
+ skip_sa_check=true
37
+ ;;
38
+ -h | --help ) print_usage
39
+ exit 1
40
+ ;;
41
+ * ) print_usage
42
+ exit 1
43
+ ;;
44
+ esac
45
+ done
46
+
47
+ ensure_var GCE_PD_SA_DIR
48
+
49
+ function check_service_account()
50
+ {
51
+ # Using bash magic to parse JSON for IAM
52
+ # Grepping for a line with client email returning anything quoted after the colon
53
+ readonly IAM_NAME=$( grep -Po ' "client_email": *\K"[^"]*"' ${GCE_PD_SA_DIR} /cloud-sa.json | tr -d ' "' )
54
+ readonly PROJECT=$( grep -Po ' "project_id": *\K"[^"]*"' ${GCE_PD_SA_DIR} /cloud-sa.json | tr -d ' "' )
55
+ readonly GOTTEN_BIND_ROLES=$( gcloud projects get-iam-policy ${PROJECT} --flatten=" bindings[].members" --format=' table(bindings.role)' --filter=" bindings.members:${IAM_NAME} " )
56
+ readonly BIND_ROLES=$( get_needed_roles)
57
+ MISSING_ROLES=false
58
+ for role in ${BIND_ROLES}
59
+ do
60
+ if ! grep -q $role <<< ${GOTTEN_BIND_ROLES} ;
61
+ then
62
+ echo " Missing role: $role "
63
+ MISSING_ROLES=true
64
+ fi
65
+ done
66
+ if [ " ${MISSING_ROLES} " = true ];
67
+ then
68
+ echo " Cannot deploy with missing roles in service account, please run setup-project.sh to setup Service Account"
69
+ exit 1
70
+ fi
71
+ }
72
+
73
+ ensure_kustomize
74
+
75
+ if [ " $skip_sa_check " != true ]; then
76
+ check_service_account
77
+ fi
78
+
79
+ if ! ${KUBECTL} get namespace ${NAMESPACE} -v=" ${VERBOSITY} " ;
80
+ then
81
+ ${KUBECTL} create namespace ${NAMESPACE} -v=" ${VERBOSITY} "
82
+ fi
83
+
84
+ if ! ${KUBECTL} get secret cloud-sa -v=" ${VERBOSITY} " -n ${NAMESPACE} ;
85
+ then
86
+ ${KUBECTL} create secret generic cloud-sa -v=" ${VERBOSITY} " --from-file=" ${GCE_PD_SA_DIR} /cloud-sa.json" -n ${NAMESPACE}
87
+ fi
88
+
89
+ # GKE Required Setup
90
+ if ! ${KUBECTL} get clusterrolebinding -v=" ${VERBOSITY} " cluster-admin-binding;
91
+ then
92
+ ${KUBECTL} create clusterrolebinding cluster-admin-binding -v=" ${VERBOSITY} " --clusterrole cluster-admin --user $( gcloud config get-value account)
93
+ fi
94
+
95
+ # Debug log: print ${KUBECTL} version
96
+ ${KUBECTL} version
97
+
98
+ readonly tmp_spec=/tmp/gcp-compute-persistent-disk-csi-driver-specs-generated.yaml
99
+
100
+ if [[ ${DEPLOY_OS} = " mixed" ]]; then
101
+ FIRST_OS=linux
102
+ SECOND_OS=windows
103
+ else
104
+ FIRST_OS=${DEPLOY_OS}
105
+ fi
106
+
107
+ os_dir=$( mktemp -d -p ./ -t os-XXXXXXXXXX)
108
+ cat << EOF >${os_dir} /kustomization.yaml
109
+ apiVersion: kustomize.config.k8s.io/v1beta1
110
+ kind: Kustomization
111
+ namespace:
112
+ gce-pd-csi-driver
113
+ bases:
114
+ - ${PKGDIR} /deploy/kubernetes/kustomization/base_setup
115
+ - ${PKGDIR} /deploy/kubernetes/kustomization/node_setup/${FIRST_OS}
116
+ EOF
117
+
118
+ if [[ -n ${SECOND_OS} ]]; then
119
+ echo " - ../node_setup/${SECOND_OS} " >> ${os_dir} /kustomization.yaml
120
+ fi
121
+
122
+ image_dir=$( mktemp -d -p ./ -t image-XXXXXXXXXX)
123
+ cp ${PKGDIR} /deploy/kubernetes/kustomization/image_setup/${DEPLOY_VERSION} /kustomization.yaml ${image_dir}
124
+ cat << EOF >>${image_dir} /kustomization.yaml
125
+ bases:
126
+ - ../${os_dir}
127
+ EOF
128
+
129
+ ${KUSTOMIZE_PATH} build ${image_dir} | tee $tmp_spec ; \
130
+ ${KUBECTL} apply -v=" ${VERBOSITY} " -f $tmp_spec ; \
131
+ rm -rf ${os_dir} ${image_dir}
0 commit comments