provision.j2.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/bin/bash
  2. set -euo pipefail
  3. metadata=""
  4. if [[ -n "{{ openshift_gcp_startup_script_file }}" ]]; then
  5. if [[ ! -f "{{ openshift_gcp_startup_script_file }}" ]]; then
  6. echo "Startup script file missing at {{ openshift_gcp_startup_script_file }} from=$(pwd)"
  7. exit 1
  8. fi
  9. metadata+="--metadata-from-file=startup-script={{ openshift_gcp_startup_script_file }}"
  10. fi
  11. if [[ -n "{{ openshift_gcp_user_data_file }}" ]]; then
  12. if [[ ! -f "{{ openshift_gcp_user_data_file }}" ]]; then
  13. echo "User data file missing at {{ openshift_gcp_user_data_file }}"
  14. exit 1
  15. fi
  16. if [[ -n "${metadata}" ]]; then
  17. metadata+=","
  18. else
  19. metadata="--metadata-from-file="
  20. fi
  21. metadata+="user-data={{ openshift_gcp_user_data_file }}"
  22. fi
  23. # Select image or image family
  24. image="{{ openshift_gcp_image }}"
  25. if ! gcloud --project "{{ openshift_gcp_project }}" compute images describe "${image}" &>/dev/null; then
  26. if ! gcloud --project "{{ openshift_gcp_project }}" compute images describe-from-family "${image}" &>/dev/null; then
  27. echo "No compute image or image-family found, create an image named '{{ openshift_gcp_image }}' to continue'"
  28. exit 1
  29. fi
  30. image="family/${image}"
  31. fi
  32. ### PROVISION THE INFRASTRUCTURE ###
  33. dns_zone="{{ dns_managed_zone | default(openshift_gcp_prefix + 'managed-zone') }}"
  34. # Check the DNS managed zone in Google Cloud DNS, create it if it doesn't exist and exit after printing NS servers
  35. if ! gcloud --project "{{ openshift_gcp_project }}" dns managed-zones describe "${dns_zone}" &>/dev/null; then
  36. echo "DNS zone '${dns_zone}' doesn't exist. Must be configured prior to running this script"
  37. exit 1
  38. fi
  39. # Create network
  40. if ! gcloud --project "{{ openshift_gcp_project }}" compute networks describe "{{ openshift_gcp_network_name }}" &>/dev/null; then
  41. gcloud --project "{{ openshift_gcp_project }}" compute networks create "{{ openshift_gcp_network_name }}" --mode "auto"
  42. else
  43. echo "Network '{{ openshift_gcp_network_name }}' already exists"
  44. fi
  45. # Firewall rules in a form:
  46. # ['name']='parameters for "gcloud compute firewall-rules create"'
  47. # For all possible parameters see: gcloud compute firewall-rules create --help
  48. range=""
  49. if [[ -n "{{ openshift_node_port_range }}" ]]; then
  50. range=",tcp:{{ openshift_node_port_range }},udp:{{ openshift_node_port_range }}"
  51. fi
  52. declare -A FW_RULES=(
  53. ['icmp']='--allow icmp'
  54. ['ssh-external']='--allow tcp:22'
  55. ['ssh-internal']='--allow tcp:22 --source-tags bastion'
  56. ['master-internal']="--allow tcp:2224,tcp:2379,tcp:2380,tcp:4001,udp:4789,udp:5404,udp:5405,tcp:8053,udp:8053,tcp:8444,tcp:10250,tcp:10255,udp:10255,tcp:24224,udp:24224 --source-tags ocp --target-tags ocp-master"
  57. ['master-external']="--allow tcp:80,tcp:443,tcp:1936,tcp:8080,tcp:8443${range} --target-tags ocp-master"
  58. ['node-internal']="--allow udp:4789,tcp:10250,tcp:10255,udp:10255 --source-tags ocp --target-tags ocp-node,ocp-infra-node"
  59. ['infra-node-internal']="--allow tcp:5000 --source-tags ocp --target-tags ocp-infra-node"
  60. ['infra-node-external']="--allow tcp:80,tcp:443,tcp:1936${range} --target-tags ocp-infra-node"
  61. )
  62. for rule in "${!FW_RULES[@]}"; do
  63. ( if ! gcloud --project "{{ openshift_gcp_project }}" compute firewall-rules describe "{{ openshift_gcp_prefix }}$rule" &>/dev/null; then
  64. gcloud --project "{{ openshift_gcp_project }}" compute firewall-rules create "{{ openshift_gcp_prefix }}$rule" --network "{{ openshift_gcp_network_name }}" ${FW_RULES[$rule]}
  65. else
  66. echo "Firewall rule '{{ openshift_gcp_prefix }}${rule}' already exists"
  67. fi ) &
  68. done
  69. # Master IP
  70. ( if ! gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-ssl-lb-ip" --global &>/dev/null; then
  71. gcloud --project "{{ openshift_gcp_project }}" compute addresses create "{{ openshift_gcp_prefix }}master-ssl-lb-ip" --global
  72. else
  73. echo "IP '{{ openshift_gcp_prefix }}master-ssl-lb-ip' already exists"
  74. fi ) &
  75. # Internal master IP
  76. ( if ! gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-network-lb-ip" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  77. gcloud --project "{{ openshift_gcp_project }}" compute addresses create "{{ openshift_gcp_prefix }}master-network-lb-ip" --region "{{ openshift_gcp_region }}"
  78. else
  79. echo "IP '{{ openshift_gcp_prefix }}master-network-lb-ip' already exists"
  80. fi ) &
  81. # Router IP
  82. ( if ! gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}router-network-lb-ip" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  83. gcloud --project "{{ openshift_gcp_project }}" compute addresses create "{{ openshift_gcp_prefix }}router-network-lb-ip" --region "{{ openshift_gcp_region }}"
  84. else
  85. echo "IP '{{ openshift_gcp_prefix }}router-network-lb-ip' already exists"
  86. fi ) &
  87. {% for node_group in openshift_gcp_node_group_config %}
  88. # configure {{ node_group.name }}
  89. (
  90. if ! gcloud --project "{{ openshift_gcp_project }}" compute instance-templates describe "{{ openshift_gcp_prefix }}instance-template-{{ node_group.name }}" &>/dev/null; then
  91. gcloud --project "{{ openshift_gcp_project }}" compute instance-templates create "{{ openshift_gcp_prefix }}instance-template-{{ node_group.name }}" \
  92. --machine-type "{{ node_group.machine_type }}" --network "{{ openshift_gcp_network_name }}" \
  93. --tags "{{ openshift_gcp_prefix }}ocp,ocp,{{ 'ocp-bootstrap,' if (node_group.bootstrap | default(False)) else '' }}{{ node_group.tags }}" \
  94. --boot-disk-size "{{ node_group.boot_disk_size }}" --boot-disk-type "pd-ssd" \
  95. --scopes "logging-write,monitoring-write,useraccounts-ro,service-control,service-management,storage-ro,compute-rw" \
  96. --image "{{ node_group.image | default('${image}') }}" ${metadata} \
  97. --metadata "bootstrap={{ node_group.bootstrap | default(False) | bool | to_json }},cluster-id={{ openshift_gcp_prefix + openshift_gcp_clusterid }},node-group={{ node_group.name }}"
  98. else
  99. echo "Instance template '{{ openshift_gcp_prefix }}instance-template-{{ node_group.name }}' already exists"
  100. fi
  101. # Create instance group
  102. if ! gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed describe "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" --zone "{{ openshift_gcp_zone }}" &>/dev/null; then
  103. gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed create "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" \
  104. --zone "{{ openshift_gcp_zone }}" --template "{{ openshift_gcp_prefix }}instance-template-{{ node_group.name }}" --size "{{ node_group.scale }}"
  105. else
  106. echo "Instance group '{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}' already exists"
  107. fi
  108. ) &
  109. {% endfor %}
  110. for i in `jobs -p`; do wait $i; done
  111. # Configure the master external LB rules
  112. (
  113. # Master health check
  114. if ! gcloud --project "{{ openshift_gcp_project }}" compute health-checks describe "{{ openshift_gcp_prefix }}master-ssl-lb-health-check" &>/dev/null; then
  115. gcloud --project "{{ openshift_gcp_project }}" compute health-checks create https "{{ openshift_gcp_prefix }}master-ssl-lb-health-check" --port "{{ internal_console_port }}" --request-path "/healthz"
  116. else
  117. echo "Health check '{{ openshift_gcp_prefix }}master-ssl-lb-health-check' already exists"
  118. fi
  119. gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed set-named-ports "{{ openshift_gcp_prefix }}ig-m" \
  120. --zone "{{ openshift_gcp_zone }}" --named-ports "{{ openshift_gcp_prefix }}port-name-master:{{ internal_console_port }}"
  121. # Master backend service
  122. if ! gcloud --project "{{ openshift_gcp_project }}" compute backend-services describe "{{ openshift_gcp_prefix }}master-ssl-lb-backend" --global &>/dev/null; then
  123. gcloud --project "{{ openshift_gcp_project }}" compute backend-services create "{{ openshift_gcp_prefix }}master-ssl-lb-backend" --health-checks "{{ openshift_gcp_prefix }}master-ssl-lb-health-check" --port-name "{{ openshift_gcp_prefix }}port-name-master" --protocol "TCP" --global --timeout="{{ openshift_gcp_master_lb_timeout }}"
  124. gcloud --project "{{ openshift_gcp_project }}" compute backend-services add-backend "{{ openshift_gcp_prefix }}master-ssl-lb-backend" --instance-group "{{ openshift_gcp_prefix }}ig-m" --global --instance-group-zone "{{ openshift_gcp_zone }}"
  125. else
  126. echo "Backend service '{{ openshift_gcp_prefix }}master-ssl-lb-backend' already exists"
  127. fi
  128. # Master tcp proxy target
  129. if ! gcloud --project "{{ openshift_gcp_project }}" compute target-tcp-proxies describe "{{ openshift_gcp_prefix }}master-ssl-lb-target" &>/dev/null; then
  130. gcloud --project "{{ openshift_gcp_project }}" compute target-tcp-proxies create "{{ openshift_gcp_prefix }}master-ssl-lb-target" --backend-service "{{ openshift_gcp_prefix }}master-ssl-lb-backend"
  131. else
  132. echo "Proxy target '{{ openshift_gcp_prefix }}master-ssl-lb-target' already exists"
  133. fi
  134. # Master forwarding rule
  135. if ! gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules describe "{{ openshift_gcp_prefix }}master-ssl-lb-rule" --global &>/dev/null; then
  136. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-ssl-lb-ip" --global --format='value(address)')
  137. gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules create "{{ openshift_gcp_prefix }}master-ssl-lb-rule" --address "$IP" --global --ports "{{ console_port }}" --target-tcp-proxy "{{ openshift_gcp_prefix }}master-ssl-lb-target"
  138. else
  139. echo "Forwarding rule '{{ openshift_gcp_prefix }}master-ssl-lb-rule' already exists"
  140. fi
  141. ) &
  142. # Configure the master internal LB rules
  143. (
  144. # Internal master health check
  145. if ! gcloud --project "{{ openshift_gcp_project }}" compute http-health-checks describe "{{ openshift_gcp_prefix }}master-network-lb-health-check" &>/dev/null; then
  146. gcloud --project "{{ openshift_gcp_project }}" compute http-health-checks create "{{ openshift_gcp_prefix }}master-network-lb-health-check" --port "8080" --request-path "/healthz"
  147. else
  148. echo "Health check '{{ openshift_gcp_prefix }}master-network-lb-health-check' already exists"
  149. fi
  150. # Internal master target pool
  151. if ! gcloud --project "{{ openshift_gcp_project }}" compute target-pools describe "{{ openshift_gcp_prefix }}master-network-lb-pool" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  152. gcloud --project "{{ openshift_gcp_project }}" compute target-pools create "{{ openshift_gcp_prefix }}master-network-lb-pool" --http-health-check "{{ openshift_gcp_prefix }}master-network-lb-health-check" --region "{{ openshift_gcp_region }}"
  153. else
  154. echo "Target pool '{{ openshift_gcp_prefix }}master-network-lb-pool' already exists"
  155. fi
  156. # Internal master forwarding rule
  157. if ! gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules describe "{{ openshift_gcp_prefix }}master-network-lb-rule" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  158. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-network-lb-ip" --region "{{ openshift_gcp_region }}" --format='value(address)')
  159. gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules create "{{ openshift_gcp_prefix }}master-network-lb-rule" --address "$IP" --region "{{ openshift_gcp_region }}" --target-pool "{{ openshift_gcp_prefix }}master-network-lb-pool"
  160. else
  161. echo "Forwarding rule '{{ openshift_gcp_prefix }}master-network-lb-rule' already exists"
  162. fi
  163. ) &
  164. # Configure the infra node rules
  165. (
  166. # Router health check
  167. if ! gcloud --project "{{ openshift_gcp_project }}" compute http-health-checks describe "{{ openshift_gcp_prefix }}router-network-lb-health-check" &>/dev/null; then
  168. gcloud --project "{{ openshift_gcp_project }}" compute http-health-checks create "{{ openshift_gcp_prefix }}router-network-lb-health-check" --port "1936" --request-path "/healthz"
  169. else
  170. echo "Health check '{{ openshift_gcp_prefix }}router-network-lb-health-check' already exists"
  171. fi
  172. # Router target pool
  173. if ! gcloud --project "{{ openshift_gcp_project }}" compute target-pools describe "{{ openshift_gcp_prefix }}router-network-lb-pool" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  174. gcloud --project "{{ openshift_gcp_project }}" compute target-pools create "{{ openshift_gcp_prefix }}router-network-lb-pool" --http-health-check "{{ openshift_gcp_prefix }}router-network-lb-health-check" --region "{{ openshift_gcp_region }}"
  175. else
  176. echo "Target pool '{{ openshift_gcp_prefix }}router-network-lb-pool' already exists"
  177. fi
  178. # Router forwarding rule
  179. if ! gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules describe "{{ openshift_gcp_prefix }}router-network-lb-rule" --region "{{ openshift_gcp_region }}" &>/dev/null; then
  180. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}router-network-lb-ip" --region "{{ openshift_gcp_region }}" --format='value(address)')
  181. gcloud --project "{{ openshift_gcp_project }}" compute forwarding-rules create "{{ openshift_gcp_prefix }}router-network-lb-rule" --address "$IP" --region "{{ openshift_gcp_region }}" --target-pool "{{ openshift_gcp_prefix }}router-network-lb-pool"
  182. else
  183. echo "Forwarding rule '{{ openshift_gcp_prefix }}router-network-lb-rule' already exists"
  184. fi
  185. ) &
  186. for i in `jobs -p`; do wait $i; done
  187. # set the target pools
  188. (
  189. if [[ "ig-m" == "{{ openshift_gcp_infra_network_instance_group }}" ]]; then
  190. gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed set-target-pools "{{ openshift_gcp_prefix }}ig-m" --target-pools "{{ openshift_gcp_prefix }}master-network-lb-pool,{{ openshift_gcp_prefix }}router-network-lb-pool" --zone "{{ openshift_gcp_zone }}"
  191. else
  192. gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed set-target-pools "{{ openshift_gcp_prefix }}ig-m" --target-pools "{{ openshift_gcp_prefix }}master-network-lb-pool" --zone "{{ openshift_gcp_zone }}"
  193. gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed set-target-pools "{{ openshift_gcp_prefix }}{{ openshift_gcp_infra_network_instance_group }}" --target-pools "{{ openshift_gcp_prefix }}router-network-lb-pool" --zone "{{ openshift_gcp_zone }}"
  194. fi
  195. ) &
  196. # configure DNS
  197. (
  198. # Retry DNS changes until they succeed since this may be a shared resource
  199. while true; do
  200. dns="${TMPDIR:-/tmp}/dns.yaml"
  201. rm -f $dns
  202. # DNS record for master lb
  203. if ! gcloud --project "{{ openshift_gcp_project }}" dns record-sets list -z "${dns_zone}" --name "{{ openshift_master_cluster_public_hostname }}" 2>/dev/null | grep -q "{{ openshift_master_cluster_public_hostname }}"; then
  204. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-ssl-lb-ip" --global --format='value(address)')
  205. if [[ ! -f $dns ]]; then
  206. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  207. fi
  208. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns add -z "${dns_zone}" --ttl 3600 --name "{{ openshift_master_cluster_public_hostname }}." --type A "$IP"
  209. else
  210. echo "DNS record for '{{ openshift_master_cluster_public_hostname }}' already exists"
  211. fi
  212. # DNS record for internal master lb
  213. if ! gcloud --project "{{ openshift_gcp_project }}" dns record-sets list -z "${dns_zone}" --name "{{ openshift_master_cluster_hostname }}" 2>/dev/null | grep -q "{{ openshift_master_cluster_hostname }}"; then
  214. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}master-network-lb-ip" --region "{{ openshift_gcp_region }}" --format='value(address)')
  215. if [[ ! -f $dns ]]; then
  216. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  217. fi
  218. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns add -z "${dns_zone}" --ttl 3600 --name "{{ openshift_master_cluster_hostname }}." --type A "$IP"
  219. else
  220. echo "DNS record for '{{ openshift_master_cluster_hostname }}' already exists"
  221. fi
  222. # DNS record for router lb
  223. if ! gcloud --project "{{ openshift_gcp_project }}" dns record-sets list -z "${dns_zone}" --name "{{ wildcard_zone }}" 2>/dev/null | grep -q "{{ wildcard_zone }}"; then
  224. IP=$(gcloud --project "{{ openshift_gcp_project }}" compute addresses describe "{{ openshift_gcp_prefix }}router-network-lb-ip" --region "{{ openshift_gcp_region }}" --format='value(address)')
  225. if [[ ! -f $dns ]]; then
  226. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  227. fi
  228. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns add -z "${dns_zone}" --ttl 3600 --name "{{ wildcard_zone }}." --type A "$IP"
  229. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns add -z "${dns_zone}" --ttl 3600 --name "*.{{ wildcard_zone }}." --type CNAME "{{ wildcard_zone }}."
  230. else
  231. echo "DNS record for '{{ wildcard_zone }}' already exists"
  232. fi
  233. # Commit all DNS changes, retrying if preconditions are not met
  234. if [[ -f $dns ]]; then
  235. if ! out="$( gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns execute -z "${dns_zone}" 2>&1 )"; then
  236. rc=$?
  237. if [[ "${out}" == *"HTTPError 412: Precondition not met"* ]]; then
  238. continue
  239. fi
  240. exit $rc
  241. fi
  242. fi
  243. break
  244. done
  245. ) &
  246. # Create bucket for registry
  247. (
  248. if ! gsutil ls -p "{{ openshift_gcp_project }}" "gs://{{ openshift_gcp_registry_bucket_name }}" &>/dev/null; then
  249. gsutil mb -p "{{ openshift_gcp_project }}" -l "{{ openshift_gcp_region }}" "gs://{{ openshift_gcp_registry_bucket_name }}"
  250. else
  251. echo "Bucket '{{ openshift_gcp_registry_bucket_name }}' already exists"
  252. fi
  253. ) &
  254. # wait until all node groups are stable
  255. {% for node_group in openshift_gcp_node_group_config %}
  256. {% if node_group.wait_for_stable | default(False) or not (node_group.bootstrap | default(False)) %}
  257. # wait for stable {{ node_group.name }}
  258. ( gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed wait-until-stable "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" --zone "{{ openshift_gcp_zone }}" --timeout=600 ) &
  259. {% else %}
  260. # not waiting for {{ node_group.name }} due to bootstrapping
  261. {% endif %}
  262. {% endfor %}
  263. for i in `jobs -p`; do wait $i; done