diff --git a/.gitignore b/.gitignore index a54158d..bf74025 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ +account.json __pycache__/ -terraform.tfvars .terraform/ -terraform/terraform.tfstate -terraform/terraform.tfstate.backup +terraform.tfvars +.terraform.tfstate* +terraform.tfstate* diff --git a/scripts/common.sh b/scripts/common.sh index 3b877a6..9ddf15e 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -20,11 +20,6 @@ # "- -" # "---------------------------------------------------------" -APP_NAME="tracing-demo" -CLUSTER_NAME="tracing-demo-space" -GKE_VERSION=$(gcloud container get-server-config \ - --format="value(validMasterVersions[0])") - # gcloud and kubectl are required for this POC command -v gcloud >/dev/null 2>&1 || { \ echo >&2 "I require gcloud but it's not installed. Aborting."; exit 1; } @@ -32,29 +27,6 @@ command -v gcloud >/dev/null 2>&1 || { \ command -v kubectl >/dev/null 2>&1 || { \ echo >&2 "I require kubectl but it's not installed. Aborting."; exit 1; } -usage() { echo "Usage: $0 [-c ]" 1>&2; exit 1; } - -# parse -c flag for the CLUSTER_NAME using getopts -while getopts ":c:" opt; do - case ${opt} in - c) - CLUSTER_NAME=$OPTARG - ;; - \?) - echo "Invalid flag on command line: $OPTARG" 1>&2 - ;; - *) - usage - ;; - esac -done -shift $((OPTIND -1)) - -# If user did not pass in -c flag then fail -if [ -z "${CLUSTER_NAME}" ]; then - usage -fi - # Get the default zone and use it or die ZONE=$(gcloud config get-value compute/zone) if [ -z "${ZONE}" ]; then diff --git a/scripts/teardown.sh b/scripts/teardown.sh index fce6325..1c9f437 100644 --- a/scripts/teardown.sh +++ b/scripts/teardown.sh @@ -30,5 +30,5 @@ ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "$ROOT"/scripts/common.sh # Tear down Terraform-managed resources and remove generated tfvars -(cd "$ROOT"/terraform; terraform destroy -input=false -auto-approve) +cd "$ROOT"/terraform || exit; terraform destroy -input=false -auto-approve rm -f "$ROOT"/terraform/terraform.tfvars diff --git a/scripts/validate.sh b/scripts/validate.sh index d2b2855..ab9059d 100644 --- a/scripts/validate.sh +++ b/scripts/validate.sh @@ -29,22 +29,27 @@ ROOT=$(dirname "${BASH_SOURCE[0]}")/.. # shellcheck disable=SC1090 source "$ROOT"/scripts/common.sh +APP_NAME=$(kubectl get deployments \ + -ojsonpath='{.items[0].metadata.labels.app}') APP_MESSAGE="deployment \"$APP_NAME\" successfully rolled out" +cd "$ROOT"/terraform || exit; CLUSTER_NAME=$(terraform output cluster_name) \ + ZONE=$(terraform output primary_zone) + # Get credentials for the k8s cluster -gcloud container clusters get-credentials "$CLUSTER_NAME" --zone "$ZONE" +gcloud container clusters get-credentials "$CLUSTER_NAME" --zone="$ZONE" # Wait for the rollout of demo app to finish while true do - ROLLOUT=$(kubectl rollout status --namespace default --watch=false deployment/"$APP_NAME") \ - &> /dev/null + ROLLOUT=$(kubectl rollout status --namespace default \ + --watch=false deployment/"$APP_NAME") &> /dev/null if [[ $ROLLOUT = *"$APP_MESSAGE"* ]]; then break fi sleep 2 done -echo "step 1 of the validation passed." +echo "Step 1 of the validation passed. App is deployed." # Grab the external IP and port of the service to confirm that demo app # deployed correctly. @@ -60,7 +65,7 @@ do if [[ $EXT_IP =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then break - elif [[ $EXT_PORT =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + elif [[ $EXT_PORT =~ ^0*(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9])$ ]]; then break else continue @@ -70,4 +75,4 @@ done echo "App is available at: http://$EXT_IP:$EXT_PORT" [ "$(curl -s -o /dev/null -w '%{http_code}' "$EXT_IP:$EXT_PORT"/)" \ -eq 200 ] || exit 1 -echo "step 2 of the validation passed." +echo "Step 2 of the validation passed. App handles requests." diff --git a/terraform/main.tf b/terraform/main.tf index b56a05a..d6c6433 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -64,3 +64,11 @@ resource "google_pubsub_subscription" "tracing-demo-subscription" { name = "tracing-demo-cli" topic = "${google_pubsub_topic.tracing-demo-topic.name}" } + +output "cluster_name" { + value = "${google_container_cluster.primary.name}" +} + +output "primary_zone" { + value = "${google_container_cluster.primary.zone}" +}