Skip to content

Commit 9e3adbc

Browse files
committed
Wait for GKE cluster to not be in RECONCILING status before completing Terraform runs or destroying resources
1 parent 68dcd99 commit 9e3adbc

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

cluster_regional.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,18 @@ resource "google_container_node_pool" "pools" {
134134

135135
depends_on = ["google_container_cluster.primary"]
136136
}
137+
138+
resource "null_resource" "wait_for_regional_cluster" {
139+
count = "${var.regional ? 1 : 0}"
140+
141+
provisioner "local-exec" {
142+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
143+
}
144+
145+
provisioner "local-exec" {
146+
when = "destroy"
147+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
148+
}
149+
150+
depends_on = ["google_container_cluster.primary", "google_container_node_pool.pools"]
151+
}

cluster_zonal.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,18 @@ resource "google_container_node_pool" "zonal_pools" {
134134

135135
depends_on = ["google_container_cluster.zonal_primary"]
136136
}
137+
138+
resource "null_resource" "wait_for_zonal_cluster" {
139+
count = "${var.regional ? 0 : 1}"
140+
141+
provisioner "local-exec" {
142+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
143+
}
144+
145+
provisioner "local-exec" {
146+
when = "destroy"
147+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
148+
}
149+
150+
depends_on = ["google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
151+
}

scripts/wait-for-cluster.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -e
17+
18+
PROJECT=$1
19+
CLUSTER_NAME=$2
20+
gcloud_command="gcloud container clusters list --project=$PROJECT --format=json"
21+
jq_query=".[] | select(.name==\"$CLUSTER_NAME\") | .status"
22+
23+
echo "Waiting for cluster $2 in project $1 to reconcile..."
24+
25+
current_status=$($gcloud_command | jq -r "$jq_query")
26+
27+
while [[ "${current_status}" == "RECONCILING" ]]; do
28+
printf "."
29+
sleep 5
30+
current_status=$($gcloud_command | jq -r "$jq_query")
31+
done
32+
33+
echo "Cluster is ready!"

0 commit comments

Comments
 (0)