Skip to content

Commit 53998d3

Browse files
authored
Merge pull request #1324 from mercedes-benz/tobiasgiese/loop-all-gcp-zones
🌱 gce-project.sh: loop over all GCP zones during instance creation
2 parents 0d39705 + 89e479e commit 53998d3

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

hack/ci/gce-project.sh

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function cloud_init {
2222
GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS:-""}
2323
GCP_PROJECT=${GCP_PROJECT:-""}
2424
GCP_REGION=${GCP_REGION:-"us-east4"}
25-
GCP_ZONE=${GCP_ZONE:-"us-east4-a"}
2625
GCP_MACHINE_MIN_CPU_PLATFORM=${GCP_MACHINE_MIN_CPU_PLATFORM:-"Intel Cascade Lake"}
2726
GCP_NETWORK_NAME=${GCP_NETWORK_NAME:-"${CLUSTER_NAME}-mynetwork"}
2827

@@ -40,8 +39,7 @@ function cloud_init {
4039

4140
function init_infrastructure() {
4241
if [[ ${GCP_NETWORK_NAME} != "default" ]]; then
43-
if ! gcloud compute networks describe "$GCP_NETWORK_NAME" --project "$GCP_PROJECT" > /dev/null;
44-
then
42+
if ! gcloud compute networks describe "$GCP_NETWORK_NAME" --project "$GCP_PROJECT" >/dev/null; then
4543
gcloud compute networks create --project "$GCP_PROJECT" "$GCP_NETWORK_NAME" --subnet-mode custom
4644
gcloud compute networks subnets create "$GCP_NETWORK_NAME" --project "$GCP_PROJECT" \
4745
--network="$GCP_NETWORK_NAME" --range="$PRIVATE_NETWORK_CIDR" --region "$GCP_REGION"
@@ -66,17 +64,15 @@ function init_infrastructure() {
6664
gcloud compute networks list --project="$GCP_PROJECT"
6765
gcloud compute networks describe "$GCP_NETWORK_NAME" --project="$GCP_PROJECT"
6866

69-
if ! gcloud compute routers describe "${CLUSTER_NAME}-myrouter" --project="$GCP_PROJECT" --region="$GCP_REGION" > /dev/null;
70-
then
67+
if ! gcloud compute routers describe "${CLUSTER_NAME}-myrouter" --project="$GCP_PROJECT" --region="$GCP_REGION" >/dev/null; then
7168
gcloud compute routers create "${CLUSTER_NAME}-myrouter" --project="$GCP_PROJECT" \
72-
--region="$GCP_REGION" --network="$GCP_NETWORK_NAME"
69+
--region="$GCP_REGION" --network="$GCP_NETWORK_NAME"
7370
fi
7471
if ! gcloud compute routers nats describe --router="$CLUSTER_NAME-myrouter" "$CLUSTER_NAME-mynat" \
75-
--project="$GCP_PROJECT" --region="${GCP_REGION}" > /dev/null;
76-
then
77-
gcloud compute routers nats create "${CLUSTER_NAME}-mynat" --project="$GCP_PROJECT" \
78-
--router-region="$GCP_REGION" --router="${CLUSTER_NAME}-myrouter" \
79-
--nat-all-subnet-ip-ranges --auto-allocate-nat-external-ips
72+
--project="$GCP_PROJECT" --region="${GCP_REGION}" >/dev/null; then
73+
gcloud compute routers nats create "${CLUSTER_NAME}-mynat" --project="$GCP_PROJECT" \
74+
--router-region="$GCP_REGION" --router="${CLUSTER_NAME}-myrouter" \
75+
--nat-all-subnet-ip-ranges --auto-allocate-nat-external-ips
8076
fi
8177
}
8278

@@ -92,49 +88,56 @@ function create_vm {
9288
local diskname="${CLUSTER_NAME}-disk"
9389
local imagename="${servername}-image"
9490

95-
# Create the base disk image based on the public Ubuntu 20.04 LTS cloud image
96-
# Note that this has also been verified to work with CentOS 8 as of
97-
# 2021-01-12, but this is not tested regularly.
98-
# To use CentOS 8:
99-
# --image-project centos-cloud --image-family centos-stream-8
100-
if ! gcloud compute disks describe "$diskname" --project "$GCP_PROJECT" --zone "$GCP_ZONE" > /dev/null;
101-
then
102-
gcloud compute disks create "$diskname" \
103-
--project "$GCP_PROJECT" \
104-
--image-project ubuntu-os-cloud --image-family ubuntu-2004-lts \
105-
--zone "$GCP_ZONE"
106-
fi
107-
108-
if ! gcloud compute images describe "$imagename" --project "$GCP_PROJECT" > /dev/null;
109-
then
110-
gcloud compute images create "$imagename" \
111-
--project "$GCP_PROJECT" \
112-
--source-disk "$diskname" --source-disk-zone "$GCP_ZONE" \
113-
--licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
114-
fi
91+
# Loop over all zones in the GCP region to ignore a full zone.
92+
# We are not able to use 'gcloud compute zones list' as the gcloud.compute.zones.list permission is missing.
93+
for GCP_ZONE in "${GCP_REGION}-a" "${GCP_REGION}-b" "${GCP_REGION}-c"; do
94+
# Check if image was already created.
95+
# Images are not zone specific, but the disk is.
96+
if ! gcloud compute images describe "$imagename" --project "$GCP_PROJECT" >/dev/null; then
97+
# Create the base disk image based on the public Ubuntu 20.04 LTS cloud image
98+
# Note that this has also been verified to work with CentOS 8 as of
99+
# 2021-01-12, but this is not tested regularly.
100+
# To use CentOS 8:
101+
# --image-project centos-cloud --image-family centos-stream-8
102+
if ! gcloud compute disks describe "$diskname" --project "$GCP_PROJECT" --zone "$GCP_ZONE" >/dev/null; then
103+
gcloud compute disks create "$diskname" \
104+
--project "$GCP_PROJECT" \
105+
--image-project ubuntu-os-cloud --image-family ubuntu-2004-lts \
106+
--zone "$GCP_ZONE"
107+
fi
108+
gcloud compute images create "$imagename" \
109+
--project "$GCP_PROJECT" \
110+
--source-disk "$diskname" --source-disk-zone "$GCP_ZONE" \
111+
--licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
112+
fi
115113

116-
if ! gcloud compute instances describe "$servername" --project "$GCP_PROJECT" --zone "$GCP_ZONE" > /dev/null;
117-
then
118-
gcloud compute instances create "$servername" \
119-
--project "$GCP_PROJECT" \
120-
--zone "$GCP_ZONE" \
121-
--image "$imagename" \
122-
--boot-disk-size 200G \
123-
--boot-disk-type pd-ssd \
124-
--can-ip-forward \
125-
--tags http-server,https-server,novnc,openstack-apis \
126-
--min-cpu-platform "$GCP_MACHINE_MIN_CPU_PLATFORM" \
127-
--machine-type "$machine_type" \
128-
--network-interface="private-network-ip=${ip},network=${CLUSTER_NAME}-mynetwork,subnet=${CLUSTER_NAME}-mynetwork" \
129-
--metadata-from-file user-data="$userdata"
130-
fi
114+
if ! gcloud compute instances describe "$servername" --project "$GCP_PROJECT" --zone "$GCP_ZONE" >/dev/null; then
115+
if gcloud compute instances create "$servername" \
116+
--project "$GCP_PROJECT" \
117+
--zone "$GCP_ZONE" \
118+
--image "$imagename" \
119+
--boot-disk-size 200G \
120+
--boot-disk-type pd-ssd \
121+
--can-ip-forward \
122+
--tags http-server,https-server,novnc,openstack-apis \
123+
--min-cpu-platform "$GCP_MACHINE_MIN_CPU_PLATFORM" \
124+
--machine-type "$machine_type" \
125+
--network-interface="private-network-ip=${ip},network=${CLUSTER_NAME}-mynetwork,subnet=${CLUSTER_NAME}-mynetwork" \
126+
--metadata-from-file user-data="$userdata"; then
127+
# return function create_vm if the instance have been created successfully.
128+
return
129+
fi
130+
fi
131+
done
132+
echo "No free GCP zone could be found to create instance $servername."
133+
exit 1
131134
}
132135

133136
function get_public_ip {
134137
local ip
135138
while ! ip=$(gcloud compute instances describe "${CLUSTER_NAME}-controller" \
136-
--project "$GCP_PROJECT" --zone "$GCP_ZONE" \
137-
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'); do
139+
--project "$GCP_PROJECT" --zone "$GCP_ZONE" \
140+
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'); do
138141
echo "Waiting for a public IP"
139142
sleep 5
140143
done

0 commit comments

Comments
 (0)