Skip to content

Instantly share code, notes, and snippets.

@lestrrat
Created October 30, 2018 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lestrrat/f0261d89d1046ef9afb17e4fb9008fb9 to your computer and use it in GitHub Desktop.
Save lestrrat/f0261d89d1046ef9afb17e4fb9008fb9 to your computer and use it in GitHub Desktop.
Ever wanted to make health checks for GCP LBs (created from GKE ingress objects) to look at Cloud Endpoint's ESP server's alternate health check port? Well you're in luck, my friend.
#!/bin/bash
set -e
# Script to configure the healthcheck for ESP
# (ESP listens on a separate port from the main port for healthchecks)
SERVICE_NAME="This the name of your service that you proxy your requests to"
ESP_HEALTH_CHECK_PORT_NAME="This is the name of the port that ESP listens for health checks."
ESP_MAIN_PORT_NAME="This is the name of the port that ESP listens for the actual service."
NODE_PORT=$(kubectl get services --namespace production -o json $SERVICE_NAME | jq -Mr ".spec.ports[] | select(.name == '$ESP_HEALTH_CHECK_PORT_NAME') | .nodePort")
# Check if we already have a health check that uses the above port,
# with the path == /healthz. if we do already have it, we're done
# jq -e forces jq to exit with an error status (4) if there was no
# valid output.
set +e
gcloud compute health-checks list --format json | \
jq -Mre ".[] | select((.httpHealthCheck.port == $NODE_PORT) and .httpHealthCheck.requestPath == '/healthz')" >/dev/null 2>&1
HCCHECK=$?
set -e
if [[ "$HCCHECK" == "0" ]]; then
echo "Health check already properly configured"
exit 0
fi
# If not found, find the health check that is targeting the
# API port.
API_NODE_PORT=$(kubectl get services --namespace production -o json $SERVICE_NAME | jq -Mr ".spec.ports[] | select(.name == '$ESP_MAIN_PORT_NAME') | .nodePort")
echo "Going to look for health check for API port $API_NODE_PORT"
HCNAME=$(gcloud compute health-checks list --format json | \
jq -Mre ".[] | select(.httpHealthCheck.port == $NODE_PORT) | .name")
echo "Going to update $HCNAME to look at port $NODE_PORT"
# Once we got the health-check name, update it
echo "Updating...."
gcloud compute health-checks update http --request-path=/healthz --port=$NODE_PORT $HCNAME
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment