Skip to content

Commit 26ea28d

Browse files
feat: ACM - Wait for gatekeeper & Hub: expose module_depends_on (#689)
* wait for gatekeeper if policy controller is enabled * destroy command * forgot the var * use deployment ready * add context * more wait * webhook * depends_on * concat modules * depends on * terraform format * update docs * shell check
1 parent c5d1e4d commit 26ea28d

File tree

8 files changed

+115
-1
lines changed

8 files changed

+115
-1
lines changed

Diff for: modules/acm/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ By default, this module will attempt to download the ACM operator from Google di
6868
| Name | Description |
6969
|------|-------------|
7070
| git\_creds\_public | Public key of SSH keypair to allow the Anthos Config Management Operator to authenticate to your Git repository. |
71+
| wait | An output to use when you want to depend on cmd finishing |
7172

7273
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Diff for: modules/acm/outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ output "git_creds_public" {
1818
description = "Public key of SSH keypair to allow the Anthos Config Management Operator to authenticate to your Git repository."
1919
value = module.acm_operator.git_creds_public
2020
}
21+
22+
output "wait" {
23+
description = "An output to use when you want to depend on cmd finishing"
24+
value = module.acm_operator.wait
25+
}

Diff for: modules/hub/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ To deploy this config:
4040
| gke\_hub\_membership\_name | Memebership name that uniquely represents the cluster being registered on the Hub | string | `"gke-hub-membership"` | no |
4141
| gke\_hub\_sa\_name | Name for the GKE Hub SA stored as a secret `creds-gcp` in the `gke-connect` namespace. | string | `"gke-hub-sa"` | no |
4242
| location | The location (zone or region) this cluster has been created in. | string | n/a | yes |
43+
| module\_depends\_on | List of modules or resources this module depends on. | list | `<list>` | no |
4344
| project\_id | The project in which the resource belongs. | string | n/a | yes |
4445
| sa\_private\_key | Private key for service account base64 encoded. Required only if `use_existing_sa` is set to `true`. | string | `"null"` | no |
4546
| skip\_gcloud\_download | Whether to skip downloading gcloud (assumes gcloud and kubectl already available outside the module) | bool | `"true"` | no |

Diff for: modules/hub/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module "gke_hub_registration" {
4949
skip_download = var.skip_gcloud_download
5050
upgrade = true
5151
use_tf_google_credentials_env_var = var.use_tf_google_credentials_env_var
52-
module_depends_on = [var.cluster_endpoint]
52+
module_depends_on = concat([var.cluster_endpoint], var.module_depends_on)
5353

5454
create_cmd_entrypoint = "${path.module}/scripts/gke_hub_registration.sh"
5555
create_cmd_body = "${var.gke_hub_membership_name} ${var.location} ${var.cluster_name} ${local.gke_hub_sa_key} ${var.project_id}"

Diff for: modules/hub/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ variable "sa_private_key" {
8181
type = string
8282
default = null
8383
}
84+
85+
variable "module_depends_on" {
86+
description = "List of modules or resources this module depends on."
87+
type = list
88+
default = []
89+
}

Diff for: modules/k8s-operator-crd-support/main.tf

+16
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ module "k8sop_config" {
113113
kubectl_create_command = "kubectl apply -f ${local_file.operator_cr.filename}"
114114
kubectl_destroy_command = "kubectl delete -f ${local_file.operator_cr.filename}"
115115
}
116+
117+
module "wait_for_gatekeeper" {
118+
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
119+
version = "~> 2.0.2"
120+
enabled = var.enable_policy_controller ? true : false
121+
module_depends_on = [module.k8sop_config.wait]
122+
skip_download = var.skip_gcloud_download
123+
cluster_name = var.cluster_name
124+
cluster_location = var.location
125+
project_id = var.project_id
126+
create_cmd_triggers = { script_sha1 = sha1(file("${path.module}/scripts/wait_for_gatekeeper.sh")) }
127+
service_account_key_file = var.service_account_key_file
128+
129+
kubectl_create_command = "${path.module}/scripts/wait_for_gatekeeper.sh ${var.project_id} ${var.cluster_name} ${var.location}"
130+
kubectl_destroy_command = ""
131+
}

Diff for: modules/k8s-operator-crd-support/outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ output "git_creds_public" {
1919
value = var.create_ssh_key ? tls_private_key.k8sop_creds.*.public_key_openssh : null
2020
}
2121

22+
output "wait" {
23+
description = "An output to use when you want to depend on cmd finishing"
24+
value = var.enable_policy_controller ? module.wait_for_gatekeeper.wait : module.k8sop_config.wait
25+
}
26+
2227

2328

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
is_deployment_ready() {
17+
kubectl --context "$1" -n "$2" get deploy "$3" &> /dev/null
18+
export exit_code=$?
19+
while [ ! " ${exit_code} " -eq 0 ]
20+
do
21+
sleep 5
22+
echo -e "Waiting for deployment $3 in cluster $1 to be created..."
23+
kubectl --context "$1" -n "$2" get deploy "$3" &> /dev/null
24+
export exit_code=$?
25+
done
26+
echo -e "Deployment $3 in cluster $1 created."
27+
28+
# Once deployment is created, check for deployment status.availableReplicas is greater than 0
29+
availableReplicas=$(kubectl --context "$1" -n "$2" get deploy "$3" -o json | jq -r '.status.availableReplicas')
30+
while [[ " ${availableReplicas} " == " null " ]]
31+
do
32+
sleep 5
33+
echo -e "Waiting for deployment $3 in cluster $1 to become ready..."
34+
availableReplicas=$(kubectl --context "$1" -n "$2" get deploy "$3" -o json | jq -r '.status.availableReplicas')
35+
done
36+
37+
echo -e "$3 in cluster $1 is ready with replicas ${availableReplicas}."
38+
return "${availableReplicas}"
39+
}
40+
41+
is_service_ready() {
42+
kubectl --context "$1" -n "$2" get service "$3" &> /dev/null
43+
export exit_code=$?
44+
while [ ! " ${exit_code} " -eq 0 ]
45+
do
46+
sleep 5
47+
echo -e "Waiting for service $3 in cluster $1 to be created..."
48+
kubectl --context "$1" -n "$2" get service "$3" &> /dev/null
49+
export exit_code=$?
50+
done
51+
echo -e "Service $3 in cluster $1 created."
52+
53+
# Once service is created, check endpoints is greater than 0
54+
kubectl --context "$1" -n "$2" get endpoints "$3"
55+
export exit_code=$?
56+
57+
while [ ! " ${exit_code} " -eq 0 ]
58+
do
59+
sleep 5
60+
echo -e "Waiting for endpoints for service $3 in cluster $1 to become ready..."
61+
kubectl --context "$1" -n "$2" get endpoints "$3"
62+
export exit_code=$?
63+
done
64+
65+
echo -e "Service $3 in cluster $1 is ready with endpoints."
66+
return
67+
}
68+
69+
if [ "$#" -lt 3 ]; then
70+
>&2 echo "Not all expected arguments set."
71+
exit 1
72+
fi
73+
74+
PROJECT_ID=$1
75+
CLUSTER_NAME=$2
76+
CLUSTER_LOCATION=$3
77+
78+
# Gatekeeper causes issues if not ready
79+
is_deployment_ready gke_"${PROJECT_ID}"_"${CLUSTER_LOCATION}"_"${CLUSTER_NAME}" gatekeeper-system gatekeeper-controller-manager
80+
is_service_ready gke_"${PROJECT_ID}"_"${CLUSTER_LOCATION}"_"${CLUSTER_NAME}" gatekeeper-system gatekeeper-webhook-service

0 commit comments

Comments
 (0)