|
| 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